-
[RESOLVED] Converting String to Integer
Hello, i have a question... If i have a ListView and i have a subitem with the text 140.00, what can i do to make that string an integer?
Right, now this is my code:
Code:
Dim v As String = "50"
Dim val As Integer = Convert.ToInt32(v)
But, i still can't manage to multiply val, it gives me an error...
-
Re: Converting String to Integer
This seems to work OK
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim v As String = "50"
Dim val As Integer = Convert.ToInt32(v)
Dim ival As Integer = 3 * val
MessageBox.Show(ival.ToString)
End Sub
Quote:
Originally Posted by
tassa
it gives me an error...
That statement, by itself, always means the same thing....nothing. If you are getting an error, we need to know what the error is.
-
Re: Converting String to Integer
You can do that, but a safer solution is to use the TryParse method. This method returns a boolean value letting you know if the parsing was successful or not so that you can proceed accordingly.
Code:
Dim v As String = "50"
Dim val As Integer
If Integer.TryParse(v, val) Then
'It succeeded. Do whatever needs to be done next.
Else
'It failed. Perform desired action to correct the problem here.
End If
-
Re: Converting String to Integer
deciding which one to use depends on the confidence you have in the data. if you are 101% certain that the string will always be a number, then convert.to is fine, otherwise use tryparse.
btw -
Code:
Dim v As String = "50"
Dim val As Integer = Convert.ToInt32(v) 'this will work
val += 1
v = "5.1"
val = Convert.ToInt32(v) 'this won't work
val += 1
-
Re: Converting String to Integer
since Val is a function in VB.... I'd use a different variable name....
-tg
-
Re: Converting String to Integer
Since this is a Listview, converting might well be fine. It depends on whether or not the user is able to type random junk into the cell, or not. Usually, Listviews are used for display rather than editing, so conversion ought to be ok.
We still need to know the error message to give any meaningful suggestions.
-
Re: Converting String to Integer
Quote:
Originally Posted by
techgnome
since Val is a function in VB.... I'd use a different variable name....
-tg
Good point. The OP mentioned multiplying it, and something like this:
Dim n = val * 5
would be problematic because Val needs an argument.
-
Re: Converting String to Integer
i agree about val, and was going to say something and then totally forgot.
-
Re: Converting String to Integer
So... That worked (THANKS A LOT!), but i have another problem regarding that + a ListView (it has 5 columns [index count = 4])...
I'm using this code to input the product of the multiplication of both values, but it gives me this error:
InvalidArgument=Value of 4' is not valid for 'index'. Parameter name: index
And i don't know why it says that index 4 is invalid if i have 5 columns... This is the code line for adding the product into the Rth row and the 4th-indexed column...
Code:
'Adds the numeric value of Combobox2 to the Rth row and the 2 indexed column
Me.ListView1.Items(R).SubItems(2).Text = ComboBox2.Text
'It's the string with the unitPrice as string
Dim unitPrice As String = Me.ListView1.Items(R).SubItems(3).Text
'Here is converted to integer
Dim unitPriceInt As Integer = Convert.ToInt32(unitPrice)
'Same thing, string to integer
Dim comboInt As Integer = Convert.ToInt32(ComboBox2.Text)
'This is the product of the multiplication
Dim totalPrice As Integer = unitPriceInt * comboInt
'This is where i try to input the number into the Rth row 4 indexed column...
Me.ListView1.Items(R).SubItems(4).Text = totalPrice.ToString
R = R + 1
Any ideas??
-
Re: Converting String to Integer
Give each column a key instead, and access them that way.... does seem a bit odd thought.
-tg
-
Re: Converting String to Integer
I would venture to guess the issue is that the given listviewitem you are working with doesn't have subitems for all the columns. Just because a listview has 5 columns, doesn't automatically make it so listviewitems added to the listview have 5 items. It just means it can have UP TO 5 items.
Chances are in this line of code:
Code:
Me.ListView1.Items(R).SubItems(4).Text = totalPrice.ToString
Me.ListView1.Items(R) doesn't have a subitem at index 4. A simple test would be to check the Me.ListView1.Items(R).subitems.count property.
-
Re: Converting String to Integer
I'm sorry, but how do i give each column a key? I also did the "test" and it said "4".....
-
Re: Converting String to Integer
in the designer...select the listview and look at it's properties. open the property window for the Columns collection... select each col entry. In the property list for each col, there should be a "Key" entry....
-tg
-
Re: Converting String to Integer
Yes, i did that, but it only had "Image key", but I couldn't change it, still said (none)
-
Re: Converting String to Integer
yes, but subitems include the first item. So if you have a 5 column listview then the indexes are
0,1,2,3,4
so if the subitem count is only 4, you are missing a subitem for the given listviewitem.
The count should be 5.
-
Re: Converting String to Integer
Yes, but when i try to input a subitem for that given listviewitem, it gives me the error of the index being invalid, and just how i posted, i don't know how to change the image key (or key) of each column.
-
Re: Converting String to Integer
this code is not adding a subitem
Code:
Me.ListView1.Items(R).SubItems(4).Text = totalPrice.ToString
all this code does is try to assign a text value to a subitem that already exists.
try
Code:
me.listview1.items(r).subitems.add(totalPrice.ToString)
-
Re: Converting String to Integer
YEAH! It works! But why does it add it to the empty subitem? (column 5, indexed 4), i just want to understand :D
-
Re: Converting String to Integer
because subitems 0-3 (columns 1-4) must already have data. subitem 4 (column location 5) does not. So when you add another subitem, it ads it to the next place in the subitem list.
-
Re: Converting String to Integer
So... one last question: if i had two empty columns, would it place the info in both colums or just in the first one? (first one, i mean from left to right, the first one empty)
-
Re: Converting String to Integer
first one. If you called SubItems.Add("Value") another time, it would fill in the next column that doesn't have a value in it.
Note a listviewitem having a subitem with empty text, and a listviewitem not having a subitem at all are 2 different things.
-
Re: Converting String to Integer
And how can i check if it has an empty text or doesnt have a subitem? Can i determine that by using Me.ListView1.Items(R).subitems.count ?
-
Re: Converting String to Integer
yes.
So for example you will never have 5 columns in a listview, but one of the middle columns doesn't have a value (remember being blank doesn't mean it doesn't have a value, null (nothing in visual basic) and ""/string.empty are not the same thing)
The order of subitems is always left to right sequential so the number and order of possible subitems in a 5 column listview would be this
1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
-
Re: [RESOLVED] Converting String to Integer
OK, thanks for your patience and your help! (So, just to be sure, the only way that a subitem will not have a value, will be if we don't do anything to it, right?)
And, THANKS A LOT!
-
Re: [RESOLVED] Converting String to Integer
The general rule I go by is when I am making a listview, and I am adding listviewitems to it, I always add the number of subitems to fill up all columns.
So for a 5 column listview, even if the other columns don't have values right now (but will later, otherwise why would it be a column in the first place) then I would add a listviewitem to the listview like this:
Code:
'ADD ALL 5 COLUMNS GIVING INITIAL VALUES
Dim LVI As New ListViewItem("Column 1")
LVI.SubItems.AddRange(New String() {"Column 2", "Column 3", "Column 4", "Column 5"})
listview1.Items.Add(LVI)
'ADD ALL 5 COLUMNS, BUT LEAVE ALL BLANK BUT FIRST
Dim LVI As New ListViewItem("Column 1")
LVI.SubItems.AddRange(New String() {"", "", "", ""})
listview1.Items.Add(LVI)
-
Re: [RESOLVED] Converting String to Integer
Thanks again! that sure helps!!