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
Jason Aller
3,66028 gold badges43 silver badges40 bronze badges
asked Oct 17, 2012 at 16:27
0

5 Answers 5

15

If you are using linq try this instead :

String[] mylist = list.Select(I => Convert.ToString(I.ip)).ToArray();
answered Oct 17, 2012 at 16:31
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.

answered Oct 17, 2012 at 17:02

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>();
answered Oct 17, 2012 at 16:34

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];
answered Oct 17, 2012 at 17:16

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++
}
answered Oct 17, 2012 at 16:36

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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.