C
C#8mo 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
Thinker8mo 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
stubby8mo ago
i dont really understand i am trying to recusrively create objects based off of user input
stubby
stubby8mo ago
No description
stubby
stubby8mo ago
im not sure what to put between Item and =
Thinker
Thinker8mo ago
what is items?
stubby
stubby8mo ago
a list of all items
stubby
stubby8mo ago
No description
stubby
stubby8mo ago
the code works if i put a random variable name, but it will only work once not variable, object
Thinker
Thinker8mo ago
Well, you'll need a loop in you want to enter multiple items
stubby
stubby8mo ago
yeah this is in a loop oh shit it does work
Thinker
Thinker8mo ago
yeah
stubby
stubby8mo ago
haha my bad
Thinker
Thinker8mo 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
stubby8mo ago
ok i understand
Thinker
Thinker8mo ago
There is no such thing as variables building up over the course of a loop
stubby
stubby8mo ago
thanks a lot haha
Accord
Accord8mo 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
More Posts
❔ Hi for some reason this exercise on indexing isnt clicking for me can someone guide me this?1-im used to properties that have a name this property has no name so i guess it refers to the class❔ Limit incremental generator's RegisterPostInitializationOutput() call to one specific assemblyIs it possible to add an attribute declaration code to a single assembly and let the code generation❔ What happening in background when i return Task.CompletedTask?I just wonder, what c# doing if i have async method returning Task.completedTask? does it try to syn✅ Razor pages, not reaching OnPost methodI have made a <form method="post"> and within it i have a <button type="submit" >. when i click on ❔ Visual Studio build error: "mark of the web"???Get this error every time I try to build my project. All I did was add a toolstrip to a form and now✅ Replace enum with class nameI'm working on an API right now which, for those familiar with OpenMP, is a parallel programming API❔ What is the identity open-source platform such as Keycloack for asp.net core backend ?title✅ How do I make a window unselectableHow do I make a window like this (I'm using avalonia but any help is preferable)❔ Blazor .NET 8 RenderModeInteractiveAuto - fetch data via APIBlazor WASM: use `HttpClient` to call service (co-hosted or external) Blazor Server: interactivity i❔ ✅ Help with bingo game (beginner school project)I'm very blocked and maybe someone can help me. There's a lot to explain but I'll provide any additi