1

So I have many variable like status_CR001,status_CR002,status_CR003.

and I want to get that variable based on for looping, here's my code :

  for (int i = 1; i<5; i++) {
                T_Log_Result.id_log = GetUniqID();
                T_Log_Result.email_user = userID;
                T_Log_Result.bagian = "CR00"+i;
                T_Log_Result.report_status = status_CR00(i);// PROBLEM AT THIS LINE
                T_Log_Result.phase = 1;
                T_Log_Result.schedule = filter + " Mingguan";
                T_Log_Result.date_created = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                T_Log_Result.date_updated = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                _context.Add(T_Log_Result);
                await _context.SaveChangesAsync();
            }

So I dont have to do this :

 T_Log_Result.id_log = GetUniqID();
                T_Log_Result.email_user = userID;
                T_Log_Result.bagian = "CR001";
                T_Log_Result.report_status = status_CR001;//THIS LINE
                T_Log_Result.phase = 1;
                T_Log_Result.schedule = filter + " Mingguan";
                T_Log_Result.date_created = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                T_Log_Result.date_updated = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                _context.Add(T_Log_Result);
                await _context.SaveChangesAsync();

 T_Log_Result.id_log = GetUniqID();
                T_Log_Result.email_user = userID;
                T_Log_Result.bagian = "CR002";
                T_Log_Result.report_status = status_CR002;//THIS LINE
                T_Log_Result.phase = 1;
                T_Log_Result.schedule = filter + " Mingguan";
                T_Log_Result.date_created = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                T_Log_Result.date_updated = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                _context.Add(T_Log_Result);
                await _context.SaveChangesAsync();

It's easy to do that on PHP, but how about C# ?

PHP code example :

 $status_CR001="first";
 $status_CR002="second";
 $status_CR003="third";
 for ($i = 1; $i < 4; $i++) {
  echo ${'status_CR00'.$i};
 }
7
  • 2
    You probably want a Dictionary<int, string> or something. Commented Mar 3, 2020 at 7:57
  • 1
    Or use an array or List would, problem solved Commented Mar 3, 2020 at 7:58
  • status_CR001 feels like it should possibly be an array? what is status_CR001 (etc); can we see where they are declared? Commented Mar 3, 2020 at 7:58
  • it's not an array, it's just a variable that I created for testing. Commented Mar 3, 2020 at 8:00
  • 1
    Use an array or a collection rather than different variables. Then you can use the array index for the loop. Commented Mar 3, 2020 at 8:02

2 Answers 2

3

A minimal change solution might be to write a mapping function:

    private SomeType GetStatus(int id) => id switch {
        1 => status_CR001,
        2 => status_CR002,
        3 => status_CR003,
        _ => throw new ArgumentException(nameof(id)),
    };

and use GetStatus(i) in your code.

However, it feels like the better solution here would be for this to be an array, i.e. SomeType[] status_CR (and remember that the index is zero-based, not one-based, so you'd need status_CR[i - 1]).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use reflection :

for (int i = 1; i < 5; i++)
{
    // some code ...

    // note that status can be null
    var status = this.GetType()
                     .GetField($"status_CR00{i}",
                               BindingFlags.NonPublic    // use this flag to find private members
                               | BindingFlags.Instance)? // and this one if status_CR00... is non static
                     .GetValue(this);

    // more code ...
}

However, I strongly suggest you to use arrays/list/whatever collections to achieve that

3 Comments

The owner of fields (status_cr00) must a class (not method) ?
@AdemAygun indeed
@AdemAygun if they are in a method, then they are "locals", not "fields", so it becomes a tautology vs "the owner of fields" (I'm going to just ignore everything related to method rewrites here - captures, iterators, async, local functions, etc)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.