How can I store each element in a string array Joe, Ben, Carl
into separate variables so that GuestA
is Joe, GuestB
is Ben, and GuestC
is Carl without using a dictionary? (Notice that the variables are going in alphabetical order)
Asked
Active
Viewed 93 times
-2

ComputersAreCool
- 87
- 1
- 13
-
If you talk about doing this dynamically, why would you need that? – Yeldar Kurmangaliyev Apr 17 '17 at 04:18
-
Possible duplicate of [Create dynamic variable name](http://stackoverflow.com/questions/20857773/create-dynamic-variable-name) – 41686d6564 stands w. Palestine Apr 17 '17 at 04:30
-
Assuming that John Wu's answer isn't what you're looking for, and that you want the variables to be automatically created, what perceived benefit does that have over using a dictionary? How would you access such variable even if you could create them? – ProgrammingLlama Apr 17 '17 at 04:58
3 Answers
2
Not sure why you'd want to do this, but this code answers your question if taken literally.
string[] list = new string[] {"Joe","Ben","Carl"};
string GuestA = list[0];
string GuestB = list[1];
string GuestC = list[2];
Something tells me there are additional requirements that you are having trouble articulating.

John Wu
- 50,556
- 8
- 44
- 80
-
I think what the OP meant is to do this dynamically. E.g. generate 26 variables with names `GuestA` to `GuestZ`. However, I'm not sure for what possible reason that would be a requirement. – 41686d6564 stands w. Palestine Apr 17 '17 at 04:25
0
Assuming you have GuestA
, GuestB
, and GuestC
already declared, you'd use Reflection like this:
private string GuestA;
private string GuestB;
private string GuestC;
private void button1_Click(object sender, EventArgs e)
{
string variableName;
string[] values = {"Joe", "Ben", "Carl" };
for(int i = 0; i < values.Length; i++)
{
variableName = "Guest" + Convert.ToChar(65 + i).ToString();
System.Reflection.FieldInfo fi = this.GetType().GetField(variableName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (fi != null)
{
fi.SetValue(this, values[i]);
}
}
}
Though I suspect this isn't quite what you've got since you used the word "Generate" in your title.

Idle_Mind
- 38,363
- 3
- 29
- 40
-
I think yours is close enough. In case anyone was wondering, I wanted to generate separate variables for each array element because I was trying to create a machine learning API that would generate a variable for each hidden layer that the user wanted to add. I hope this would clear up confusion. – ComputersAreCool Apr 17 '17 at 17:37
0
You can't use string like refrence type, but u can simulate reference behavoir by operators overloading, but in this case appears one big problem: inability for overloading assignment operator, in code below its bypasses through property-like style. But i think you can try doublQe assignment like Guest guestD = tempGuest = "Joe Ho";
(not sure about this).
public class ReferencedStringExample
{
public void Wrong()
{
string GuestA = "Joe",
GuestB = "Ben",
GuestC = "Carl";
var array = new string[]
{
GuestA,
GuestB,
GuestC
};
GuestA = "Joe Ho";
Debug.Assert(GuestA == array[0]);
}
public void Right()
{
Guest GuestA = "Joe",
GuestB = "Ben",
GuestC = "Carl";
var array = new Guest[]
{
GuestA,
GuestB,
GuestC
};
GuestA.Val("Joe Ho");
Debug.Assert(GuestA == array[0]);
Debug.Assert(GuestA == "Joe Ho");
GuestA = "Joe Ho";
Debug.Assert(GuestA != array[0]);
Debug.Assert(GuestA == "Joe Ho");
}
public class Guest
{
string value;
public static implicit operator string(Guest g)
{
return g.value;
}
public static implicit operator Guest(string s)
{
return new Guest() { value = s };
}
public Guest Val(string s)
{
this.value = s;
return this;
}
public override bool Equals(object obj)
{
Guest guest = obj as Guest;
return guest.value == this.value;
}
public override int GetHashCode()
{
return (value ?? string.Empty).GetHashCode();
}
}
}