Link to home
Create AccountLog in
Avatar of marcgu
marcguFlag for Sweden

asked on

How to avoid error BC30616 in vb.net page

Hi!

I am trying to get a better understanding of type conversions and therefore I tried to use this example from microsoft in my own aspx. web page

http://msdn.microsoft.com/en-us/library/zh1hkw6k.aspx

Dim values() As String = { "-1,035.77219", "1AFF", "1e-35", _
                           "1,635,592,999,999,999,999,999,999", "-17.455", _
                           "190.34001", "1.29e325"}
Dim result As Double 

For Each value As String In values
   Try
      result = Convert.ToDouble(value)
      Console.WriteLine("Converted '{0}' to {1}.", value, result)
   Catch e As FormatException
      Console.WriteLine("Unable to convert '{0}' to a Double.", value)            
   Catch e As OverflowException
      Console.WriteLine("'{0}' is outside the range of a Double.", value)
   End Try 
Next        
' The example displays the following output: 
'       Converted '-1,035.77219' to -1035.77219. 
'       Unable to convert '1AFF' to a Double. 
'       Converted '1e-35' to 1E-35. 
'       Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24. 
'       Converted '-17.455' to -17.455. 
'       Converted '190.34001' to 190.34001. 
'       '1.29e325' is outside the range of a Double.

Open in new window


I integrated it into my page like this:

<%@ Page Language="VB" %>

<script runat="server">
    Sub page_load(ByVal obj As Object, ByVal e As EventArgs)
        Dim values() As String = {"-1,035.77219", "1AFF", "1e-35", _
                            "1,635,592,999,999,999,999,999,999", "-17.455", _
                            "190.34001", "1.29e325"}
        Dim result As Double

        For Each value As String In values
            Try
                result = Convert.ToDouble(value)
                Console.WriteLine("Converted '{0}' to {1}.", value, result)
            Catch e As FormatException
                Console.WriteLine("Unable to convert '{0}' to a Double.", value)
            Catch e As OverflowException
                Console.WriteLine("'{0}' is outside the range of a Double.", value)
            End Try
        Next
        ' The example displays the following output: 
        '       Converted '-1,035.77219' to -1035.77219. 
        '       Unable to convert '1AFF' to a Double. 
        '       Converted '1e-35' to 1E-35. 
        '       Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24. 
        '       Converted '-17.455' to -17.455. 
        '       Converted '190.34001' to 190.34001. 
        '       '1.29e325' is outside the range of a Double.

    End Sub
    </script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


When I try to view this code in browser in Visual studio Express for webb (2008 and 2012) I get this error

I get the following error

--------------------------------------------------------------------------

Kompilatorfelmeddelande: BC30616: variable e hides a variable in an enclosing block
Source:

 

Rad 12:                 result = Convert.ToDouble(value)
Rad 13:                 Console.WriteLine("Converted '{0}' to {1}.", value, result)
Rad 14:             Catch e As FormatException
Rad 15:                 Console.WriteLine("Unable to convert '{0}' to a Double.", value)
Rad 16:             Catch e As OverflowException
 
---------------------------------------------------------------------------

I have read here about the problem but none of the suggestions leads me further:


http://msdn.microsoft.com/en-us/library/wtk40des(v=vs.110).aspx

      •A common cause for this error is the use of Catch e As Exception inside an event handler. If this is the case, name the Catch block variable ex rather than e.

•Another common source of this error is an attempt to access a local variable declared within a Try block in a separate Catch block. To correct this, declare the variable outside the Try...Catch...Finally structure.
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

change this code:
For Each value As String In values
            Try
                result = Convert.ToDouble(value)
                Console.WriteLine("Converted '{0}' to {1}.", value, result)
            Catch e As FormatException
                Console.WriteLine("Unable to convert '{0}' to a Double.", value)
            Catch e As OverflowException
                Console.WriteLine("'{0}' is outside the range of a Double.", value)
            End Try
        Next

Open in new window


to this code:
For Each value As String In values
            Try
                result = Convert.ToDouble(value)
                Console.WriteLine("Converted '{0}' to {1}.", value, result)
            Catch ex As FormatException
                Console.WriteLine("Unable to convert '{0}' to a Double.", value)
            Catch ex As OverflowException
                Console.WriteLine("'{0}' is outside the range of a Double.", value)
            End Try
        Next

Open in new window

the reason for this error is because the page_load handler has argument: ByVal e As EventArgs, which clashes with the exception e instance in your try/catch
Avatar of marcgu

ASKER

Hi!

I followed your example but, now I, only get a blank page. I am very sorry, but I would really apprecitate if I could get it to work.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of marcgu

ASKER

Thanks a lot