0

Is it possible to get an elements variable name in a foreach loop? I have tried nameof(v), which does not seem to work. See below:

//check for blank answer boxes and set flag for validation
            foreach (string v in new object[] { answer3, answer4, answer5, answer6b, answer11, answer12, answer13b, answer14c, answer18, answer20 })
            {
                if (string.IsNullOrWhiteSpace(v))
                {
                    s1.Flag = true;
                    s1.FlagContent += $"Blank answer box: {nameof(v)}. ";
                }
            }

For example, if answer3 was null or contained white space, s1.Flag would be set to true and s1.FlagContent would be set to "Blank answer box: answer3".

1
  • Both sN and answerN should be arrays instead of numbered variables, or a list of question-answer classes. Commented Mar 25, 2020 at 21:08

2 Answers 2

2

use a Dictionary:

string answer3 = "bla";
string answer4 = null;
string answer5 = "";
var answers = new Dictionary<string,string>();
answers.Add( nameof(answer3), answer3 );
answers.Add( nameof(answer4), answer4 );
answers.Add( nameof(answer5), answer5 );

foreach( var v in answers )
{
    if (string.IsNullOrWhiteSpace(v.Value))
    {
        s1.Flag = true;
        s1.FlagContent += $"Blank answer box: {v.Key}. ";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you're asking is not possible. And when you consider that a given object may be referenced by any number of variables (all of which could have different names), this idea doesn't even make sense.

The typical way to handle this sort of scenario is to store the answers in a dictionary instead of giving them separate variables. If you absolutely have to have them in separate variables, you can convert them to a dictionary like this:

var dictionary = new Dictionary<string, string>
{
    { "answer1", answer1 },
    { "answer2", answer2 },
    { "answer3", answer3 },
    { "answer4", answer4 },
    { "answer5", answer5 }
};

Then the problem is trivial:

foreach (var item in dictionary)
{
    if (string.IsNullOrWhiteSpace(item.Value))
    {
        s1.Flag = true;
        s1.FlagContent += $"Blank answer box: {item.Key}.";
    }
}

1 Comment

For my specific use case it makes sense, and I need to have these separate variables: they are used to temporarily store answers to survey questions which have been converted from handwritten text via an OCR API. I currently scan over each survey question, and store the answer in one of these variables. At the end of each survey all of the answers are associated with a Survey object to be then stored in a db. The idea of the above code is to add some validation to the extraction process i.e. catch and flag incomplete answers. Using a dictionary to store these variables makes sense, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.