Question Conversion to Double

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Hello.
This is very easy, but not for me! :(

I have some numeric fields to compare with a number and act according to the result. But sometimes the field is missing and his value equals ""
In that case I have to compare a string ("") with a number, and CDbl does not convert my empty field. I know I can put before another conditional, but I suppose there must be a wiser way to do it.

My code is:
VB.NET:
 For x = 0 To 11
                If Formdgv.dgv1.Rows(x).Cells("Column1").Value < 0.01 Then
                    Formdgv.dgv1.Rows(x).Cells("Column1").Style.ForeColor = Color.Green
                End If
                If Formdgv.dgv1.Rows(x).Cells("Column2").Value < 0.01 Then
                    Formdgv.dgv1.Rows(x).Cells("Column2").Style.ForeColor = Color.Green
                End If
...
Next

and I suppose it could be:
VB.NET:
For x = 0 To 11
                If Cstr(Formdgv.dgv1.Rows(x).Cells("Column1").Value) <> "" Then
                    If Formdgv.dgv1.Rows(x).Cells("Column1").Value < 0.01 Then
                        Formdgv.dgv1.Rows(x).Cells("Column1").Style.ForeColor = Color.Green
                    End If
                End If
                If Cstr(Formdgv.dgv1.Rows(x).Cells("Column2").Value) <> "" Then
                    If Formdgv.dgv1.Rows(x).Cells("Column2").Value < 0.01 Then
                        Formdgv.dgv1.Rows(x).Cells("Column2").Style.ForeColor = Color.Green
                    End If
                End If
...
Next

Thank you for your help.
 
Last edited:
Your code is poor because you are comparing an Object reference directly with a Double. That code wouldn't even compile with Option Strict On, which it should pretty much always be. Unfortunately, Option Strict is Off by default but, now that you know it exists, you should turn it On in the is project and in the IDE so that it will be On by default for future projects. That will force you to consider your data types much better than you currently are.

Firstly, the Value property of a DataGridViewCell is type Object, because the cell can contain anything. If your cells contains Strings then you should be casting that Value property as type String, which you could do with CStr. If that String is supposed to represent a Double but may not then you should be using Double.TryParse to convert it. Double.TryParse returns a Boolean to indicate whether the conversion was successful and, if it was, it also outputs the number. Your code then becomes something like this:
Dim number As Double

If Double.TryParse(CStr(Formdgv.dgv1.Rows(x).Cells("Column1").Value), number) AndAlso number < 0.01 Then
    '...
End If
If the cell doesn't contain a valid numerical value then Double.TryParse returns False and the numeric comparison is never performed. If the value does convert successfully then the result is compared to your literal value.
 
1. Thank you very much
2. I do not know why, but I work with
VB.NET:
[COLOR=#0000cd]Option Strict Off
[/COLOR][COLOR=#0000cd]Option Explicit On
[/COLOR]
I turn Strict On
3.
I change my code and use the
VB.NET:
[COLOR=#3E3E3E][FONT=Consolas]Dim number As Double[/FONT][/COLOR]

[COLOR=#3E3E3E][FONT=Consolas]If Double.TryParse(CStr(Formdgv.dgv1.Rows(x).Cells("Column1").Value), number) AndAlso number < 0.01 Then[/FONT][/COLOR]
[COLOR=#3E3E3E][FONT=Consolas]    '...[/FONT][/COLOR]
[COLOR=#3E3E3E][FONT=Consolas]End If[/FONT][/COLOR]
4. I learn!
 
Wooobs!
Option Strict On is giving me a lot of problems.
V.gr.:
VB.NET:
oBook = oExcel.Workbooks.Add
oSheet.Range("A1").Value = "Time" 
...
The error goes: Option Strict On doesn't allow run-time binding
 
1. Thank you very much
2. I do not know why, but I work with
VB.NET:
[COLOR=#0000cd]Option Strict Off
[/COLOR][COLOR=#0000cd]Option Explicit On
[/COLOR]

That would be because they are the default. There are logical reasons that Option Strict is Off by default but many, including myself, can see more logical reasons for it to be On.
 
Wooobs!
Option Strict On is giving me a lot of problems.
V.gr.:
VB.NET:
oBook = oExcel.Workbooks.Add
oSheet.Range("A1").Value = "Time" 
...
The error goes: Option Strict On doesn't allow run-time binding

The main reason that Option Strict exists is because it must be Off to allow late-binding and there are cases where late-binding is required. The question is, do you require late-binding here? If you're using Office Automation then maybe and maybe not. Are you trying to support multiple versions of Office? If you are then you may well need late-binding. If you're not then you definitely don't. Even if you are, the way it's usually done is to turn Option Strict On and use early-binding everywhere, i.e. define the appropriate type for every variable, until the app is finished and then remove the Office reference, turn Option Strict Off and change all the missing types to Object.

Even if late-binding is required, it's still better to have Option Strict On for the project and then have Option Strict Off only in the files that require it. By using partial classes, you can keep the code in files where Option Strict is Off to the absolute minimum, e.g. if you have a file that contains three methods and only one uses late-binding, create a second code file with a partial class in each and move only the method requires late-binding into the second file with Option Strict Off.
 
Back
Top