ASP.NET visual basic undefined functions - asp.net

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?

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

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.

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.

Related

Testing an HTML Helper

I got my first HTML helper to work, very exciting.
It is working and inserting HTML into a VIEW, so I have managed to learn that much. However, the HTML is not perfect.
I would like to create unit tests to verify the output of the helper.
I have created a new test project , the code for the single test follows
Imports System.Text
Imports System.Web
Imports System.Web.Mvc
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports mvcUtils
Imports mvcUtils.Helpers
<TestClass()> Public Class testBoundField
Inherits baseTestClass
<TestMethod()> Public Sub FormRowHelper_test()
Dim html1 As String = Html.FormRowHelper("controlId")
Assert.AreEqual("perfect html", html1)
End Sub
End Class
The mvcUtils.Helpers is the namespace where we find the helper.
I have dumbed down the HELPER call to focus on the issue. Again, it is working in the MVC project.
There error I am getting is
'FormRowHelper' is not a member of Html
I think I am missing an include. But I am having troubles seeing it.
Any help is appreciated. Thanks.
I think what that is doing inside your test is trying to map to an HTML namespace, not the helper. In your test, you have to construct the HtmlHelper class:
Dim helper As New HtmlHelper([params])
helper.FormRowHelper("controlId")
I don't know what it takes to construct the helper class; you may have to mock the instances of the classes it wants (with TypeMock or Moq or something else).

New class (and related methods, etc...) wont appear in intellisense

I added a new class file to the app_code directory. For some reason though I can't access it through other pages. Any ideas why?
Code:
Public Class PDFHelper
Public Sub New()
End Sub
Public Shared Function GetFormFieldNames(pdfPath As String) As Dictionary(Of String, String)
...
The following code produces error "PDFHelper is not declared. It may be inaccessible due to its protection level."
Code:
Dim formFieldMap = PDFHelper.GetFormFieldNames(pdfPath)
You probably need to import your PDFHelper class, that or use the fully qualified name to access the method:
NameSpace.Class.Method
instead of
Class.Method
I created the class from scratch and all of a sudden it worked. Very weird. Same code.
It turns out that there was something weird in the class. I recreated the class (using the same code, which is confusing) and all of a sudden saw compile errors. I addressed those and then I was able to access the class, methods, etc...

Linq: Namespace in code before unknown

If I have something with a LINQ extension like
// Declaration: Code Behind
Protected MyList As IList(Of Object)
// Code Before
MyList.First()
in code behind, VS is always complaining something like 'First' is not a member of 'System.Collections.Generic.IList {...}. Everything works though, I just would like to get rid of these annoying errors.
I tried:
<%# Import namespace="System.Linq" %>
In my ASPX pages but it didn't help.
I also tried the following, none of which worked:
Add the namespace in the pages-node of web.config
Imports System.Linq in code behind
P.S.: Resharper doesn't mark it as error, interestingly...
You've probably resolved your problem long ago but, for the next person:
The message you're getting is correct but not helpful: "First" is, in fact, not a member of the List class. First is, however, an extension method that attaches itself to the List class (or any class that implements the IQueryable interface, as the List class does).
Unfortunately, the class containing the First method is in the System.Linq namespace and the compiler is unable to find that class unless you provide a namespace directive (Import/using statement) to your code. If you add either of these statements to your code file, your problem should go away:
Imports System.LINQ
Using System.LINQ
Adding the declaration to your .aspx file won't help, though.
I have test below code using " System.Collections.Generic " namespace and it's working fine...
List<int> intList = new List<int>();
intList.Add(5);
intList.Add(10);
intList.Add(15);
intList.Add(20);
intList.Add(25);
int x = intList.First<int>();
int y = intList.Last<int>();
is this fulfilled your requirement??

Check if Variable is declared in a page?

I have some web pages that include other pages, and I need to check if a variable (a string) has been declared in the page or not.
I was exploring try catch and finally, but im always getting a compiler error saying the variable doesnt exits.
syntax in my head is:
if variable(exists) then
do something
else
do nothing
end if
From what im finding is this wont even compile if the variable wasnt defined anywhere. I kinda knew that, I was just hoping to find some kind of work around. :/
The only way I know of is to use reflection...
This will not work for variables defined within subs/functions...
Friend Function VariableExists(ByVal variableName As String) As Boolean
For Each tField As FieldInfo In Me.GetType.GetFields
If tField.Name.ToLower() = variableName.ToLower() Then
Return True
End If
Next
Return False
End Function
Cose here is untested and may contain minor errors. Think of it more like pseudocode.
Instead of declaring a variable, how about adding a simple Interface that you can use in the pages that need to be processed a specific way, then you can test whether or not the page implements the Interface?
For example:
Public Interface IMySpecialInterface
End Interface
In the pages that you want special behavior for:
Public Page MySpecialPage
Implements IMySpecialInterface
End Page
In the code that processes the pages:
If TypeOf Me.Page Is IMySpecialInterface Then
What about using an interface:
Public Interface ISpecialProp
Property SpecialProp() As String
End Interface
Then you can test, if a class implements the interface or not using this code:
Dim spec = TryCast(obj, ISpecialProp)
If spec IsNot Nothing Then
Console.WriteLine(spec.SpecialProp)
End If

Include code in ASP.net

I've got an ASP.net file, and I'm trying to include dynamic code which was easy in classic ASP.
Here is what I have tried so far:
<%# Register TagPrefix="TagPre" TagName="header" Src="alg/classes.aspx"%>
and
<!--#include file="alg/classes.aspx"-->
But neither of these seem to work. The content of classes.aspx is:
<script runat="server">
' Square class
Public Class square
Public sqRows As Integer 'Numbers of rows this square has
Public sqCols As Integer 'Number of columns this square has
Public sqArray(,) As Integer 'The square array
' Initialise square array to match size of canvas
Public Sub initSqArray(ByVal canvCols, ByVal canvRows)
ReDim sqArray(canvCols, canvRows)
sqRows = canvRows
sqCols = canvCols
End Sub
End Class
Thanks for any help!
What is the concrete problem or error you got? When you are using .aspx, did you already try to put the VB-Code in the Code-Behind-Sheet and pull it from there?
You should not need to use include or register to access a class. You just need to save your class to a class file (.vb) and put the class in your app_code directory (if you are using a website project) or put it anywhere in a web application project (preferrably a folder for classes) but include it in your project namespace. This should make your class visible anywhere in the website or web application.

Resources