Linq: Namespace in code before unknown - asp.net

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??

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).

Access a local class

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.

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...

call function within a namespace in ascx.vb page from the ascx page

I have a function in my ascx.vb page that i need to be called from the ascx page.
However, I get an error that says its "not declared and may be inaccessible due to its protection level".
I am thinking that for some reason the functions of my ascx.vb page cant be seen from my ascx page.
I actually have this code working in aspx pages without a namespace. Anyone know what is causing this error?
The ascx.vb namespace portion reads like:
Namespace StaffLookup
Public Class Main
Inherits PortalModuleBase
Then the function in ascx.vb is like this:
Public Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
The call in ascx is like this:
<img src='<%# ProcessPictures()%>' />
The only way I have been able to reproduce this error is if there is a second class with the same namespace, class name and method name where the second method has private scope.
The compiler is merging these two classes and the private version of the method ends up being the one used, so you end up with the error you are seeing. There will be warning messages on the class name and method name.
Check your code to see if you have another class with this same namespace and class name, and then see if it has a method with the same signature.
Also check the parent classes up the inheritance chain starting with PortalModuleBase. You can stop looking once you get to UserControl.
Like this:
Namespace StaffLookup
Public Class Main
Inherits PortalModuleBase
Public Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
End Class
End Namespace
Namespace StaffLookup
Public Class Main
Private Function ProcessPictures() As String
Return "http://info/scripts/personnel/IDVerify/BadgePictures/transparent.jpg"
End Function
End Class
End Namespace
Come to find out, the answer is here:
Asp.net controls are not accessible in code behind
Even when creating a brand new empty application you have to right click and choose convert to web app. This worked for me

ASP.NET visual basic undefined functions

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.

Resources