tags:

views:

62

answers:

4

how can i dissect or retrieve string values

Here's the sample code that im working on now

    private void SplitStrings()
    {
        List<string> listvalues = new List<string>();
        listvalues = (List<string>)Session["mylist"];
        string[] strvalues = listvalues.ToArray();
        for (int x = 0; x < strvalues.Length; x++)
        {

        }

    }

now that i'am able to retrieve List values in my session, how can i separately get the values of each list using foreach or for statement? what i want to happen is to programmatically split the values of the strings depending on how many is in the list.

A: 

I'm not sure what you're trying to obtain in this code, I don't know why you're converting your List to an Array.

You can loop through your listValues collection with a foreach block:

foreach(string value in listValues)
{
    //do something with value, I.e.
    Response.Write(value);
}
David Neale
A: 

If you have a list of string values, you can do the following:

private void SplitStrings()
{
    List<string> listValues = (List<string>) Session["mylist"];

    // always check session values for null
    if(listValues != null)
    {
        // go through each list item
        foreach(string stringElement in listValues)
        {
             // do something with variable 'stringElement'
             System.Console.WriteLine(stringElement);
        }
    }
}

Note that I test the result of casting the session and that I don't create a new list first-off, which is not necessary. Also note that I don't convert to an array, simply because looping a list is actually easier, or just as easy, as looping an array.

Note that you named your method SplitStrings, but we're not splitting anything. Did you mean to split something like "one;two;three;four" in a four-element list, based on the separator character?

Abel
A: 

I don't know what's in the strings but you can start by simplifying. There is no point allocating a new List if you're going to overwrite it immediately.

    private void SplitStrings()
    {
        List<string> list = (List<string>)Session["mylist"];

        foreach(string value in list)
        {
        }
    }
Gary
A: 
List listvalues = (List)Session["mylist"];
foreach (string s in listvalues)
{
  //do what you want with s here
}
kudor gyozo