2011-03-02 63 views
1

我正在將一堆foxweb程序轉換爲asp.net。我在asp代碼中調用的一些函數使用「外部函數」,我的意思是我在.vb文件中定義的函數。例如,FileExists()是一個很好的函數,我想把它引入一個叫做clsCommon.vb的常用函數中。ASP.NET visual basic未定義函數

我已經實現了它這樣的:

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:雖然我想解決這個具體的問題,就是我真正需要的是一般的方法來獲得像我在VB中依賴的DIR(),CHR()等函數。注意2:asp似乎只查看vb文本文件 - 而不是在編譯後的dll文件中,所以我不認爲我使用的引用對它有任何影響。

任何人都能看到我失蹤的東西嗎?

+1

您應該調用內置的'File.Exists'方法。 – SLaks 2011-03-02 22:40:23

回答

3

TheGeekYouNeed肯定是對的。最好的辦法是要麼保持你的代碼在VB中(如果它沒有損壞,不要修復它)或考慮投入一些時間在學習.Net

我見過用於將VB代碼轉換爲VB的代碼轉換工具。淨。但我無法想象他們爲不平凡的項目工作。同樣,您可以儘可能讓自己的代碼儘可能保持爲'VB',但我認爲這就像燒燬你的房子以避免不必掃地。

無論如何,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

+0

只要添加以下行,File.Exists就可以工作:將System.IO導入到vb代碼的imports部分。 – elbillaf 2011-03-03 20:29:38

+0

此外,要使用CHR()函數,我需要添加以下行:Imports Microsoft.VisualBasic – elbillaf 2011-03-03 21:13:41

1

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

編程總是一個學習的教訓。

0

解決方法:找出我需要什麼功能/法&尋找它在MSDN上 當我發現功能如: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.chr.aspx 會有這樣一行,例如,

命名空間: Microsoft.VisualBasic程序

使用與「進口」附近的VB文件的開頭,類定義之前命名如下:

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 

的MicroSoft。CHR()函數 需要VisualBasic,並且File.Exists()函數需要System.IO。

+0

導入並不是真的需要。很高興有。沒有它,只要你完全限定名字空間,你仍然可以調用相同的函數。 – 2011-03-03 21:29:38