-2

I would like to use a string from user input as the name of a new object. This is where i'm currently at:

string newMemberName = "";
Human Mihkel = new Human("Mihkel", 1); //example of creating an object

Console.WriteLine("Please enter the desired name of the new human member!");
                    newMemberName = Console.ReadLine();
                    Human [newMemberName] \*this would be the user input*\ = new Human(newMemberName, 1);

I can't figure out how to do it. I've googled the hell out of it and still nothing. I'm not experienced in C# aswell but after hours of investigation and tutorials I just can't find a solution for this problem.

  • 3
    You're trying to create a new variable, where the name of the variable is determined at runtime. That sounds like a really bad idea, and borderline not possible. Why? This seems like an [X/Y problem](https://xyproblem.info/). – gunr2171 Jul 13 '22 at 21:59
  • i'm sorry for being clumsy, i forgot to add that newMemberName is declared before all the added code as string newMemberName = "" – Karl Kadak Jul 13 '22 at 22:01
  • So then what's your question? Give a different variable name (that isn't already used elsewhere in the scope), and the rest of the last line is fine. – gunr2171 Jul 13 '22 at 22:04
  • The point is that the user should be able to create a new object many times. This code runs in a loop until the application is closed. This is why I want to use user input, so that they could create multiple objects. – Karl Kadak Jul 13 '22 at 22:08

1 Answers1

1

You're using the wrong data structure. Trying to use dynamic variable names is a bad idea, mostly not possible, and would make the code horrible in the attempts to use those variables. The vast majority of code written would be trying to hack together a working solution to the problem of naming a variable, entirely forgetting the actual domain logic you're trying to implement and the actual solution you're trying to build.

Data structures exist for such things. This sounds like a good use case for something like a Dictionary<string, Human> instead. You can declare the collection in advance:

var humans = new Dictionary<string, Human>();

And then add key/value pairs where the name (from user input) is the (always unique) key:

humans.Add(newMemberName, new Human(newMemberName, 1));

Then you can always refer to that element from the collection by its name:

humans[newMemberName].SomeProperty = "some value";

Though there's a case to be made here that, unless there's a really good reason to use a Dictionary<> (extremely fast lookups, guaranteed uniqueness of keys), you're still "duplicating data" because both the key and the "name" value in the object itself are the same value.

What happens if a Human ever changes its name? The key is now wrong.

For small data sets and simple application logic, a List<Human> will do just fine:

var humans = new List<Human>();
humans.Add(new Human(newMemberName, 1));

And you can query it by the name, or any other query you'd like to use:

humans.Single(h => h.Name == newMemberName).SomeProperty = "some value";
David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks! That is exactly what I was looking for. Real sorry about the stupid question. I'm starting to think if programming and CS in general really is the thing for me. Thanks anyways. – Karl Kadak Jul 13 '22 at 22:13
  • 1
    @KarlKadak: If it's any consolation, this same thing is asked *at least* once a day in just about any language here. Aside from the case of dynamic property names in JavaScript or "variable variables" in PHP, the response is almost always unilaterally the same. When one wants to create multiple variables following some pattern, what one *really* wants is a collection of some kind. (Array, list, set, dictionary, etc., etc.) – David Jul 13 '22 at 22:15
  • @KarlKadak David is correct of course, but FYI the `System.Reflection` namespace is still quite a lot of fun to play with, ...for some subset of possible definitions of "fun" - you can even write C# code that emits IL code (what C# compiles down to) at run-time! Not that I'd recommend anything like it in any kind of production scenario, but it's still good to know it exists =) – Mathieu Guindon Jul 13 '22 at 22:54
  • @David Thanks, I comfort myself with the fact that I'm still new to all this and doing it so that it would be easier for me in uni. – Karl Kadak Jul 13 '22 at 23:07
  • @MathieuGuindon I'll surely look into it! Thanks! – Karl Kadak Jul 13 '22 at 23:07