-3

Is it possible to change a string from another value? Here is my C# code:

if (QuestionNum == 1 && inputAnswer == RightAnswer || inputAnswer == RightAnswerLower)
{
    Program.GotQ1Correct = true;
}

I know that in Lua I could of just changed the Program.GotQ1Correct = true; part to

Program.GotQ[QuestionNum]Correct = true; 

However was just wondering if this was possible in C#.

Edit

I'm very sorry if I wasn't clear before, so basically in the above lua code it would change GotQ1Correct to GotQ2Correct and so on and was just wondering if there was a similar, simple way to do this in C# without arrays.

Max Best
  • 1
  • 1
  • 7
    Hi Max. Welcome to StackOverflow. It's really difficult to understand what you are asking because you have posted a tiny part of the code that leaves out most of the context. Can you create a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve)? – omajid Nov 08 '18 at 18:07
  • Alright, what are you actually asking for? `Program.GotQ[QuestionNum] looks to be an array. Are you asking about arrays? Can't you create an `Question[] questions = { new Question("What is 2+2", "4"); };` or something? – Frontear Nov 08 '18 at 18:08
  • yes its possible of course but your question is not at all clear. You seem, to be asking 'can I use an array of questions' answer 'yes you can' – pm100 Nov 08 '18 at 18:10
  • You can modify the value of a string, but since a string is immutable the original reference still exists in memory until the garbage collector picks up that memory block. Every instance or modification of a string is creating a new block of memory with your value. – Greg Nov 08 '18 at 18:11
  • @Greg - i think the 'string' part of the question is irrelevant. THe sample code is not doing anything to strings – pm100 Nov 08 '18 at 18:12
  • You may also want to lookup a dictionary collection, it will reference a key value pair, in essence id which may be ideal for your question number and a proper answer in the value, so you can see if the user input matches. – Greg Nov 08 '18 at 18:12
  • @pm100 You may be correct, not sure what he meant by change a string in a function, so I assumed the value was being modified. Not clear, so I figured I would add that feedback. – Greg Nov 08 '18 at 18:13

3 Answers3

1

seems like you need

    // assuming 10 questions
    var results = new bool[10];
    var correctAnswers = new string[10]; 
    var studAnswers = new string[10];

    for (int i; i < 10 ; i++)
    {
        if(studAnswer[i].ToLower() == correctAnswers[i].ToLower())
            results[i] = true;

}

or slightly cleaner

 results[i] = studAnswer[i].ToLower() == correctAnswers[i].ToLower();
pm100
  • 48,078
  • 23
  • 82
  • 145
0

For more clarity I might do a dictionary.

public IDictionary<int, bool> CheckAnswers(Dictionary<int, string> exam, Dictionary<int, string> answers)
{
     var results = new Dictionary<int, bool>();
     for(var index = 0; index < exam.Length; index++)
     {
         if(String.Compare(exam[index], answers[index], true) == 0)
             results.Add(exam[index], true);

          results.Add(exam[index], false);
     }

     return results;
}

var correct = CheckAnswers(..., ...).Where(answer => answer.Value == true).Count();

This solution needs refinement, but it is a nice compliment to PM's answer. One of these should at least point you in the proper direction.

The reason I recommended a dictionary, would be you could pass an answer key and exam result. Allowing you to see which are correct and wrong, then use a tally to get the score relatively easy.

Greg
  • 11,302
  • 2
  • 48
  • 79
-1

So if understand you want to access a property of an object by replacing the name of access property? You might want to check this post accessing-object-property-as-string

Copied from the post

System.Reflection.PropertyInfo prop =typeof(YourType).GetProperty("PropertyName");

object value = prop.GetValue(yourInstance); ... prop.SetValue(yourInstance, "value");

JhonnyAggo
  • 24
  • 2