Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] C# class help?

Started by PythonRulez Feb 14, 2007 at 5:18 AM 3 replies 900+ views
Original Post
PythonRulez
PythonRulez
I would like to create a new instance of a class, but I would like the user to tell me the name of the object they want to create, and the code uses that (stored as a string) name and creates the object. This is done as a method. This is what I thought about, but it creates it called the name of the variable: public void createTextBox(string textBoxName) { // Create a new text box TextBox = textBoxName new TextBox; } P.S: Is this in the right forum? I put it here because it isn't game programming.
benryves
benryves
You can set the Name property;
public TextBox CreateTextBox(string textBoxName){    // Create a new text box     TextBox T = new TextBox();    T.Name = textBoxName;    return T;}

I don't think that's what you want to do, though. Do you mean something like this (pseudo-code, wouldn't work):
CreateTextBox("SomeTextBox");SomeTextBox.Text = "Hooray";this.Controls.Add(SomeTextBox);

The only way I can think of doing something like this would be to do this:
private Dictionary<string, TextBox> TextBoxLookup = new Dictionary<string, TextBox>();public void CreateTextBox(string textBoxName){    // Create a new text box     TextBox T = new TextBox();    T.Name = textBoxName;    // Add it to the dictionary    TextBoxLookup.Add(textBoxName, T);}

...which could be used as:
CreateTextBox("SomeTextBox");TextBoxLookup["SomeTextBox"].Text = "Hooray";this.Controls.Add(TextBoxLookup["SomeTextBox"]);

What are you ultimately trying to achieve? (The .NET forum is usually the best place to put C# questions, by the way).
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
dalep
dalep
You mean you want to specify the type of the object as a parameter?

That goes something like this:

using System.Reflection; // for ConstructorInfopublic object MakeMeAnObject(string typename){    System.Type type = Type.GetType(typename);    ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);    return ctor.Invoke(new object[0]);        }


That will only work for types that have a no-arg constructor. Its a little more complicated if you need more control than that. But all Controls use a no-arg so as long as that's what you're making it should be fine.

The Name property of a Control doesn't really mean anything at runtime - its mostly there just to support the Forms designer.
Sneftel
Sneftel
This is more appropriate for the .NET forum. I'll move it there.
TheTroll
TheTroll
You can not directly have a user choose the name of a variable, that is done at the time of coding. Now you can create a map that names what object you want.

What you can do is create a hashtable that containes the objects you are creating and a name that matches to them.

theTroll

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.