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).
Related
I am having a "weird" situation of my VB.Net modules, as per my understanding, Module in VB.Net means static class so I have implemented a couple of helper modules with couple of functions each, let's have some examples for better explanation (free hand code, may contains syntax problem):
Namespace Helpers
Module HelperA
Public Function FunctionA() As Boolean
Return True
End Function
End Module
End Namespace
Namespace Helpers
Module HelperB
Public Function FunctionB() As Integer
Return 1
End Function
End Module
End Namespace
When I start coding in Visual Studio and type Helpers., both FunctionA() and FunctionB() are show up in the recommended auto-complete dialog which I have not type HelperA or HelperB yet, I have some C#.Net projects with static class and I found such behavior does not apply to C#.Net static class.
It is weird to me and inconvenience since I am now having 50-ish functions under a single namespace, have done some Google but nothing could be find, could anyone suggest a solution (besides change Module to Class) or any keywords to search with?
Any help will be appreciate!
Module doesn't technically mean static class. Static in VB.net (with regard to functions) is Shared, and there is no Shared Class. What I think you want is a sealed/abstract/not-inheritable class with static/shared functions (you'll be able to call the functions without an instance of the parent class, but you'll still have to reference the parent class when calling the function). If that's the case, then do something similar to the following:
Public NotInheritable Class HelperA
Public Shared Function FunctionA() as Boolean
Return True
End Function
End Class
Having said that, the only difference I've found—at least for practical purposes—between a shared function and a module function is that module functions can be called without referencing the module.
I'm having trouble accessing a class (actually several classes) from other classes, and modules, all in the same vb.net (Asp.net) project.
Here is an example of one of the classes I cannot Import or access:
(This file is clsPitch.vb in the \App_Code folder)
Imports Microsoft.VisualBasic
Public Class clsPitch
Public MyStuff As String
End Class
In another class in the same project:
Dim ThisClass As clsPitch
This gives the following error:
"Type 'clsPitch' is not defined."
Imports MyProject.clsPitch
This gives the following error:
"Namespace or type specified in the imports 'MyProject.clsPitch' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases."
When I type "Imports.MyProject." none of these classes show up in intellisense.
I have not specified any NameSpaces (My understanding is everything should be in one NameSpace for the entire project).
What's strange is that I have another class that is basically defined the same, and it works fine (I can access it and import it).
Right click on the .vb file in the App_Code folder and take a look at its properties. Make sure the Build Action is set to Compile.
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...
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??
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.