0
//Laver Array Liste
ArrayList Fields123 = new ArrayList();
Fields123.Add("12345");
Fields123.Add("67890");
Fields123.Add("09876");
Fields123.Add("54321");
Fields123.Add("12345");

//Som Variable
foreach (string tbOldField in Fields123)
{
    number = number + 1;

    string field + number = tbOldField;   
}

Dont know the syntax in C#. My problem is that I need the string field to be like:

String field1
String field2
String field3
String field4
String field5

but dont know how to get the "number" in so its is field(number).

2
  • 1
    Don't use ArrayList anymore. It belongs the old days when C# doesn't have Generics. You can use List<T> instead. In your case, Dictionary could be better.. Commented Aug 28, 2014 at 11:28
  • Not clear on what exactly you are asking. What output are you expecting? An array of key value pairs like "field1": 12345 and "field2": 67890? Is the field1 a variable name/key or is it simply a corresponding value? Commented Aug 28, 2014 at 11:37

1 Answer 1

2

Firstly do not use weakly typed ArrayList use type-safe generic collections such as List<T>.

Secondly, you can't construct a variable name by concatenating other variables like that.What you need is a Dictionary<string, string>

List<string> Fields123 = new List<string>();
Fields123.Add("12345");
Fields123.Add("67890");
Fields123.Add("09876");
Fields123.Add("54321");
Fields123.Add("12345");

Dictionary<string, string> values = new Dictionary<string, string>();
int number = 1;
foreach (string tbOldField in Fields123)
{
    values.Add("field" + number++, tbOldField);
}

Then you can get the values by using the corresponding key, for example: values["field1"]

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

1 Comment

Depending on what the values are being used for, it may be more appropriate to use Tuples rather than KVPs

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.