Search code examples
asp.netvb.netfunctionmethods

How can I add an inline VB.NET method?


Since I'm unable to add a .vb code-behind file to my .aspx page, as delineated here, I'm trying to add the method "inline" like so (the "ConvertSubcategoryIndex" function is the new bit of code):

<%
        . . .  
    If Request.Form.Item("Action") = "Save" Then
        . . .
                        .Parameters.Add("@Subcategory", SqlDbType.NVarChar).Value = ConvertSubcategoryIndex(Category, Subcategory)
        . . .
    End If 'If Save Action

    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
        If _Category = "New"
            If _Subcategory = 0
                Return "New"
            End If
            Else If _Subcategory = 1
                Return "Assumed"
            End If    
        End If
        If _Category = "Existing"
            If _Subcategory = 0
                Return "Existing"
            End If
            Else If _Subcategory = 1
                Return "Organic"
            End If    
        End If
        Return "Unknown"
    End Function
%>

That doesn't work, though, and I get this:

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 274:    End If 'If Save Action
Line 275:    
Line 276:    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 277:        If _Category = "New"
Line 278:            If _Subcategory = 0

Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 276

NOTE: I get this whether I put the function above or below End If 'If Save Action

UPDATE

Based on somebody's alias (NoAlias), I tried adding a section at the top with the code like so:

. . .
</head>

    <%
    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
        If _Category = "New"
            If _Subcategory = 0
                Return "New"
            End If
            Else If _Subcategory = 1
                Return "Assumed"
            End If    
        End If
        If _Category = "Existing"
            If _Subcategory = 0
                Return "Existing"
            End If
            Else If _Subcategory = 1
                Return "Organic"
            End If    
        End If
        Return "Unknown"
        End Function    
        %>

<%
    Unit = Request.QueryString.Item("Unit")
. . .

...but it made no difference; I still get, "Statement cannot appear within a method body. End of method assumed."

UPDATE 2

Even with the improved code from tgolisch:

Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
    If _Category = "New" Then  'really need a "Then" for every "If"
        If _Subcategory = 0 Then
            Return "New"
        'End If  'this End If was killing the next line
        ElseIf _Subcategory = 1 Then
            Return "Assumed"
        End If
    End If
    If _Category = "Existing" Then
        If _Subcategory = 0 Then
            Return "Existing"
        'End If  'this one was causing problems too
        ElseIf _Subcategory = 1 Then   'ElseIf can't have a space between Else If
            Return "Organic"
        End If
    End If
    Return "Unknown"
End Function    

...I get the same err msg as before:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 63:     <%
Line 64:     'End Sub
Line 65:     Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 66:     If _Category = "New" Then  'really need a "Then" for every "If"
Line 67:         If _Subcategory = 0 Then

Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 65 

Weirder yet, Visual Studio finds a problem with the first line of the code, and makes the bizarre suggestion that I prepend an "End Sub" there:

enter image description here

I tried that, just for yuks, but as might be suspected, that raised an err of its own, elsewhere.

UPDATE 3

Based on both answers (tgolisch's fixing the code itself, and Sam Axe's showing how it needs to be added to the mix), it is now working. With those fixes and fixes to my logic, in its working state it is:

<script runat="Server">
    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As String) As String
        If _Category = "New Business" Then
            If _Subcategory = "0" Then
                Return "New"
            ElseIf _Subcategory = "1" Then
                Return "Assumed"
            End If
        End If
        If _Category = "Existing Business" Then
            If _Subcategory = "0" Then
                Return "Existing"
            ElseIf _Subcategory = "1" Then
                Return "Organic"
            End If
        End If
        Return "Unknown"
    End Function
</script>

Solution

  • I haven't done WebForms in ages... but if memory serves you need to use something like:

    <script runat="Server">
        Public Function Whatever() As String
            Return "something"
        End Function
    </script>