Reputation: 1273
Ok, I am not sure how to do this as I am trying to teach myself C# and create a program for work at the same time.
I am creating a Windows Form C# project, I have a text with a list of anywhere from 1 IP address to thousands of IP address in it. When I click the submit button on the form I want it to create a list from the contents of the message box one line at a time.
I also would like to Parse the list to IP address arrays using the System.Net IPAddress Class into an array for each IP address.
I get no errors when I use List list = new List(textBox1.Lines); with the below code:
private void Submit_Button_Click(object sender, EventArgs e)
{
// //Pass Text of TextBox1 to String Array tempStr
List<string> list = new List<string>(textBox1.Lines);
// // Loop through the array and send the contents of the array to debug window.
// for (int counter=0; counter < list.Count; counter++)
// {
// System.Diagnostics.Debug.WriteLine(list[counter]);
// }
this.Hide();
Form2 f2 = new Form2(list);
f2.Show();
}
However if I try to parse the list here using the IPAddress.Parse it raises a number of errors. List list = new List(IPAddress.Parse(textBox1.Lines));
I was under the impression that the end product of IPAddress.Parse(textBox1.Lines) would be a 4 string array of 3 bytes each, so wouldn't a string array work?
private void Submit_Button_Click(object sender, EventArgs e)
{
// //Pass Text of TextBox1 to String Array tempStr
List<string[]> list = new List<string[]>(IPAddress.Parse(textBox1.Lines));
// // Loop through the array and send the contents of the array to debug window.
// for (int counter=0; counter < list.Count; counter++)
// {
// System.Diagnostics.Debug.WriteLine(list[counter]);
// }
this.Hide();
Form2 f2 = new Form2(list);
f2.Show();
}
Then I try a different kind of variable for my list and it doesn't work then either. List list = new List(textBox1.Lines);
get these errors. 1. The best overloaded method match for 'System.Collections.Generic.List.List(System.Collections.Generic.IEnumerable)' has some invalid arguments 2. Argument1: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable'
private void Submit_Button_Click(object sender, EventArgs e)
{
// //Pass Text of TextBox1 to String Array tempStr
List<IPAddress> list = new List<IPAddress>(textBox1.Lines);
// // Loop through the array and send the contents of the array to debug window.
// for (int counter=0; counter < list.Count; counter++)
// {
// System.Diagnostics.Debug.WriteLine(list[counter]);
// }
this.Hide();
Form2 f2 = new Form2(list);
f2.Show();
}
I cannot for the life of me figure out how to convert this string[] textBox1.Lines to an IPAddress for my purposes.
Please help.
Upvotes: 1
Views: 1607
Reputation: 157136
Create a list, iterate over the array, and parse the IP addresses one by one:
List<IPAddress> addresses = new List<IPAddress>();
foreach (string input in this.textBox1.Lines)
{
IPAddress ip;
if (IPAddress.TryParse(input, out ip))
{
addresses.Add(ip);
}
else
{
Console.WriteLine("Input malformed: {0}", input);
}
}
Upvotes: 1