C
C#14mo ago
stubby

❔ How do I name an object after a variable in that object?

I have a class with a variable called "name", how would i make "name" the name of the object?
17 Replies
Thinker
Thinker14mo ago
Objects don't have "names" What you can have is a field/property in the object which is a string which is the name eg.
public class MyClass
{
public string Name { get; set; }
// other data...
}

MyClass foo = new MyClass() { Name = "foo" };
public class MyClass
{
public string Name { get; set; }
// other data...
}

MyClass foo = new MyClass() { Name = "foo" };
stubby
stubbyOP14mo ago
i dont really understand i am trying to recusrively create objects based off of user input
stubby
stubbyOP14mo ago
No description
stubby
stubbyOP14mo ago
im not sure what to put between Item and =
Thinker
Thinker14mo ago
what is items?
stubby
stubbyOP14mo ago
a list of all items
stubby
stubbyOP14mo ago
No description
stubby
stubbyOP14mo ago
the code works if i put a random variable name, but it will only work once not variable, object
Thinker
Thinker14mo ago
Well, you'll need a loop in you want to enter multiple items
stubby
stubbyOP14mo ago
yeah this is in a loop oh shit it does work
Thinker
Thinker14mo ago
yeah
stubby
stubbyOP14mo ago
haha my bad
Thinker
Thinker14mo ago
When you declare a variable, that variable is local to the current scope, in this case your loop. After an iteration of the loop has finished, the variable is "deleted" and is redeclared on the next iteration.
stubby
stubbyOP14mo ago
ok i understand
Thinker
Thinker14mo ago
There is no such thing as variables building up over the course of a loop
stubby
stubbyOP14mo ago
thanks a lot haha
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server