1

I am creating an array of string[] in my c# program to save location ("name","Position") of a bunch of elements. The problem is any time I had to introduce a new element I have to change the code at several places according to index of elements:

   string[] list = new string[4];
   list[0] = "[ELEMENT #1 NAME],[ELEMENT #1POSITION]";
   list[1] = "[ELEMENT #2 NAME],[ELEMENT #2POSITION]";
   list[2] = "[ELEMENT #3 NAME],[ELEMENT #3POSITION]";
   list[3] = "[ELEMENT #4 NAME],[ELEMENT #4POSITION]";

What I am looking for is something like an dynamic array so that I do not have to change the index location every time I introduce/ remove an element from list.

3
  • 2
    Why not use a class with properties Name, and Position, and then use Array or List of that class objects. Commented Feb 4, 2015 at 20:58
  • Use List<string> instead. You can build it dynamically, and if you need an array you can just call its .ToString() method. Commented Feb 4, 2015 at 20:59
  • 1
    Are the element names unique? Commented Feb 4, 2015 at 21:00

4 Answers 4

4

You can use List<string> as a dynamic array, it supports IEnumerable<string> for enumerating, or you can call LINQ and ToArray().

For example:

var list = new List<string>();

list.Add("[ELEMENT #1 NAME],[ELEMENT #1POSITION]");

string array[] = list.ToArray();

However, I'd actually recommend a dictionary in this case and not a list, a dictionary will let you store key-value pairs.

For example:

var dict = new Dictionary<string,int>();

dict["Element #1 Name"] = #Element #1 Position#;

Note that I've no real idea what type the position is, could be an int, a string or even a Point, but you get the idea.

You then don't need to bother with indices but refer to everything by name:

var el1_pos = dict["Element #1 Name"];
var el999_pos = dict["Element #999 Name"];
Sign up to request clarification or add additional context in comments.

Comments

1

You can use List<T> if you want a dynamically sized collection and don't bother with the index. And you should also create a type with two properties (Name and Position) and have a list of that type instead of storing them as string. It's easier to maintain, you don't have to parse the string every time you wanna get/set the Name or Position of a particular object.

Comments

1

Normally, you would just use a List<String> here. The Add method allows you to just add an element, no indexing required.

List<string> list = new List<string>();
list.Add("Test");

In your case, since you have "Name" and "Position" associated with each other, consider using a List<PositionedThing> (a custom class in other words) or a Dictionary<String, String> to store your mappings.

The class would look like:

public class PositionedThing
{
    public String Name {get; set;}
    public String Position {get; set;}
}

Comments

0

Try

List<string> list = new List<string>();
list.Add("[ELEMENT #1 NAME],[ELEMENT #1POSITION]")

Unless I've misunderstood your question that should be what you want

1 Comment

Thanks Lloyd, BradleyDotNET, Selman22, stakx & fallaciousreasoning , It solved the problem, Much appreciated.

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.