C#: How to Get an Object by the Name Stored in String

C#: how to get an object by the name stored in String?

No, it's not.

Objects don't have names - variables do. An object may be referenced by any number of variables: zero, one or many.

What you can do, however, is get fields (static or instance variables) by name (using Type.GetField) and get the values of those fields (for a specific instance, if you're using instance variables).

Depending on what you're trying to do, you might also want to consider a dictionary from names to objects.

How would I select a object by name from a string

Assuming WinForms...

Use the Controls.Find() function:

for(int i = 0; i < 208; ++i)
{
string pnlName = "panel" + i.ToString();
Panel pnl = this.Controls.Find(pnlName, true).FirstOrDefault() As Panel;
if (pnl != null)
{
pnl.Location = new Point(1, 0);
}
}

How to create an object with name from string?

Without further elaboration, it seems like you're trying to create and then reference objects later using the name of the object. If this is the case, you can add a property to the object and then use that to find the object later:

public class myClass
{
public string name;
...
public myClass(string ObjectName)
{
name = ObjectName; //Construct the object with this name you've input
}
...
}

Then, you can store your objects in a collection of some sort:

...
List<myClass> myObjects = new List<myClass>();
myObjects.Add(new myClass("Bob")); //Creating an object we've named "Bob"
myObjects.Add(new myClass("Alice"));
...

And finally, when you need to find it later:

...
string nameToFind = "Bob";
myClass Bob = myObjects.Find(obj => obj.name == nameToFind); //Find "Bob"
...

Now you can use the Bob object as you'd like, notice this is NOT a solution to your problem the way you stated it. There is no way to make an object's name in code via a string the way you're asking (at least, as far as I know), but you can now reference the object that is named "Bob", what the source code calls it should be irrelevant (ie, you could change Bob to Person or Someone without any real change).

I didn't have time to make sure the code runs as is, but hopefully if my guess of your intentions is right, this will help you figure out what you need to do.

Get object values in loop by the property name c#

Below code might be helps you

StokItem item = new StokItem();
item.prop1 = 123;
item.prop2 = 456;
...

foreach (var prop in item.GetType().GetProperties())
{
//string propertyName = prop.Name;
//var propertyValue = prop.GetValue(item);
ScCommand.Parameters.AddWithValue ("@" + prop.Name, prop.GetValue(item));
}

How to get XAML element by name stored in a string variable?

If you have named elements in your XAML as follows:

<TextBlock x:Name="sometextblock" />

You can find them via the FindName method:

TextBlock txt = this.FindName("sometextblock") as TextBlock;

string elementName = txt.xyzproperty //do what you want with using txt.xyz property


Related Topics



Leave a reply



Submit