I want to retrieve data from an ArrayList using c#
I want to retrieve object data from an ArrayList;
public class Form1
{
ArrayList list = new ArrayList();
private void OnSockMessage(object sender, SockEventArgs e)
{
Regex MyRegex = new Regex("^[<][A-Za-z]");
if (e.SockMsg != null)
{
string y = e.SockMsg.ToString();
if (MyRegex.IsMatch(y) == true)
{
rrr = y;
string ipdd = SocClient[e.SocketRef].Soc.RemoteEndPoint.ToString();
//serverkey seckey;
list.Add(new serverkey(ipdd,开发者_StackOverflow中文版rrr));
}
else
{
string curipadd = SocClient[e.SocketRef].Soc.RemoteEndPoint.ToString();
for (int i = 0; i < list.Count-1; i++)
{
//serverkey pk = list[i] as serverkey;
//string jj = list[i].ToString();
// serverkey pk = new serverkey(list[i].ToString());
/*********************************************
here i want to retrieve data from array list
*********************************************/
string ipadd;
if (curipadd == ipadd )
{
y = DecryptString(e.SockMsg, rrr);
listBox1.Items.Add(txtIP.Text + " <<" + y);
}
}
}
}
public class serverkey : Form1
{
string ipaddress;
string secertkey;
public serverkey(string IPAdd, string Seckey)
{
ipaddress = IPAdd;
secertkey = Seckey;
}
public string ip
{
get { return ipaddress; }
}
public string key
{
get { return secertkey; }
}
You'd be better off using a strongly typed generic List<serverkey>
and a foreach loop rather than a for loop. It'll be something like
List<serverkey> list = new List<serverkey>();
//add your items as you already are
foreach(var item in list)
{
item.ip ...// use item as a serverkey
}
Having said that, if you can you use a generic for some reason, use an 'as'
ArrayList list = new ArrayList();
//add your items as you already are
foreach(var item in list)
{
var sk = item as serverkey;
sk.ip ...// use item as a serverkey
}
精彩评论