如何添加内联 VB.NET 方法?

问题描述 投票:0回答:3

由于我无法将 .vb 代码隐藏文件添加到我的 .aspx 页面,如here所述,我尝试添加“内联”方法,如下所示(“ConvertSubcategoryIndex”函数是新位代码):

<%
        . . .  
    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
%>

但这行不通,但我明白了:

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

注意: 无论我将函数放在上面还是下面,我都会得到这个 End If 'If Save Action

更新

基于某人的别名(NoAlias),我尝试在顶部添加一个部分,代码如下:

. . .
</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")
. . .

...但这没有什么区别;我仍然得到,“语句不能出现在方法体内。假定方法结束。”

更新2

即使使用 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    

...我收到与以前相同的错误消息:

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 

更奇怪的是,Visual Studio 发现代码第一行有问题,并提出了一个奇怪的建议,即我在此处添加一个“End Sub”:

我尝试过这样做,只是为了讨厌,但正如可能怀疑的那样,这在其他地方引发了自己的错误。

更新3

基于这两个答案(tgolisch 正在修复代码本身,Sam Axe 正在展示如何将其添加到组合中),它现在正在运行。通过这些修复和对我的逻辑的修复,在其工作状态下它是:

<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>
asp.net vb.net function methods
3个回答
1
投票

我已经很久没有使用过 WebForms...但是如果没记错的话你需要使用类似的东西:

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

1
投票

您的 VB.NET 语法存在多个语法错误。尝试将此代码粘贴到您的

ConvertSubcategoryIndex()
函数中:

<%
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    
%>

您可能还需要查看第一个示例顶部的代码质量。我不知道你把

...
放在哪里发生了什么。


0
投票

当我想在纯 VBScript 中显示带有前导零的日期值(来自 UNIX 时间戳)时,我现在遇到了同样的问题。我定义了函数来进行一些格式化,但也收到了 BC30289 编译器消息。 然后我将完整的编译器输出变成红色,喝了一杯咖啡,然后......



    Dim ts, td, t
    ts = "1690008848"
    td = DateAdd("s", ts, "01/01/1970 00:00:00")
    t = DatePart("yyyy", td) & "-"
    t = t & DatePartFormatter(td, "m") & "-"
    t = t & DatePartFormatter(td, "d") & " "
    t = t & DatePartFormatter(td, "h") & ":"
    t = t & DatePartFormatter(td, "n") & ":"
    t = t & DatePartFormatter(td, "s")
    Response.Write(resp & ", " & t)
    
    
    ' Here comes a small trick, after examining the full compiler output
    ' ASP pages run inside a Sub started by the system; in my case, this is called as
    '  Private Sub __Render__control1(ByVal __w As System.Web.UI.HtmlTextWriter, ByVal parameterContainer As System.Web.UI.Control)
    ' Everything in your ASP page code is inside this system Sub, that is why you get the error message of
    '  "Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed."
    ' when you try to define your own Subs or Functions to make your life easier.
    ' Fortunately, VB compiler is a good partner for a trick, because it only needs an exact string at a good position.
    ' That means: we simply have to close the system-opened Sub at the end of the page code, then we can write our Subs or Functions there,
    ' and finally we open an empty Sub, so that the system can close it instead of the original one, without reclaiming about anything.
    ' And, that's it :-)
    
    ' here we close the __Render_control1 system Sub...
    End Sub
    
    ' ...declare our program pieces, ...
    Function DatePartFormatter(d as Date, part as String)
        Dim var
        var = CStr(DatePart(part, d))
        If Len(var) = 1 Then
            var = "0" & var
        End If
        DateFormatter = var
    End Function
    
    ' ... and finally open a Sub, but let the system close it, after reading in the page code (by the compiler) ends completely
    Sub CompletelyDummyThing
    %>

虽然有点丑,但是很管用。如果没有,请检查编译器输出中 ASP 代码的父级,并与之对齐。

© www.soinside.com 2019 - 2024. All rights reserved.