PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 Syntax error-VBForums
Results 1 to 9 of 9

Thread: Syntax error

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    11

    Syntax error

    Private Sub GstTextBox_LostFocus(sender As Object, e As EventArgs) Handles GstTextBox.LostFocus

    (TotalTextBox = CostTextBox + (CostTextBox / GstTextBox))

    'Cost field being numeric(9, 2)
    'Gst field being numeric(2,0)
    'Total field being numeric(9,2)

    'I have syntax error in front of 1st bracket.

    'I am very new to VB and have tried all that I can to resolve this. (saturation point)

    'Please help, thank you'
    End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,734

    Re: Syntax error

    I'd say that there are several errors in the line. For one thing, you are trying to assign to a Textbox, but you can't do that. You assign to the textbox.Text property, so it might (sort of) work if you wrote TotalTextBox.Text =

    That is only the beginning, though, and probably isn't the error you are getting. That's probably the error you haven't gotten yet. That outer set of parenthesis is wrong. The line should (sort of) be this:

    TotalTextBox.Text = CostTextBox.Text + (CostTextBox.Text/GstTextBox.Text)

    However, it pains me to write that, because there is plenty wrong with that line, too. It should, at least, avoid the error you were getting, and the error that you hadn't yet gotten, but it's still totally horrible. For one thing, textboxes hold strings. The strings may look like numbers but they aren't. They are strings. VB will let you perform math with strings if you have Option Strict OFF, and the strings happen to be numbers, but you shouldn't do it for a variety of reasons. For one thing, the strings have to be implicitly converted to numbers. If they can't be implicitly converted, then you'll get an exception. If you used a NumericUpDown control rather than a textbox, then you could use the Value property which would be type Decimal, which IS a number, so those implicit conversions won't happen, and you won't get exceptions.

    The next issue is that you are putting the result into a textbox. The user has the expectation that anything in a textbox can be changed, but this is a result of a calculation. They shouldn't be changing that, so put the result into a label, rather than a textbox. Better still, put it into a variable so you can use it elsewhere, and ALSO put it into a label.

    LostFocus might not be a great event for this, but that can wait.

    By the way, that "numeric(n,n)" has no meaning in most languages. A number is a number. Integers have no decimal places, doubles have decimal places, Decimals have decimal places, but you don't designate how many in any of those types. That kind of notation is used for some databases, and you can use something like that for formatting numbers for display, but a number is just a number.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,201

    Re: Syntax error

    Please format code snippets for easy readability. The longer the code snippet, the more important that becomes.
    vb.net Code:
    1. Private Sub GstTextBox_LostFocus(sender As Object, e As EventArgs) Handles GstTextBox.LostFocus
    2.  
    3.         (TotalTextBox = CostTextBox + (CostTextBox / GstTextBox))

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,201

    Re: Syntax error

    Quote Originally Posted by Shaggy Hiker View Post
    LostFocus might not be a great event for this, but that can wait.
    No it can't. That's actually a simple fix, too. As the documentation for the LostFocus event says, application developers should handle Enter and Leave rather than GotFocus and LostFocus. The latter are more for custom control developers.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    11

    Re: Syntax error

    So GstTextBox.Text worked with Return Event. But NOT the way to go, you say.

    Is Cost Gst and Total the wrong field data type?
    My assumption was them being a numeric field, the generic name might be something like GstNumericBox.
    I don't see in Properties a way of formatting fields to a numeric display.

    Anyway, I'll work on variables and labels.

    Question: What is the point of assigning a field Numeric(9,2) when it has no use?
    Many thanks Shaggy Hiker.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    11

    Re: Syntax error

    Including .Text in the code under the Leave event seems to work fine.

    Many thanks.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,201

    Re: Syntax error

    The additional problems that Shaggy was alluding to relate to the fact that you are assuming that the user will do the right thing and that the system will do some of the work for you. Both are bad assumptions. You should anticipate ways in which users could misuse your app, both accidentally and intentionally, and ensure that you handle such situations gracefully rather than crashing. You should also do things like make sure that you are actually using numbers where numbers are expected and text where text is expected. Setting Option Strict On will help with the latter, because the compiler will insist that you do so. As an example, you are trying to perform arithmetic with text. Arithmetic only works with numbers, so the system has to implicitly convert your text to numbers, which can be done but is less efficient than you doing it explicitly. It will also fail if the text doesn't represent a valid number, so you should be validating that text and then explicitly converting it to a number. Code like yours would be better written like this:
    vb.net Code:
    1. Dim cost As Decimal
    2. Dim gst As Decimal
    3.  
    4. If Decimal.TryParse(CostTextBox.Text, cost) AndAlso Decimal.TryParse(GstTextBox.Text, gst) Then
    5.     TotalTextBox.Text = (cost + (cost / gst)).ToString()
    6. Else
    7.     'Notify the user that the input data was invalid.
    8. End If

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    11

    Re: Syntax error

    Thank you jmcilhinney.
    Your code is awesome and Option Strict On is ON.

    Solved.

    Many thanks, Shane.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,734

    Re: Syntax error

    Using a NumericUpDown control rather than a Textbox would remove the need for the Decimal.TryParse, as a NumericUpDown.Value property returns a Decimal. You can ONLY enter numbers into a NUD, too, which is convenient. You can also set the minimum and maximum, along with the number of decimal places.

    As to this:
    Question: What is the point of assigning a field Numeric(9,2) when it has no use?
    Many thanks Shaggy Hiker.
    I'm not sure what you mean there. If you are talking about a field in a database, then there is a meaning, but there isn't a meaning in the .NET language that I am aware of. Databases might use that as a means to store a certain level of precision for a number. In .NET, as in most programming languages, you get a certain data size with each data type, and you get ALL the precision of that data size when you use that type. For example, an Integer is a 32-bit number, though it is a signed number, so you get roughly minus two billion to plus two billion, whereas an unsigned integer would hold from zero to a bit over four billion. Doubles are eight byte values using one specific (and common) format which has a very slight imprecision, though better performance. Decimals are the same size, without the slight imprecision, and slightly worse performance. You won't see the difference in performance in most programs, so Decimals are always used for financial work, while Doubles are used for other math.
    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
  •  



Featured


Click Here to Expand Forum to Full Width