4

How can I add to an array which is in a foreach loop.

pseudo example

String[] mylist;

foreach ( ipadress ip in list )
{
    // I want to add to array ip.ToString();
}
// then put my list to a textbox
0

5 Answers 5

15

If you are using linq try this instead :

String[] mylist = list.Select(I => Convert.ToString(I.ip)).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

7

First of all, if this is a homework problem, it should really be tagged as such.

Anyway, assuming you have complete control of the string[] you are passing values to, and assuming your ipaddress class has .ToString() overloaded to give you back some intelligent information:

string[] myList = new string[list.Count];
int i = 0;
foreach (IPAddress ip in list)
{
    myList[i++] = ip.ToString();
}

Although I have to question why you are going back and forth between arrays and list objects to begin with.

6 Comments

i am figureing out cod to show my local ip address. I posted it down. Although inside the foreach i made an if (ip.AddressFamily.ToString() == "InterNetwork") that wil show me the ip, i realised that beeing new to c# don't know how to add array`s from loop. So i did that just for learning.
And don't laugh at "ip.AdressFamily.ToString()" :)) i did not knew to do this the hard way, and did not want to research
Oops. Thanks for the heads up. :)
@Ramhound - Have you tried compiling it? The assumption is that 'ipaddress' is a fully fleshed-out class, and that 'list' is a List<ipaddress> object. It compiles just fine on my machine.
|
2

Quick and short:

 String[] myList;

 List<int> intList = new List<int> { 1, 2, 3, 4 };

 myList = intList.ConvertAll<String>(p => p.ToString()).ToArray<String>();

Comments

-1

Figured it out...new to programming.., thanks to all. I used same code that tharindlaksh posted.

this is how it looks:

string[] all ;
int i = 0;

foreach (IPAddres ip in host.AddressList)
{
     all[i] = ip.ToString();
     i++;
}

textBoxMain.Text = all[0] + "\n" + all[1] + \n" + all[2] + "\n" + all[3];

2 Comments

This code will not compile. The correct class name is IPAddress. Its not clear the reason you are converting the contents of AddressList to a string.
Don't forget to initialize your string[]! You'll want to set its length to (at least) match the length of the data you are passing in, or you risk getting out of bound exceptions.
-2
int i=0;
String[] mylist;
foreach(ipaddress ip in list)
{
mylist[i]=ip.ToString();
i++
}

5 Comments

Code that compiles is possible here.
Thank you this is what i was looking for! I figured it out by myself tho.. I tought that if i give an int i =0; out the loop and a i++ in the loop, at each loop it wil give i the value 0 .... New to c# :D
I just wrote this.. i imageine that the class is not Predefined one.. The idea was that.. Sorry if this code is not working..
It wasn't clear from the question, but it turns out there is a predefined IPAddress class in System.Net. @Ramhound - Posting "This won't compile" and downvoting everything without explaining why it won't compile is not helpful to anyone.
That's right mate.. We are begginers of this field.. thank you for comment.. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.