Working with Strings

shepmom

Member
Joined
Aug 18, 2010
Messages
9
Programming Experience
Beginner
I am trying to populate an array from a listbox.

My listbox items contain something like this:

1 for January
10 for February
3 for March

I have code in place and it was working, but I am taking the first character of the listbox item to populate the array. If the value of the listbox is 10 for February, I need to get 10, not just 1. Can I do something with the string that says I want to trim everything from the space to the end? Since they are all formatted the same way, if I could trim out anything from the space on, that would work.

Here's what I currently have:

ReDim m_adecRainfall(m_astrMonths.Length - 1)
For intCount = 0 To m_astrMonths.Length - 1
m_adecRainfall(intCount) = CDec(lstMontlyRainfall.Items(intStart).ToString.Substring(0, 1))
intStart += 1
Next
 
Here's what I would do, I would create a class that has 2 properties: A Single (or Integer if you want whole values only) to hold the rainfall value and a String to hold the month (or an enum for the month, either way would work) then I would Override the ToString() method in the class to return the rainfall amount, a space with the word "for", another space and the month. Then you can simply create the number of instances you need and just add them to the ListBox (which will make use of your ToString method to display the same text you have now) and to get the rainfall back, just use the rainfall property (you'll want to cast the ListBox.Item(whatever) to your class & then use the property)

That's the proper way I would do it, though if you're looking for a cheap & quick hack look at the IndexOf method of the string:
m_adecRainfall(intCount) = CDec(lstMontlyRainfall.Items(intCount).ToString.Substring(0, lstMontlyRainfall.Items(intCount).ToString.IndexOf(" ")))
 
Back
Top