1

My goal is to store the strings I got from my foreach loop into different variables. For example, I have the string Apple=3;Orange=10;Mango=12, I need to store Apple, Orange, and Mango to string sFruit1, sFruit2, sFruit3 (these strings need to increment also depending on how many items are there in the string like if 4, I need to have a sFruit4 variable also) after doing a foreach loop. This seems to be easy but I cannot get the right solution. Here is my code:

string sRes = "Apple=3;Orange=10;Mango=12";
string[] sSplitStrings = sRes.Split(';');

foreach (string sFruit in sSplitStrings)
{
    string sLabel = sFruit.Substring(0, sFruit.IndexOf("="));
}
Stefan
  • 652
  • 5
  • 19
Vina Chan
  • 179
  • 1
  • 1
  • 7
  • 2
    So how do you know it's always exactly three items? This sounds more like a job for a `Dictionary`. – Jeroen Mostert Oct 22 '19 at 10:18
  • Yeah you have a point, it could be just 1 or 2 items only. So the string will increment also like sFuit1, sFuit2, sFruit3 and so on depending on how many items are there in the string. – Vina Chan Oct 22 '19 at 10:20
  • 1
    The thing is that you can't have a variable number of... variables, the compiler needs to know up front what they are. If you declare four and you need five, too bad. This is why you would normally use a collection (like `Dictionary`, but also `List` and arrays), as these can store a variable number of items. `Dictionary` can index these by name, so it's just like a store that holds an arbitrary number of variables. – Jeroen Mostert Oct 22 '19 at 10:23
  • Apart from using Reflection your best bet would be creating a Dictionary and storing the Key as sFruit1->sFruitN and the Value as Apple=3... – demoncrate Oct 22 '19 at 10:25
  • @VinaChan are you talking about creating variable names dynamically? – Sushant Yelpale Oct 22 '19 at 10:27
  • Hi @Sushantyelpale, yes – Vina Chan Oct 22 '19 at 10:28
  • As c# is strongly typed, it is not possible. see this, https://stackoverflow.com/questions/20857773/create-dynamic-variable-name – Sushant Yelpale Oct 22 '19 at 10:28
  • Why not create a Fruit class and add each fruit + associated properties to a collection? Would be far easier than messing with string arrays. – Syntax Error Oct 22 '19 at 10:31
  • The simplest approach is to just use the string array you get from `string.Split()`. This contains all individual strings. – Stefan Oct 22 '19 at 11:11

2 Answers2

0

Create a list ouside the foreach loop and add an item into the list in the loop.

var labels = new List<string>();
foreach (string sFruit in sSplitStrings)
{
    labels.Add(sFruit.Substring(0, sFruit.IndexOf("=")));
}
0

it is better to use a dictionary, here is an example with it, and if you want to change, you can use list or any other class:

string sRes = "Apple=3;Orange=10;Mango=12";
string[] sSplitStrings = sRes.Split(';');
Dictionary<string,int> listsFruit = new Dictionary<string, int>();
foreach (string sFruit in sSplitStrings)
{
    string sLabel = sFruit.Substring(0, sFruit.IndexOf("="));
    string sValue = sFruit.Replace(sLabel + "=", "");
    listsFruit.Add(sLabel, int.Parse(sValue));
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ahmed Msaouri
  • 316
  • 1
  • 10