1

I am making a password manager program, and in this program each login is an object. I would like to dynamically name these objects with a unique ID for each login, but what I cannot figure out is how to name the object something without flat out declaring a name as follows:

Login Google = new Login(info)

what i want to do is something like:

Login [id] = new Login(info)

Is there a way to do this?

Andrew C
  • 137
  • 2
  • 10
  • 1
    It sounds like you're looking to create an array or dictionary or collection of some kind. What's the overall goal here? That is, what problem are you trying to solve by doing this? – David Aug 04 '20 at 19:15
  • Do you need to resolve the name at compile time? What's the type of `id`? You've shown assigning the login to a local variable (or possibly a field of a class) but you haven't shown any surrounding code. For example, how do you hope to access the Login again? This needs more context. – Wyck Aug 04 '20 at 19:20
  • Does this answer your question? [Store class definition in dictionary, instance later](https://stackoverflow.com/questions/13188784/store-class-definition-in-dictionary-instance-later). Also why not store them in a `List`? – Trevor Aug 04 '20 at 19:29

1 Answers1

4

You're going to want to use a Dictionary in this case. You would arrange your code like this:

var accounts = new Dictionary<string, Login>();
accounts["id"] = new Login(info);
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39