I am trying to convert a listbox to an array so i got this line
var modarray = listBox1.Items.Cast<String>().ToArray();
but then i also need to use an int array
so i tried the following
int[] arr = modarray.Cast<int>().ToArray();
but i get an error that suggests that is not possible to convert the array. Can anybody helpme please

Recommended Answers

You'll need to convert them one by one, you can't cast strings into int. So use Convert.ToInt32, Int32.Parse or Int32.TryParse

Jump to Post

All 3 Replies

You'll need to convert them one by one, you can't cast strings into int. So use Convert.ToInt32, Int32.Parse or Int32.TryParse

so you are telling me that i sould convert every item inside first? i just get confused a bit . or i can just change the
int[] arr = Convert.toInt32(modarray.Cast<string>().ToArray());

There is no built in way to convert an entire array of strings to int, so you'll have to do it yourself

int[] myInts = listbox1.Items.Select(p => Int32.Parse(p)).ToArray();

This assumes your listbox1 items are all strings to start with. This hasn't been tested, so syntax might be off :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.