-1

I am trying to make a query system for objects, but I have been unable to figure out how to turn a string from Console.ReadLine() to an object's name. Here is an example code snippet:

class Program
{
    Notes note = new Notes();
    note.notes = "note";

    Notes note2 = new Notes();
    note2.notes = "note2";

    Console.WriteLine("which note would you like?");
    string which = Console.ReadLine();
    if(which.ToUpper == ("NOTE" || "NOTE2")
    {
        Console.WriteLine(which + "\'s note is " + which.note);//this is where I need to find an object from a string
    }else
    {
        Console.WriteLine("There is no note called " + which);
    }
}
class Notes
{
    string notes;
}

For context, I intend to add a lot more objects into this and a large amount of if statements to find the right object will not be very practical

If I need to be more clear, please say so. Thanks for any help you can give.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Taxolotl
  • 13
  • 5
  • 2
    I mean, you _could_ use reflection to get a variable's value by variable name, but it's going to be a lot easier to use a Dictionary. – gunr2171 Dec 21 '22 at 03:20
  • c# is case sensitive so "NOTE" does not equal "note". Use which.ToUpper() – jdweng Dec 21 '22 at 03:21
  • You want to use Activator.CreateInstance. Try this SO answer: [enter link description here](https://stackoverflow.com/questions/223952/create-an-instance-of-a-class-from-a-string) – Tom Regan Dec 21 '22 at 03:32

2 Answers2

0

since there can be many notes, you can use list to search

Notes note = new Notes();
note.notes = "note";

Notes note2 = new Notes();
note2.notes = "note2";

var noteList = new List<Notes>();
noteList.Add(note);
noteList.Add(note2);

Notes foundNote = null
foreach(var note in noteList)
{
    if (note.notes.Equals(which, StringComparison.OrdinalIgnoreCase))
    {
        foundNote = note;
        break;
    }
}


if (foundNote != null)
    Console.WriteLine("the note is {0}", foundNote.Notes);
else
   Console.WriteLine("no notes");


T.S.
  • 18,195
  • 11
  • 58
  • 78
  • This took some modification, and I ended up just adding a name member and checking if it matched the user input, but in the end I used list to solve my problem – Taxolotl Dec 21 '22 at 17:24
0

you have two choices either you can choose dictionary or use the reflection approach.

solution with dictionary.

Dictionary<string, Notes> dict = new Dictionary<string, Notes>();

Notes note = new Notes();
note.notes = "note";
dict["note"] = note;


Notes selectedNote = dict[which];
Console.WriteLine($"{which}'s note is {selectedNote.notes}");

solution with reflection.

FieldInfo field = typeof(Program).GetField(which, BindingFlags.NonPublic);
if (field != null)
{
    object selectedNote = field.GetValue(null);
    Console.WriteLine($"{which}'s note is {((Notes)selectedNote).notes}");
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 1
    IMHO use case insensitive keys on the dictionary; `new Dictionary(StringComparison.OrdinalIgnoreCase)`. Then `dict.TryGetValue` to validate user input without an exception. – Jeremy Lakeman Dec 21 '22 at 04:14