title: How can I add an inline VB.NET method?
body: <p>Since I'm unable to add a .vb code-behind file to my .aspx page, as delineated <a href="https://stackoverflow.com/questions/43327349/how-can-i-add-a-code-behind-file-to-an-aspx-page">here</a>, I'm trying to add the method "inline" like so (the "ConvertSubcategoryIndex" function is the new bit of code):</p>
  
  <pre><code>&lt;%
          . . .  
      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
  %&gt;
  </code></pre>
  
  <p>That doesn't work, though, and I get this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>NOTE:</strong> I get this whether I put the function above or below <em>End If 'If Save Action</em></p>
  
  <h2>UPDATE</h2>
  
  <p>Based on somebody's alias (NoAlias), I tried adding a section at the top with the code like so:</p>
  
  <pre><code>. . .
  &lt;/head&gt;
  
      &lt;%
      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    
          %&gt;
  
  &lt;%
      Unit = Request.QueryString.Item("Unit")
  . . .
  </code></pre>
  
  <p>...but it made no difference; I still get, "Statement cannot appear within a method body. End of method assumed."</p>
  
  <h2>UPDATE 2</h2>
  
  <p>Even with the improved code from tgolisch:</p>
  
  <pre><code>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    
  </code></pre>
  
  <p>...I get the same err msg as before:</p>
  
  <pre><code>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:     &lt;%
  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 
  </code></pre>
  
  <p>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:</p>
  
  <p><a href="https://i.stack.imgur.com/8Cftk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Cftk.png" alt="enter image description here"></a></p>
  
  <p>I tried that, just for yuks, but as might be suspected, that raised an err of its own, elsewhere.</p>
  
  <h2>UPDATE 3</h2>
  
  <p>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:</p>
  
  <pre><code>&lt;script runat="Server"&gt;
      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
  &lt;/script&gt;
  </code></pre>