Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Help Indexing ArrayList (http://www.programmingforums.org/showthread.php?t=15517)

csrocker101 Mar 31st, 2008 5:13 AM

Help Indexing ArrayList
 
Hey everyone I have made an arraylist that takes a stream of information from a string I have made that contains some IP addresses. However I am having a problem assigning the information correctly to my ArrayList. Each IP address is approxiamtely 13 characters long. Here is my code..

:

string IPAdressesofSystems = service.GetNearbySystems(host);
            ArrayList newList = new ArrayList();

            for (int i = 0; i < IPAdressesofSystems.Length; i++)
            {
                newList.Add(IPAdressesofSystems.Substring(i, 13));
            }


I am well aware it's because of the "i" index I am using in my Substring method is starting at the next incremented index. For example it starts out doing "index[0] = 150.542.234.23 index[1] = 45.543.245.21 index[2] = 4.342.233.29..etc.." because the i is incrementing by one which is making the starting index go up by one everytime. How would I make it so it records all 13 characters of Each IP adress?

Jabo Mar 31st, 2008 6:33 AM

Re: Help Indexing ArrayList
 
why would you not split your string into the array? I don't know how getnearbysystems populates the string, but it should separate the ip addresses somehow, and you can use that delimiter to split your string into usable array entries

Quote:

Originally Posted by csrocker101 (Post 143249)
:

string IPAdressesofSystems = service.GetNearbySystems(host);
            ArrayList newList = new ArrayList();

            for (int i = 0; i < IPAdressesofSystems.Length; i++)
            {
                newList.Add(IPAdressesofSystems.Substring(i, 13));
            }


I am well aware it's because of the "i" index I am using in my Substring method is starting at the next incremented index.

What this code does is what you said. If you use the string.split method to separate your string, it will look for whatever delimiter you specify.

I'm not familiar with C#, but in VB it would look something like this:
:

  1. Dim str1 As String = "xyz,pbb7,rx78"
  2. Dim str2() As String
  3. str2 = str1.Split(",")
  4. For x As Integer = 0 To str2.Length - 1
  5.         MsgBox(str2(x), MsgBoxStyle.Information, "Array")
  6. Next


This way, it don't matter what the size of each individual string is. The code I posted displays each array element in a new message box.

Jabo Mar 31st, 2008 7:25 AM

Re: Help Indexing ArrayList
 
I tried to convert this to C# but I don't know enough about it to really do it.

:

  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             string str1 = "xyz,pbb7,rx78";
  4.             string[] str2=new string[]{str1.Split",");
  5.             for (int p = 0; p < str2.Length; p++)
  6.             {
  7.                 label1.Text = str2[p]+"\n";
  8.             }
  9.         }


I get all kinds of errors about my strings and arrays, but maybe it'll help you out at least a little bit.

Kilo Mar 31st, 2008 7:03 PM

Re: Help Indexing ArrayList
 
Use this code with your variables.... remember to replace the comma in the split call with your delimiter.

:

  1.             string str1 = "xyz,pbb7,rx78";
  2.             string[] str2 = str1.Split(',');
  3.             for (int i = 0; i < str2.Length; i++)
  4.             {
  5.                 textbox1.Text = str2[i]+"\n";
  6.             }


Arla Apr 2nd, 2008 11:04 AM

Re: Help Indexing ArrayList
 
Or just use i=i+13 in your loop?

If you KNOW each IP is 13 characters, and you don't have a delimiter...


All times are GMT -5. The time now is 1:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC