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)
-
If you talk about doing this dynamically, why would you need that?Yeldar Kurmangaliyev– Yeldar Kurmangaliyev2017-04-17 04:18:38 +00:00Commented Apr 17, 2017 at 4:18
-
Possible duplicate of Create dynamic variable name41686d6564– 41686d65642017-04-17 04:30:41 +00:00Commented Apr 17, 2017 at 4: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– ProgrammingLlama2017-04-17 04:58:36 +00:00Commented Apr 17, 2017 at 4:58
3 Answers
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.
1 Comment
GuestA to GuestZ. However, I'm not sure for what possible reason that would be a requirement.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.
1 Comment
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();
}
}
}