how do i convert a string into an arraylint?

hi..

i have an array list called students.. i need to add all students in my database into this arraylist.. thats easy but the problem is i need each student to be an arraylist (jagged arraylist)... is that possible ! i tried to do it like this but it didnt work..

if (socketData.clientID == reader.ReadElementContentAsString())
classes.Add((ArrayList)socketData.clientID); <=== "Cannot convert type string to arraylist"


im open for ideas..

peace.



Answer this question

how do i convert a string into an arraylint?

  • swapna_n

    what you said was helpful.. but not what i need to know..

    i need to use the class_name retrieved from the data base to declare an arraylist object within the calsses array list...

    thnx


  • Starck

    Im sorry maybe i wasnt clear...

    what i need to do exactly is this..

    server starts.. server goes to database... retrieve all classes..

    insert each class into the "classes" arraylist.. BUT i need each class added to the array list to be an arraylist object so i can add the teacher and the students to this class later.

    sorry again if i was unclear or my question was missleading.

    in other words.. can i use the class name retrieved from the database to create an arraylist object using the class name !
    example:
    class names in database are -> math, physics, sience, etc...
    "classes" arraylist should contain- > (arraylist)math, (arraylist)physics, etc...
    but the convertion here cannot be done this simple.. cannot convert from string to arraylist.



    thnx




  • Kmartin

    Perhaps you could use .NET 2.0 and use generic collection classes. Namely a Dictionary<String, List<String>> but to be honest your design needs to be reworked entirely. You want some collection classes and some objects representing these entities and methods on them. From a performance perspective it might not be best to load everything into memory but that's up to you.

    I still recommend making collection classes that extend generic classes and not trying to construct these massive jagged collections in one go.



  • OptikConnex

    Not sure what you're trying to do... "use the class name retrieved from the database to create an arraylist object using the class name"

    Where do you want to use the class name

    Andrej



  • Michael A Thomas

    Hi,

    you can add values to your row using something like:

    classes.Add(new Object[] {socketData.clientID, socketData.clientName, socketData.clientLastName, ...});

    Just remember - the number of items you're adding in your curly brackets should match the number of columns in your data row. Also, the column order should be the same...

    Andrej



  • Jim Perry

    what im trying to do is use the class name to declare an arraylist object..

    arralylist "class name" = new arraylist();

    or

    arraylist classes = new arraylist();

    classes.add((arraylist)classname);

    is this possible


  • karthik.sr

    Perhaps you need to use a Hashtable. This will allow you to assocate a string (e.g., "maths") with an arraylist.

    Hashtable myHT = new Hashtable();
    myHT.Add("maths", new arraylist());
    myHT.Add("physics", new arraylist());


  • Syri

    Something like this

    ArrayList classes = new ArrayList();

    ArrayList myClass = new ArrayList();

    myClass.AddRange(new object[] { socketData.clientID, socketData.clientName });

    classes.Add(myClass);

    Andrej



  • Bluehunter

    Better performance might be yielded by using the ArrayList constructor that taked an ICollection instance as a parameter, this will automatically copy all the elements to the ArrayList. After construction the AddRange method provides better performance for adding multiple items at one time.

    That aside, as I understand the problem. You want to have a list of students and each student have a list of classess.

    For one Student itself should be its own class not just an array list. Student should expose properties such as name and student ID, etc, but should contain a list of classes.

    To populate this all you would read all students into a list. Iterate over this temporary list and for each student name go back to the DB and get all their classes and put that into a second list. Associate the student's info and classes in a new Student object which you add to the final list. I'd throw up some idea code but from your code I really have no idea how you're getting your data. It looks like there are sockets and XmlReaders involved.



  • steveareno

    Perhaps you could wrap the arralist in another class:

    class ClassInfo

    {

    public string Id;

    public ArrayList ClassInfoList;

    }

    From there, you can do things like:

    ClassInfo info = new ClassInfo();

    info.Id = "class1";

    info.ClassInfoList = new ArrayList();

    ClassInfo subinfo = new ClassInfo();

    subinfo.Id = "class1student1";

    info.ClassInfoList.Add(subinfo);

    I'm not sure if this is what you want, just guessing.


  • GWILSON1981

    this was very helpful.. but after i add the myClass to the classes arraylist.. how do i get to "socketData.clientID"

    thnx


  • SrinivasParimi

    osamaT wrote:
    arralylist "class name" = new arraylist();

    "class name" here is just a variable name. Variable name is defined in your editor, it doesn't mean anything at runtime.

    Your socketData.clientID... This is a string, let's say "math"... How would you want this string to be converted into the array

    Andrej



  • Constantijn Enders

    This code works to convert a string to arraylist:

    private void button1_Click(object sender, EventArgs e)

    {

    char[] separator = { ',' };

    string strResult = ("1, 12, 44");

    string[] temp = strResult.Split(separator);

    ArrayList myAL = new ArrayList();

    foreach (string s in strResult.Split(separator))

    {

    myAL.Add(s);

    }

    Console.WriteLine("{0}", temp[0]);

    Console.WriteLine("{0}", myAL[0].ToString());

    Console.WriteLine("{0}", temp[1]);

    Console.WriteLine("{0}", myAL[1].ToString());

    Console.WriteLine("{0}", temp[2]);

    Console.WriteLine("{0}", myAL[2].ToString());

    }


  • how do i convert a string into an arraylint?