Results 1 to 26 of 26

Thread: [RESOLVED] Converting String to Integer

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [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...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Converting String to Integer

    This seems to work OK
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim v As String = "50"
    3.         Dim val As Integer = Convert.ToInt32(v)
    4.         Dim ival As Integer = 3 * val
    5.         MessageBox.Show(ival.ToString)
    6. End Sub
    Quote Originally Posted by tassa View Post
    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.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Converting String to Integer

    since Val is a function in VB.... I'd use a different variable name....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Converting String to Integer

    Quote Originally Posted by techgnome View Post
    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.
    My usual boring signature: Nothing

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Converting String to Integer

    i agree about val, and was going to say something and then totally forgot.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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??
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Converting String to Integer

    Give each column a key instead, and access them that way.... does seem a bit odd thought.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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".....
    Last edited by tassa; Mar 18th, 2009 at 12:01 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Converting String to Integer

    Yes, i did that, but it only had "Image key", but I couldn't change it, still said (none)
    Last edited by tassa; Mar 18th, 2009 at 12:07 PM. Reason: I had a double post...
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  15. #15
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  17. #17
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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)

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  19. #19
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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)
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  21. #21
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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 ?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  23. #23
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  25. #25
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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)

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] Converting String to Integer

    Thanks again! that sure helps!!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width