-2

I am new to programming and can't figure out how to apply division to a string in Visual Basic; specifically to "discount".

The

 = Data("discount") /100" 

doesn't have any effect.

Me.fldRate.txt=lstrText

Me.txtDiscount.Value = Data("discount") /100
If not Data.EOF then 
    Me.txtDiscountedRate.Visible = True
    Me.txtDiscount.Visible = False
    If Data ("discount") > 0 and Data("discountable") <> 0 Then
       blnDiscount = True
       Me.txtDiscount.Visible = True 

Is there a work around?

Thank you in advance.

Thor
  • 1
  • 2
    What is the value of _Data("Discount")_ when you want to divide it by 100? Do you have any error message and if yes, on which line? – Steve Mar 02 '20 at 18:25
  • `Me.txtDiscount` seems like it would be a TextBox however it has a .Value property. You really should include all information such as what type that control is. – djv Mar 02 '20 at 18:48
  • I think we could help a lot more if we saw more of the code in this method. Also, is this really VB.Net or is it VBA? Modern VB.Net should pretty much never use the `EOF` property, and VBA is much more likely to check a `Value` property on a control. – Joel Coehoorn Mar 02 '20 at 20:05

3 Answers3

1

There's not enough information to be certain of what's going on, but if I had to guess I'd bet the Data variable is an old-school ADO RecordSet object. If this is true, the type of the Data("discount").Value expression is Object, with an actual underlying type determined based on the SQL command. Moreover, I'd bet the specific type here is Integer.

Pop quiz: what happens in programming when you divide an Integer by another Integer?

The result is still an Integer! You get integer division, where any fractional part of the result is completely discarded. Therefore, if you have a table full of integer discount percentages, where of course no one ever has discounts that are negative or more than 100, the result of this statement will always be 0.

To fix it, again we'd need to see more of what's going on. But if this really is VB.Net, rather than VBA or old-school VB6, I hate to say this but I'd start by rewriting all data access from scratch (really!) to get rid of these old classic ADO APIs. The older API library exists only for backwards compatibility with old code, and should not be used anymore for new development! Look for the newer ADO.Net API in the System.Data namespace. Additionally, for this rewrite you should turn on Option Strict, which would have forced you to deal with this mistake much earlier.

You want to end up with code more like this:

Dim sql As String = "SELECT discount, ... WHERE SomeKeyColum = @KeyField"
Using cn As New SqlConnection("connection string here"), _
      cmd As New SqlCommand(sql, cn)

    cmd.Parameter.Add("@KeyField", SqlDbType.NVarChar, 50).Value = "KeyValue"
    cn.Open()

    Using rdr As SqlDataReader = cmd.ExecuteReader()
        While rdr.Read()
            Dim discount As Decimal = DirectCast(rdr("discount"), Integer) / 100.0D
            '...

        End While
    End Using
End Using

One more thing. Microsoft updated their Visual Basic programming guide in between the last VB6/vbscript/VBA release and VB.Net, to explicitly forbid hungarian type prefixes like lstr, bln, etc. This is not binding or mandatory, but it is the published recommendation of the vendor. In modern code today, these prefixes tend to only survive for WinForms control variables.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

You might have to convert the string to a number. Wrap your Data("discount") with the conversion function CDbl().

Me.txtDiscount.Value = CDbl(Data("discount")) /100
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

As others have said, it really depends on the data type of Data("Discounts")

' It's String or varchar
Dim discount As Double = 0R
If Double.TryParse(CStr(Data("discount")), discount) Then

    ' Conversion passed, use the discount variable
    Me.txtDiscount.Value = (discount / 100.0R).ToString()
Else
    ' conversion failed...throw error or log
End If

 ' Already a double
Dim discount2 As Double = Data("discount")
Me.txtDiscount.Value = (discount2 / 100.0R).ToString()
JohnPete22
  • 523
  • 3
  • 15