Results 1 to 7 of 7

Thread: multiplying two text boxes

  1. #1
    crazymarvin is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2006
    Location
    Belfast, UK
    Posts
    18
    Reputation
    100

    Question multiplying two text boxes

    I need to multiply two integers in two different text boxes, i was useing the following code:
    Code:
    Me.txtboxCost = (frmOrder.txtboxSize.Text * txtboxPsqm)
    and vs 2005 says:
    Error 1 Operator '*' is not defined for types 'String' and 'System.Windows.Forms.TextBox'.
    I'm sure this is something simple lol, i know the method I'm useing works in vs 2003, is this just a difference between the two versions of the langauge or am i missing something?

    thanks, martyn

  2. #2
    vis781's Avatar
    vis781 is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2005
    Location
    Cambridge, UK
    Posts
    2,015
    Reputation
    268
    If i were to write..

    10 * 10

    Would you say the output would be 100. Well a peoples world where we can tell that the 10 is ment to be a number you probably would. However a textbox.text is a string not a number value. You can't multiply a string, you have to multiply a number type i.e.. Integer etc.. So you need to convert the values in the textbox to integers.

    Code:
     
    Dim Number1 as Integer = Convert.Toint32(txtboxsize.text)
    With me?
    GDI+ Code Generation Utility : GDI+ Architect

    A Must : FxCop .Net Code analyzer : FxCop

  3. #3
    CPCisHere's Avatar
    CPCisHere is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2006
    Posts
    4
    Reputation
    0
    Try this

    Code:
    Me.txtboxCost = val(frmOrder.txtboxSize.Text) * val(txtboxPsqm)

  4. #4
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    978
    Quote Originally Posted by CPCisHere View Post
    Try this

    Code:
    Me.txtboxCost = val(frmOrder.txtboxSize.Text) * val(txtboxPsqm)
    actually it's recamended that we stay away from the val() function, i would personally use a decimal or double variable type for this:

    Code:
    Me.txtboxCost.Text = (CDec(frmOrder.txtboxSize.Text) * CDec(Me.txtboxPsqm.Text)).ToString()
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  5. #5
    EStallworth is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Aug 2006
    Location
    Destin, FL
    Posts
    75
    Reputation
    104
    Quote Originally Posted by JuggaloBrotha View Post
    actually it's recamended that we stay away from the val() function, i would personally use a decimal or double variable type for this:

    Why is it that it is recommended to not use val()? I am asking becaue I have an application that does a bunch of calculations and I have either used val() or CType.
    -Eli Stallworth

    Okaloosa County Clerk of Courts
    Computer Programmer

  6. #6
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    978
    the Val() function is legacy code located in the VisualBasic namespace (which there is rumor that future versions of VB will not support it) therefor it's recommended that we don't use it when building applications. Also avoiding the Val() function helps better utilize computer resources by just converting the value to a specific variable (eg. use CDec() to convert to a decimal, CInt() for integer, Cstr() and .ToString() for strings, CDbl() for double etc) or CType() specifying the variable type there too
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  7. #7
    EStallworth is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Aug 2006
    Location
    Destin, FL
    Posts
    75
    Reputation
    104
    Thanks for the insight! I will have to go back and make some modifications.
    -Eli Stallworth

    Okaloosa County Clerk of Courts
    Computer Programmer

Bookmarks

Posting Permissions

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