Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I have access to an arraylist

How can I have access to an arraylist

Scheduled Pinned Locked Moved C#
questionvisual-studiosysadminhelptutorial
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Schmullus

    Hi there, I have the following problem: I want to add a variable 'myServerInfo' to an arraylist. This variable is of the datatyp 'ServerInfo' (public class). I can add this variable without any problems to the arraylist with the command: myServerArrayList.Add(myServerInfo); But how can I have access to this through the arraylist? If I type in the following commandline, Intellisense will not provide me the necessary information: Console.WriteLine(myServerArrayList[0].??? I thought I can type in: Console.WriteLine(myServerArrayList[0].strServerName); Console.WriteLine(myServerArrayList[0].strServerIP); Console.WriteLine(myServerArrayList[0].strServerNetMask); My complete code for this example is like: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class ServerInfo { public string strServerName; public string strServerIP; public string strServerNetMask; } class Program { static void Main(string[] args) { ArrayList myServerArrayList = new ArrayList(); ServerInfo myServerInfo = new ServerInfo(); myServerInfo.strServerName = "Server 1"; myServerInfo.strServerIP = "192.168.0.1"; myServerInfo.strServerNetMask = "255.255.255.0"; // Add to ArrayList myServerArrayList.Add(myServerInfo); Console.WriteLine(myServerArrayList[0].??? } } } If anybody gives me a hint, I can go on with my project.:) Thanks in advance Erik

    G Offline
    G Offline
    Guffa
    wrote on last edited by
    #2

    What you get from the ArrayList is a reference to the type Object. You have to cast the reference to the actual data type: (ServerInfo)(myServerArrayList[0])

    --- b { font-weight: normal; }

    S 1 Reply Last reply
    0
    • S Schmullus

      Hi there, I have the following problem: I want to add a variable 'myServerInfo' to an arraylist. This variable is of the datatyp 'ServerInfo' (public class). I can add this variable without any problems to the arraylist with the command: myServerArrayList.Add(myServerInfo); But how can I have access to this through the arraylist? If I type in the following commandline, Intellisense will not provide me the necessary information: Console.WriteLine(myServerArrayList[0].??? I thought I can type in: Console.WriteLine(myServerArrayList[0].strServerName); Console.WriteLine(myServerArrayList[0].strServerIP); Console.WriteLine(myServerArrayList[0].strServerNetMask); My complete code for this example is like: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class ServerInfo { public string strServerName; public string strServerIP; public string strServerNetMask; } class Program { static void Main(string[] args) { ArrayList myServerArrayList = new ArrayList(); ServerInfo myServerInfo = new ServerInfo(); myServerInfo.strServerName = "Server 1"; myServerInfo.strServerIP = "192.168.0.1"; myServerInfo.strServerNetMask = "255.255.255.0"; // Add to ArrayList myServerArrayList.Add(myServerInfo); Console.WriteLine(myServerArrayList[0].??? } } } If anybody gives me a hint, I can go on with my project.:) Thanks in advance Erik

      E Offline
      E Offline
      ejuanpp
      wrote on last edited by
      #3

      Since anything derived from object can be stored in your ArrayList, Intellisense does not "know" what kind of object is stored in the 0th position. You need to cast it before: ((ServerInfo)myServerArrayList[0]). ... or use something more appropriate than ArrayList. Try List<ServerInfo> :)

      1 Reply Last reply
      0
      • S Schmullus

        Hi there, I have the following problem: I want to add a variable 'myServerInfo' to an arraylist. This variable is of the datatyp 'ServerInfo' (public class). I can add this variable without any problems to the arraylist with the command: myServerArrayList.Add(myServerInfo); But how can I have access to this through the arraylist? If I type in the following commandline, Intellisense will not provide me the necessary information: Console.WriteLine(myServerArrayList[0].??? I thought I can type in: Console.WriteLine(myServerArrayList[0].strServerName); Console.WriteLine(myServerArrayList[0].strServerIP); Console.WriteLine(myServerArrayList[0].strServerNetMask); My complete code for this example is like: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class ServerInfo { public string strServerName; public string strServerIP; public string strServerNetMask; } class Program { static void Main(string[] args) { ArrayList myServerArrayList = new ArrayList(); ServerInfo myServerInfo = new ServerInfo(); myServerInfo.strServerName = "Server 1"; myServerInfo.strServerIP = "192.168.0.1"; myServerInfo.strServerNetMask = "255.255.255.0"; // Add to ArrayList myServerArrayList.Add(myServerInfo); Console.WriteLine(myServerArrayList[0].??? } } } If anybody gives me a hint, I can go on with my project.:) Thanks in advance Erik

        G Offline
        G Offline
        George L Jackson
        wrote on last edited by
        #4

        You have to cast it since it returns an object: (myServerArrayList[0] as myServerInfo).strServerName

        S 1 Reply Last reply
        0
        • G Guffa

          What you get from the ArrayList is a reference to the type Object. You have to cast the reference to the actual data type: (ServerInfo)(myServerArrayList[0])

          --- b { font-weight: normal; }

          S Offline
          S Offline
          Schmullus
          wrote on last edited by
          #5

          Hi, I tried MessageBox.Show((ServerInfo)(myServerArrayList[0]).strServerName); but it won't work:confused: 'Object has no definition for strServerName' Erik

          E G 2 Replies Last reply
          0
          • G George L Jackson

            You have to cast it since it returns an object: (myServerArrayList[0] as myServerInfo).strServerName

            S Offline
            S Offline
            Schmullus
            wrote on last edited by
            #6

            This works perfect! Thanks a lot:-D

            R 1 Reply Last reply
            0
            • S Schmullus

              Hi there, I have the following problem: I want to add a variable 'myServerInfo' to an arraylist. This variable is of the datatyp 'ServerInfo' (public class). I can add this variable without any problems to the arraylist with the command: myServerArrayList.Add(myServerInfo); But how can I have access to this through the arraylist? If I type in the following commandline, Intellisense will not provide me the necessary information: Console.WriteLine(myServerArrayList[0].??? I thought I can type in: Console.WriteLine(myServerArrayList[0].strServerName); Console.WriteLine(myServerArrayList[0].strServerIP); Console.WriteLine(myServerArrayList[0].strServerNetMask); My complete code for this example is like: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { public class ServerInfo { public string strServerName; public string strServerIP; public string strServerNetMask; } class Program { static void Main(string[] args) { ArrayList myServerArrayList = new ArrayList(); ServerInfo myServerInfo = new ServerInfo(); myServerInfo.strServerName = "Server 1"; myServerInfo.strServerIP = "192.168.0.1"; myServerInfo.strServerNetMask = "255.255.255.0"; // Add to ArrayList myServerArrayList.Add(myServerInfo); Console.WriteLine(myServerArrayList[0].??? } } } If anybody gives me a hint, I can go on with my project.:) Thanks in advance Erik

              B Offline
              B Offline
              bes2005
              wrote on last edited by
              #7

              Arrays store System.Object type. When you add a new object or an item of promotive type they will be upcasted to System.Object. This is the line inside ArrayList which adds the new item: public virtual int Add(object value); Use Code DEfinition Window in VS 2005 to see what's inside ArrayList class. In order to access properties you need to down cast the array item you want to acces like: Console.WriteLine(((ServerInfo)myServerArrayList[0]).PropertyName); You would have the same problem if you tried: ArrayList myList = new ArrayList(); int int1 = 1; myList.Add(int1); int int2 = 2; myList.Add(int2); int int3 = myList[0]; // oops error - cannot convert type 'object' to 'int' int int3 = (int)myList[0]; // this will do the trick Happy coding ...

              1 Reply Last reply
              0
              • S Schmullus

                This works perfect! Thanks a lot:-D

                R Offline
                R Offline
                Russell Jones
                wrote on last edited by
                #8

                make sure you only have serverinfo in that arraylist though item as ServerInfo will return null if item is not a serverinfo, this will create a runtime error. If you can and you are using 2005 i would try to use one of the generic collection types Collection or List as someone else suggested later. That's more likely to show errors at compile time. HTH Russ

                S 1 Reply Last reply
                0
                • S Schmullus

                  Hi, I tried MessageBox.Show((ServerInfo)(myServerArrayList[0]).strServerName); but it won't work:confused: 'Object has no definition for strServerName' Erik

                  E Offline
                  E Offline
                  ejuanpp
                  wrote on last edited by
                  #9

                  In (ServerInfo)(myServerArrayList[0]).strServerName ... It won't if the cast is applied after having attempted to retrieve the strServerName in (myServerArrayList[0]) which is recognized as an object type instance.

                  1 Reply Last reply
                  0
                  • R Russell Jones

                    make sure you only have serverinfo in that arraylist though item as ServerInfo will return null if item is not a serverinfo, this will create a runtime error. If you can and you are using 2005 i would try to use one of the generic collection types Collection or List as someone else suggested later. That's more likely to show errors at compile time. HTH Russ

                    S Offline
                    S Offline
                    Schmullus
                    wrote on last edited by
                    #10

                    Thanks a lot for this detailed information!!

                    1 Reply Last reply
                    0
                    • S Schmullus

                      Hi, I tried MessageBox.Show((ServerInfo)(myServerArrayList[0]).strServerName); but it won't work:confused: 'Object has no definition for strServerName' Erik

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #11

                      Add parentheses so that you access the property of the ServerInfo object, not the Object object. MessageBox.Show(((ServerInfo)(myServerArrayList[0])).strServerName);

                      --- b { font-weight: normal; }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups