-1

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)

3
  • If you talk about doing this dynamically, why would you need that? Commented Apr 17, 2017 at 4:18
  • Possible duplicate of Create dynamic variable name Commented 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? Commented Apr 17, 2017 at 4:58

3 Answers 3

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.

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

1 Comment

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.
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.

1 Comment

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.
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();
        }
    }
}

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.