0

This is my Code

Public List<string> Values{get;set;}

In the above list contains multiple values . For Example ["10","20","30"] . I want to create three variables with list values(a="10",b="20",c="30") in c#.net . If List Count is Zero No need to Create Variables.

6
  • 2
    You need to think of your design. Why do you need variable creation for each value (just curious)? Commented Aug 17, 2016 at 7:32
  • what does this have to do with MVC? And most importantly...variables doesn't say much here if there's no context? Are we talking about JS variables or adding private members to a class or local variables inside a method? Commented Aug 17, 2016 at 7:32
  • Yes me too thought the same @HariPrasad.. Commented Aug 17, 2016 at 7:33
  • Well you could do some convoluted code to define say a-z and assign to a-> whatever is relevant, and then check what you used and didnt.. but why would you need to do that? Commented Aug 17, 2016 at 7:43
  • 2
    Dictionary<string,string> ? Commented Aug 17, 2016 at 7:46

2 Answers 2

1

You can try using ExpandoObject:

  using System.Dynamic;

  ...

  List<string> Values = new List<string>() {
    "10", "20", "30"
  };

  ...

  dynamic variables = new ExpandoObject();

  for (int i = 0; i < Values.Count; ++i)
    (variables as IDictionary<String, Object>).Add(
       ((char) ('a' + i)).ToString(), 
       Values[i]);

  ...

  // 10
  Console.Write(variables.a);
Sign up to request clarification or add additional context in comments.

Comments

0

I hope this will help you.

if (Values.Count != 0){   
    Dictionary<string, string> dictionary =
            new Dictionary<string, string>();
    char key = 'a';
    for (i = 0; i < Values.Count; i++){
        dictionary.Add(key, Values[i]);
        key = (key == 'z'? 'a': (char)(input + 1));
    }
}

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.