Results 1 to 14 of 14

Thread: [RESOLVED] Type problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    20

    Resolved [RESOLVED] Type problem

    Hey, I'm working on VB 2008 and I keep having the same problem when I debug. It keeps telling me "Conversion from string "blah blah" to type 'Double' is not valid." I know that its something to do with the types and possibly ByVals? I have changed around the types many times an I keep coming up with errors while debugging. I currently have no errors on the error list and I am stumped.

    What it should do first is read the input from the txtboxes on my form then calculate meal and total cost in the Processing function. then give output in the output sub. Thanks in advance.


    HTML Code:
     Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
            Inpt()
        End Sub
    
        Sub Inpt()
            Dim num1, num2, num3, num4, num5, num6, num7, calcMeal, totalCost As Double
            num1 = CDbl(org.Text)
            num2 = CDbl(dat.Text)
            num3 = CDbl(loc.Text)
            num4 = CDbl(mea.Text)
            num5 = CDbl(air.Text)
            num6 = CDbl(tax.Text)
            num7 = CDbl(tax.Text)
            Processing(num1, num2, num3, num4, calcMeal, totalCost, num5, num6, num7)
        End Sub
       
     Function Processing(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double, ByVal num4 As Double, ByVal calcMeal As Double, ByVal totalCost As Double, ByVal num5 As Double, ByVal num6 As Double, ByVal num7 As Double) As Double
            calcMeal = num4 * 0.5
            totalCost = num4 + num5 + num6 + num7
            Output(num1, num2, num3, num4, num5, num6, num7,totalCost, calcMeal)
        End Function
    Sub Output(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double, ByVal num4 As Double, ByVal num5 As Double, ByVal num6 As Double, ByVal num7 As Double, ByVal totalCost As Double, ByVal calcMeal As Double)
    
    some code omitted
    Last edited by That Guy; Oct 8th, 2009 at 07:14 PM.

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

    Re: Type problem

    The problem is that you're using CDbl to convert a String, i.e. the Text form a TextBox, into a Double, which is obviously a number. Now, if the String you pass to CDbl doesn't represent a number then you will get that error message. You must ONLY ever use CDbl if you know for sure that the data is valid, which you can't really know if it's unvalidated user input.

    You should use the Double.TryParse method, which will attempt to convert the String to a Double and return True or False to let you know whether it succeeded or not. You can use an If statement to test all the return values and, if any of them are False, then you cannot proceed with your calculation.

    Now, if this is a learning exercise then teachers will often say that you can use something like CDbl and just assume that the input will be valid. This makes the code less complex and allows you to concentrate on the point of the lesson rather than validation. In that case though, you MUST make sure that, when you test, you actually do provide valid data.

    On an unrelated topic, why is your Processing method a Function when it doesn't return anything?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    20

    Re: Type problem

    Ok, well I understand the need for data validation but I need to use CDbl.
    I took some of your suggestions and now have a problem with the types in the inpt sub. Its this line...
    HTML Code:
    Processing(CDbl(Str(num1)), CDbl(Str(num2)), CDbl(Str(num3)), num4, calcMeal, totalCost, num5, num6, num7)
    I tried to convert it along with everything else and having problems there now.

    HTML Code:
      Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
            Inpt()
        End Sub
        Sub Inpt()
            Dim num1, num2, num3 As String
            Dim num4, num5, num6, num7 As Double
            Dim calcMeal, totalCost As Integer
            num1 = (org.Text)
            num2 = (dat.Text)
            num3 = (loc.Text)
            num4 = CDbl(mea.Text)
            num5 = CDbl(air.Text)
            num6 = CDbl(lod.Text)
            num7 = CDbl(tax.Text)
            
    
    Processing(CDbl(Str(num1)), CDbl(Str(num2)), CDbl(Str(num3)), num4, calcMeal, totalCost, num5, num6, num7)
    
    
        End Sub
        Sub Processing(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double, ByVal num4 As Double, ByVal calcMeal As Double, ByVal totalCost As Double, ByVal num5 As Double, ByVal num6 As Double, ByVal num7 As Double)
            calcMeal = num4 * 0.5
            totalCost = num4 + num5 + num6 + num7
            Output(num1, num2, num3, num4, num5, num6, num7, totalCost, calcMeal)
        End Sub
        Sub Output(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double, ByVal num4 As Double, ByVal num5 As Double, ByVal num6 As Double, ByVal num7 As Double, ByVal totalCost As Double, ByVal calcMeal As Double)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Type problem

    Why exactly do you NEED to use CDbl? Is it because your teacher told you to? If so then the assumption is that the data will be valid. In that case there's no need for you to change your code. What you need to do is make sure you enter valid data. Just read the error message. It's telling you that the conversion from a particular string to a number isnt valid. Changing the code isn't going to change that. If you enter some text that isn't a number then you can't convert it to a Double, plain and simple.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Type problem

    What are you entering, and where are you from? One possibility is that you are entering symbols, like $. Or you are using a comma in place of a decimal (with certain regional settings), or that you are leaving a textbox empty.

    By the way, CDbl(Str()) is converting a number to a string, then back to a number. That's a waste of time.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    20

    Re: Type problem

    I do live in the U.S so i am not using any different characters or entering dollar signs. For num1-num3 i am using text and for num4-7 i am using numbers. I am still having problems with the processing statement in the Inpt sub. I have no errors or warnings but when i run and put in the right data i return a error saying "Argument 'Number' cannot be converted to a numeric value." and it points to the Processing statement in the Inpt sub.

    HTML Code:
    Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
            Inpt()
        End Sub
        Sub Inpt()
            Dim num1, num2, num3 As String
            Dim num4, num5, num6, num7 As Double
            num1 = (org.Text)
            num2 = (dat.Text)
            num3 = (loc.Text)
            num4 = CDbl(mea.Text)
            num5 = CDbl(air.Text)
            num6 = CDbl(lod.Text)
            num7 = CDbl(tax.Text)
            Processing(Str(num1), Str(num2), Str(num3), CDbl(num4), CDbl(num5), CDbl(num6), CDbl(num7))
        End Sub
        Sub Processing(ByVal num1 As String, ByVal num2 As String, ByVal num3 As String, ByVal num4 As Double, _
                       ByVal num5 As Double, ByVal num6 As Double, ByVal num7 As Double)
            Dim calcMeal, totalCost As Double
            calcMeal = num4 * 0.5
            totalCost = num4 + num5 + num6 + num7
            Output(Str(num1), Str(num2), Str(num3), CDbl(num4), CDbl(num5), CDbl(num6), CDbl(num7), CDbl(totalCost), CDbl(calcMeal))
        End Sub
        Sub Output(ByVal num1 As String, ByVal num2 As String, ByVal num3 As String, ByVal num4 As Double, ByVal num5 As Double, _
                   ByVal num6 As Double, ByVal num7 As Double, ByVal totalCost As Double, ByVal calcMeal As Double)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Type problem

    Why are you passing some values as Strings and some as Doubles?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    20

    Re: Type problem

    Because for num1-num3 i am using text and for num4-7 i am using numbers

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

    Re: Type problem

    oi now!....

    You do this:
    num4 = CDbl(mea.Text)

    and then do this:
    CDbl(num4)

    What the heck? num4 IS a double already... so there's no need to do that a second time.

    "when i run and put in the right data i return a error " -- WHAT is the "right" data?

    Put a break point on the first line of your sub... check the values of your variables and stuff... I suspect something isn't what you think it it.

    And I HIGHLY recommend using meaningful variable names....

    -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??? *

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

    Re: Type problem

    I totally agree. This is the time for a bit of debugging, as there is something in there that is not what you think it is. If you haven't used breakpoints and stepping through code, ask. It's the most useful tool you can have.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    20

    Re: Type problem

    Thanks for the help guys. I finally learned how to properly debug and found the problem. Thanks for not just posting an answer because I learned a lot by figuring it out. I came to this forum because I was spending hours upon hours searching, reading, and changing but some how I couldn't put it all together. I finally did put in some stops and found out that I am an idiot. It was right in front of my face and it took a while for me to get there. It all makes total sense because I was over thinking and beating the crap out of the code.

    Thanks again

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

    Re: [RESOLVED] Type problem

    Isn't learning fun? Frankly, I kind of hate it while I'm doing it, but I do love it after the fact.
    My usual boring signature: Nothing

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

    Re: [RESOLVED] Type problem

    depens on what you're learning... if it's something new and useful, like say working with WPF or something, it can be fun... if it's learning how to mangle a manifest file... then yeah, I'm with you shaggy.

    -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
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] Type problem

    My destiny is manifest.

    This weekend I will be exploring genetic algorithms and a novel neural net implementation. That will be FUN!!!
    My usual boring signature: Nothing

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