Reputation: 1984
I'm converting a bunch of foxweb programs to asp.net. Some of the functions I invoke in the asp code use "external functions," by which I mean functions that I have defined in .vb files. For example, FileExists() is a nice function I would like to pull out into a common thing called clsCommon.vb .
I have implemented it like this:
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
I have tried using both DIR$() and DIR(). In each case, the error returned on the web page reads:
Compiler Error Message: BC30451: Name 'Dir' is not declared.
As with other functions I have written I invoke FileExists() something like this:
<%@ 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
%>
Note 1: While I want to solve this specific problem, what I'm really looking for is the general way to get to these functions like DIR(), CHR(), etc., that I have come to rely on in VB.
Note 2: asp seems to only look at the vb text file - and not at the compiled dll file, so I don't think the references I use have any effect on it.
Anyone see what I'm missing?
Upvotes: 1
Views: 1843
Reputation: 1984
Solution: Figure out what function / method I need & search for it on msdn When I find the function as in: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.chr.aspx There will be a line that says, for example,
Namespace: Microsoft.VisualBasic
Use that name with an "Imports" near the beginning of the VB file, before the class definition as follows:
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.VisualBasic was required by the CHR() function and System.IO was required by the File.Exists() function.
Upvotes: 0
Reputation: 15091
TheGeekYouNeed is certainly right. The best approach is to either keep your code in VB (if it ain't broke, don't fix it) or consider investing some time in learning .Net
I have seen code conversion tools for turning VB code into VB.Net. I can't imagine them working for non-trivial projects though. Likewise, you can go out of your way to keep your code as 'VB like' as possible, but I think it's like burning down your house to avoid having to sweep the floor.
Anyway, the DIR function does still exist in the Microsoft.VisualBasic
namespace.
http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.71).aspx
The more generally accepted way of doing this in .NET would be to use File.Exists
http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Upvotes: 3
Reputation: 7539
You are using VB.Net ... not VB. There are differences, and you need to use the .Net framework appropriately.
Programming is always a lesson of learning.
Upvotes: 1