0
public static string filename2_1, filename2_2, filename2_3;
filename2_1="xyz.jpg";
filename2_2="abc.png";
filename2_3="qwe.jpg";

.....
...
for (int key = 1; key <= 3; key++)
{
        ....
foreach (var item in tabItem)
{
 item.ImagePath.Value = "images1/" + ("filename2_" + key);
 item.ThumbPath.Value = "thumbnails1/" + ("filename2_" + key);
}
}

As stated above I need to convert ("filename2_" + key) into actual variable. Can anyone help me regarding this

1
  • 2
    Have you considered using a dictionary? Commented Jan 9, 2012 at 19:49

2 Answers 2

10

You can't have dynamic variable names.

Variable names cannot be "created".

You can use an array or a generic collection to hold the collections of data you are using.

var fileSuffixList = new List<string>{ "xyz.jpg" , "abc.png", "qwe.jpg"};

foreach(string fileSuffix in fileSuffixList)
{
  ....
 foreach (var item in tabItem)
  {
    item.ImagePath.Value = "images1/" + ("filename2_" + fileSuffix);
    item.ThumbPath.Value = "thumbnails1/" + ("filename2_" + fileSuffix);
  } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide me with an example
3

As @Oded stated, you can't have dynamic variable names.

What you can do is use a Collection such as a dictionary:

Dictionary<string, int> dictionary = new Dictionary<string, string>();
dictionary.Add("filename2_" + key, "Value");

If your keys are always numeric, you can also use an array or a List. However, without more information, it's hard to tell you the best way to go about it.

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.