0

I have the following function which is supposed to check whether the given entry is above 0.0

Dim inputstr As String = .Item("conc")

Try
    Dim concentration As Double = CDbl(inputstr)
Catch ex As Exception
    Dim concentration As Double = -1.0
Finally
    Dim concentration As Double = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

However, I keep getting "concentration is not declared'. It may be inaccessible due to its protection level.

Any ideas? Thanks

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
Zee Fer
  • 339
  • 4
  • 14

5 Answers5

3

Variable Scope

The variable Concentration exists only in the Try block. Therefore, whenever you leave this block, the variable no longer exists.

To solve this, you must declare Concentration before the Try.

Finally Block

Another problem you will encounter is that Concentration will always be -1, because you said so in the Finally block, this block is not necessary here.

Dim inputstr As String = .Item("conc")
Dim concentration As Double

Try
    concentration = CDbl(inputstr)
Catch ex As Exception
    concentration = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

A bit of reading about Variable scope in VB .Net

And another bit of reading about Try/Catch blocks

Don't use Try/Catch to do that

However, as stated by Fabio, you can use Double.TryParse() to do that, it is easier to read and more important, it is a performance increase.

So, in the end, it is better coding practice to do:

Dim inputstr As String = .Item("conc")
Dim concentration As Double

If Not Double.TryParse(inputstr, concentration) Then
    concentration = -1.0
End If

If concentration > 0.0 Then
    err = 1
End If
Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
2

It is not in scope outside of the try catch. declare it out side then try to set it within the try/catch. Problem though is that it will always end up as -1.0 because of the Finally.

Dim inputstr As String = .Item("conc")
Dim concentration As Double = 0.0
Try
    concentration = CDbl(inputstr)
Catch ex As Exception
    concentration = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

Ultimately you can use Double.TryParse

Dim inputstr As String = .Item("conc")
Dim concentration As Double = 0.0
If Double.TryParse(inputstr, concentration) AndAlso concentration > 0.0 then
    'Do what you intended
End If
Nkosi
  • 235,767
  • 35
  • 427
  • 472
2

While you main problem that concentration is not in the scope
Finally will be executed always, doesn't meter was exception thrown or not - so concentration will be always set to -1.0
- you still don't need to use Try .. Catch approach. Double.TryParse will handle it in one line.

Dim concentration As Double = 0.0
Double.TryParse(.Item("conc"), concentration)

err = If(concentration > 0.0, 1, 0)

Double.TryParse(inputstr, concentration) - will set concentration to converted double value or to default value, which is 0.0 for double type if string is not in correct format.

Fabio
  • 31,528
  • 4
  • 33
  • 72
1

How about using VB.Net, and use Double.TryParse or Double.Parse

Which would make your code something like

dim concentration as Double = -1
'dont really know where this one is coming from though
err = 0

if Double.TryParse(inputstr, concentration) then
    if concentration > 0.0 then
        err = 1
    end if
end if
Icepickle
  • 12,689
  • 3
  • 34
  • 48
0

in fact, you have 2 issues with your code

one concentration need to be declared before the try
second remove the finally otherwise it will ALWAYS be -1.0

Fredou
  • 19,848
  • 10
  • 58
  • 113