How can I have access to an arraylist
-
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 -
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 ErikSince 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> :)
-
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 ErikYou have to cast it since it returns an object: (myServerArrayList[0] as myServerInfo).strServerName
-
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; }
-
You have to cast it since it returns an object: (myServerArrayList[0] as myServerInfo).strServerName
-
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 ErikArrays 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 ...
-
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
-
Hi, I tried MessageBox.Show((ServerInfo)(myServerArrayList[0]).strServerName); but it won't work:confused: 'Object has no definition for strServerName' Erik
-
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
-
Hi, I tried MessageBox.Show((ServerInfo)(myServerArrayList[0]).strServerName); but it won't work:confused: 'Object has no definition for strServerName' Erik