C# Reading Data into Static Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    C# Reading Data into Static Array

    I have this code on load:
    Code:
    Reader = command.ExecuteReader();
    Reader.Read();
    while (Reader.NextResult)
    {
        session.gameList = Reader.GetValue(0).ToString();
    }
    I'm trying to read it into a static array defined in this class called session:
    Code:
    class session
    {
            private static string[] gamelist;
    
            public static string[] gameList
            {
                get { return gamelist; }
                set { gamelist = value; }
            }
    }
    Can't get it to work, i'm thinking i may need to loop it somewhere but unsure as where!?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    "I can't get it to work" is a bit vague. Do you get an error message? Exception not handled? Results just different than what you expect?

    session.gameLis t = Reader.GetValue (0).ToString();

    What is it you think this line is going to do?
    Reason I ask is because you keep setting it's value to something new.
    Notice I didn't say "adding to its value", or setting the value of one if its elements.

    You have a string array called gamelist. You need to access the elements of the array. For example...
    sesson.gameList[nIndex] = Reader.GetValue (0).ToString();

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by tlhintoq
      You have a string array called gamelist. You need to access the elements of the array. For example...
      sesson.gameList[nIndex] = Reader.GetValue (0).ToString();
      I think a better object to use would be a List or an Array so that you don't have to predefine the length.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well I would say it throws the error, cannot convert string to string[]
        Since you are trying to do this:
        session.gameLis t = Reader.GetValue (0).ToString();

        where session.gameLis t is a string[] and Reader.GetValue (0).ToString() is just a string. And you do not supply an index into the string[].

        It *is* possible to do this with a straight static array, but it would be just as easy to use a List or other collection object as mentioned. then if need be you can just use the .ToArray() function of the collection to get a string[]

        Comment

        Working...