簡體   English   中英

ASP.NET visual basic未定義的函數

[英]ASP.NET visual basic undefined functions

我正在將一堆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文件,所以我不認為我使用的引用對它有任何影響。

有人看到我錯過了嗎?

TheGeekYouNeed當然是對的。 最好的方法是將代碼保存在VB中(如果沒有破壞,不要修復它)或考慮花一些時間學習.Net

我見過用於將VB代碼轉換為VB.Net的代碼轉換工具。 我無法想象他們為非平凡的項目工作。 同樣地,你可以盡可能地將你的代碼保持為'VB like',但我認為這就像燒毀你的房子以避免不得不掃地。

無論如何,DIR函數仍然存在於Microsoft.VisualBasic命名空間中。 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

您正在使用VB.Net ...而不是VB。 存在差異,您需要適當地使用.Net框架。

編程始終是學習的一課。

解決方案:找出我需要的函數/方法並在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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM