【问题标题】:ASP.NET visual basic undefined functionsASP.NET Visual Basic 未定义函数
【发布时间】:2011-07-07 15:48:30
【问题描述】:

我正在将一堆 foxweb 程序转换为 asp.net。我在 asp 代码中调用的一些函数使用“外部函数”,我指的是我在 .vb 文件中定义的函数。例如,FileExists() 是一个不错的函数,我想将其引入一个名为 clsCommon.vb 的常见事物中。

我是这样实现的:

Option Explicit On 
Option Strict On

Imports System
Imports System.Web.UI
Imports System.Web.UI.Page

Public Class clsCommon
    Inherits Page

    Public Shared Function FileExists(ByVal filename As String) As Boolean
        If Dir$(filename) <> "" Then
            Return True
        Else
            Return False
        End If
    End Function

End Class

我尝试过同时使用 DIR$() 和 DIR()。在每种情况下,网页上返回的错误都是:

编译器错误消息:BC30451:未声明名称“Dir”。

与我编写的其他函数一样,我调用 FileExists() 如下所示:

<%@ page  Debug="true" inherits="clsCommon" src="clsCommon.vb" %>
<%

Dim filename as String = "example.txt"

If clsCommon.FileExists(filename) then
  Response.Write(filename & " Exists")
else
  Response.Write(filename & " does not Exist")
end if

%>

注意 1:虽然我想解决这个特定问题,但我真正在寻找的是获得这些函数的一般方法,如 DIR()、CHR() 等,我已经开始依赖这些函数在 VB 中。

注意2:asp似乎只看vb文本文件——而不看编译后的dll文件,所以我认为我使用的引用对它没有任何影响。

有人知道我错过了什么吗?

【问题讨论】:

  • 你应该调用内置的File.Exists方法。

标签: asp.net vb.net


【解决方案1】:

TheGeekYouNeed 肯定是对的。最好的方法是将您的代码保留在 VB 中(如果它没有损坏,请不要修复它)或考虑花一些时间来学习 .Net

我见过将 VB 代码转换为 VB.Net 的代码转换工具。我无法想象他们为非平凡的项目工作。同样,您可以竭尽全力让您的代码尽可能“像 VB”,但我认为这就像烧毁房子以避免扫地一样。

无论如何,Microsoft.VisualBasic 命名空间中仍然存在 DIR 函数。 http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.71).aspx

在 .NET 中更普遍接受的方法是使用 File.Exists http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

【讨论】:

  • File.Exists 工作,只要我添加以下行:Imports System.IO 到 vb 代码的导入部分。
  • 另外,要使用 CHR() 函数,我需要添加以下行: Imports Microsoft.VisualBasic
【解决方案2】:

您使用的是 VB.Net ...不是 VB。有区别,需要适当使用.Net框架。

编程始终是一堂学习的课。

【讨论】:

    【解决方案3】:

    解决方案:弄清楚我需要什么功能/方法并在 msdn 上搜索它 当我找到如下功能时: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.chr.aspx 会有一行写着,例如,

    命名空间: Microsoft.VisualBasic

    在 VB 文件开头附近使用该名称和“Imports”,在类定义之前,如下所示:

    Option Explicit On 
    Option Strict On
    
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.Page
    
    ' The two critical lines follow:
    Imports System.IO
    Imports Microsoft.VisualBasic
    
    Public Class clsCommon
        Inherits Page
    
        Public Shared Sub TestExistence(ByVal filename As String)
            if NOT File.Exists(filename) then
                ' ... do something.
            end if
        End Sub
    
        Public Shared Function TestCHR(ByVal str As String) as string
            return str & chr(13) & chr(10)  'just an example
        End Function
    
    End Class
    

    CHR() 函数需要 MicroSoft.VisualBasic File.Exists() 函数需要 System.IO。

    【讨论】:

    • 进口并不是真正需要的。很高兴拥有。没有它,只要你完全限定命名空间,你仍然可以调用相同的函数。
    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多