0

I've recently started working on a project in C# that requires the user to create as many instances of a class as they'd like. I've thought about two different ways to go about this.

1: Use a list of string arrays that contains the information held by the class. By this I mean when they create an instance of the class the data goes to a list so that I can re-use the same variable name.

// Initiating the class
dataSet1D currDataSet = new dataSet1D();
dataSet1D.name = txtName.Text;
dataSet1.length = Int32.Parse(txtIndex.Text);

// Creating the list of arrays
List<string[]> arrayList = new List<string[]>();
string[] strArray = new string[2];
strArray[0] = "name";
strArray[1] = "lengthAsString";
arrayList.Add(strArray);

(That was just some quick mock-up code, I'll obviously use better variable names on my actual project)

2: I could use dynamically named variables If they enter what they want as a name, I can name the instance that.

dataSet1D <dynamically named variable name> = new dataSet1D();
5
  • Not sure what you are asking here. Are you asking how to create a bunch of classes and then retrieve them by some user specified name? Commented Aug 10, 2016 at 23:52
  • "dynamically named variables" is not a thing in C#. How would you write code, at compile-time, that refers to these variables, which are named at runtime? Commented Aug 10, 2016 at 23:53
  • 6
    Instead, you should look up the Dictionary<TKey, TValue> class. Commented Aug 10, 2016 at 23:53
  • I can't imagine why would someone need dynamically named variables.. This sounds like you are doing something very wrong if this is what you are looking for. Can you expand a bit on your problem? Commented Aug 10, 2016 at 23:55
  • Welcome to Stack Overflow Xander! Have a look through @Blorgbeard's edits, and see what could be improved in future questions. Thanks/Taglines are discouraged as they clutter the post, as well as irrelevant information that don't pertain to the question (Things like "I'm new at this"). Commented Aug 10, 2016 at 23:59

3 Answers 3

2

You can creat a dictionary which maps the name of the "variable" to the actual calss instance, or you can create a List which has all your instances and they have an index in this case rather than a name.

Sign up to request clarification or add additional context in comments.

Comments

0

If you need lots of instances of 1 class, you can just have a list of that class. Like this:

class MyClass
{
   public string Name {set;get;}
   // property2 , etc
}

var myList = new List<MyClass>();
myList.Add(new MyClass());

Other than this, I have to mention that variable names has nothing to do with the run-time. when you build your code, they will all change to pointers to space that contains the data.

Variable name is only a handler for programmers to let them work with their objects

Comments

0

You could create add a string property to the class which would hold the name the user specifies for each object. Then, as a new instance of each object is created, add it to a List as others have suggested. Finally when you need to access an instance by the user defined name, use a LINQ expression to retrieve it.

public class MyClass
{
    public string UserDefinedName { get; set; }
    public int Length { get; set; }
    ... Other member variables
}

public List<MyClass> UserCreatedObjects { get; set;}

Then to get an instance which was named, say "my_object_1" use LINQ:

var theInstance = UserCreatedObjects.Where(o => o.UserDefinedName == "my_object_1").FirstOrDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.