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.
Related
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).
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??
Currently, if I have different classes containing functions etc, I would structure them all in the 1 VB file titled whatever the main Class is. For example:
MainClass.vb:
Class MainClass
Function FunctionA() as String
...
End Function
Class SubClass
Function SubFunction() as String
....
End Function
Class SubSubClass
......
End Class
End Class
End Class
However, the problem with this is the file can become 1000s of lines long making hard to find portions of code.
Is there a way I can store SubClass in a file such as MainClass.SubClass.vb so that its easier to locate classes? Or is there a better, more standard, way of doing this?
At least C# understands "partial classes" where you can spread the code over multiple files (within the same assembly)
EDIT
I expected VB to have partial classes also (I only work in C# myself), but as noted in the comments, it does have them.
In C# I could code something like this:
File MainClass.cs:
public partial class MainClass // note the "partial" keyword
{
// some method declarations etc.
}
File MainClass.SubClass.cs:
public partial class MainClass // have to repeat this to hook up correctly
// but an "Implements" or "Extends" could be different
{
// maybe some other methods
private class SubClass
{
// etc.
}
}
But the real question is: do those subclasses really need to be inner classes of that "MainClass", or could they be top-level classes themselves?
Instead of packaging all classes into 1 class you should use multiple classes and Namespaces to store and use your code. Especially with larger Projects this can help you organize your code.
Note: The name of a Namespace may not be the same as one of class. But you can use the same Namespace for multiple classes.
i.e.
Create a "MainClass.vb"
Create a "SubClass.vb"
Code "SubClass.vb"
Namespace Main
Public Class SubClass
Function SubFunction() as String
....
End Function
End Class
End Namespace
So you can access the Class SubClass by "Main.SubClass"
If you have a lot of classes and files you could i.e. create for each Namespace a SubFolder
Main
|-- SubMain
|---|-- SubSubMain
|-- AnotherSubMain
|---|-- SubAnotherSubMain
with the Namespaces
"Main"
"Main.SubMain"
"Main.SubMain.SubSubMain"
"Main.AnotherSubMain"
etc.
To store general functions which are often or commonly used you could also create a library project.
Yes, partial classes in vb.net will allow you to do exactly what you request.
MainClass.vb
Partial Class MainClass
Function FunctionA() as String
...
End Function
End Class
MainClass.SubClass.vb
Partial Class MainClass
Class SubClass
Function SubFunction() as String
....
End Function
End Class
End Class
Leaving aside questions of re-architecting and re-factoring, which is not the question you asked, with a very large class file, described as 1000s of lines long, there may be some additional quick-fix benefits in using even more partial class files to enact a separation of concerns. Use Partial to split the unwieldy class file into logically related bundles of code across multiple smaller .vb files.
How advisable, how effective, or how dirty, this action is is obviously debatable, but the facility does exist, and it would therefore be up to you to take a view on how beneficial/problematic this partitioning option may or may not prove to be.
http://en.wikipedia.org/wiki/Partial_class
This has got to be one of the most frustrating issues with asp.net. I first got the error parser error cannot load default and as I have done before checked if the naming was correct in th bin folder and made the changes accordingly.
Now I get the following error:
somethiing._DEfault is not allowed here because it does not extend class 'System.Web.UI.Page'
where something is the name of the project in the properties. I have the following code in Default:
Namespace FormDiscovery
Partial Class _Default
Inherits System.Web.UI.Page
Shared settings As New System.Configuration.AppSettingsReader
Shared connObj_Generic As New SqlConnection
Shared commObj_Generic As New SqlCommand
Shared count As Integer
Shared num As Integer
etc..
Now I have deleted the project and other in the solution and created a new one and still this error and if I take the 'something' name away I get the cannot load default error.
Seems to me like a problem with inheritance, have a look at this:
http://www.west-wind.com/Weblog/posts/3016.aspx
HTH
I've made an interface called ApprovalEvent in a separate file with the namespace myproject... I've also made a class called PurchaseOrder... it's also in the same namespace, just in a separate file. Whenever I try to make PurchaseOrder implement ApprovalEvent it always says that ApprovalEvent is undefined...
How can I make the class recognize that the interface exists but is in a different file?
Here's some code:
File #1
Namespace myproject
Public Interface ApprovalEvent
Function getDetails() As String
End Interface
End Namespace
File #2
Namespace myproject
Partial Class PurchaseOrder
Inherits Entities.Modules.PortalModuleBase
Implements ApprovalEvent
Function getDetails() As String Implements ApprovalEvent.getDetails
return "details"
End FUnction
End Class
End Namespace
Thanks,
Matt
Separate project too? Does the one project include the other as a reference if so? That shouldn't be a problem from what I can see. It works no problem. Try temporarily including the interface in the same file too, see if that helps.