Results 1 to 3 of 3

Thread: [RESOLVED] conversion from string to type 'double' is not valid

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    4

    Resolved [RESOLVED] conversion from string to type 'double' is not valid

    Code:
    Imports System.Net
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            lstboxoption.Items.Add("IP Address")
            lstboxoption.Items.Add("Source Code")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If lstboxoption.SelectedItem = 0 Then
                Form2.Show()
            End If
        End Sub
    End Class
    I want to make it so that when a user clicks on the first item which is "IP Address" to show Form2, I'm not sure if I'm using the SelectedItem correctly. I keep getting a "conversion from string to type double is not valid" error.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: conversion from string to type 'double' is not valid

    Look at this code:
    vb.net Code:
    1. lstboxoption.Items.Add("IP Address")
    2. lstboxoption.Items.Add("Source Code")
    You are adding to the Items collection of the ListBox the values "IP Address" and "Source Code". Those are the only items in the ListBox so those are the only items the user can select. If that's the case, when the user makes a selection, how can the SelectedItem possibly be 0? If the user selected the item "IP Address" then the SelectedItem will be "IP Address". That is the first item so its index is 0, thus the SelectedIndex will be 0.

    I have two pieces of advice that will help you avoid situations like this in future. Firstly, ALWAYS read the documentation. The VS Help menu will open the documentation either locally or online. Either way, you can open the topic for the ListBox class and read what each member means and does, and often see code examples. That won't always answer all your questions but it should still be the first option when you need help. The more often you do it, the better you get at it and the more issues you'll be able to fix doing it. You'll also come across information that you weren't looking for that will help later quite regularly. I speak from personal experience, as someone who has used the MSDN documentation as my primary source of information since the first day I used VB.NET.

    Secondly, learn to debug properly. That doesn't mean just reading the code or even just running it. It means running it, setting breakpoints and stepping through the code line by line, examining the state as you go. In this case, doing that would have shown you exactly what SelectedItem and SelectedIndex contained. If you don't know how to debug, start learning here:

    https://msdn.microsoft.com/en-us/lib...or=-2147217396

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    4

    Re: conversion from string to type 'double' is not valid

    Quote Originally Posted by jmcilhinney View Post
    Look at this code:
    vb.net Code:
    1. lstboxoption.Items.Add("IP Address")
    2. lstboxoption.Items.Add("Source Code")
    You are adding to the Items collection of the ListBox the values "IP Address" and "Source Code". Those are the only items in the ListBox so those are the only items the user can select. If that's the case, when the user makes a selection, how can the SelectedItem possibly be 0? If the user selected the item "IP Address" then the SelectedItem will be "IP Address". That is the first item so its index is 0, thus the SelectedIndex will be 0.

    I have two pieces of advice that will help you avoid situations like this in future. Firstly, ALWAYS read the documentation. The VS Help menu will open the documentation either locally or online. Either way, you can open the topic for the ListBox class and read what each member means and does, and often see code examples. That won't always answer all your questions but it should still be the first option when you need help. The more often you do it, the better you get at it and the more issues you'll be able to fix doing it. You'll also come across information that you weren't looking for that will help later quite regularly. I speak from personal experience, as someone who has used the MSDN documentation as my primary source of information since the first day I used VB.NET.

    Secondly, learn to debug properly. That doesn't mean just reading the code or even just running it. It means running it, setting breakpoints and stepping through the code line by line, examining the state as you go. In this case, doing that would have shown you exactly what SelectedItem and SelectedIndex contained. If you don't know how to debug, start learning here:

    https://msdn.microsoft.com/en-us/lib...or=-2147217396
    Thank you for your help sir, and I will definitely follow your advice,

Tags for this Thread

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