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

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

Related

VB.Net module behavior

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.

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

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

Is this ASP.NET Inherited Shared Function practice acceptable?

I have a bunch of different forms that I would like to create a base MustInherit class for. One function I would like them all to contain is a shared function called GetForms(). I know that you can't declare a shared function MustOverride so I did the following in my abstract class:
Public Shared Function GetForms() As List(Of OrderForm)
'to be overridden in child class'
Return Nothing
End Function
And this in my child class:
Public Overloads Shared Function GetForms() As List(Of OrderForm)
'do stuff'
End Function
Will this cause problems down the line, or is this an acceptable workaround? It has a smell to it, but it will enforce that all my forms include a shared GetForms function.
EDIT I realize that if this were possible with interfaces, I would use one, but you can't declare shared functions in interfaces and I would like to make sure that this is a SHARED function.
This has a smell because it creates a false expectation of the behavior of the code.
You mention that your reason for doing this is that 'it will enforce that all my forms include a shared GetForms function'. This is only partly true. Yes, they will all have the GetForms function, but you're not actually forcing the derived classes to implement their own version of it. If you forget to implement the function on one of them, you'll be calling the base version, and you won't get any sort of warning about it from the compiler.
That is the smell: it can't actually enforce the behavior that you want, but it creates an impression, at first glance, that it can. This will lead to headaches 6 months from now when you're adding a new Form type and you've forgotten the convention. You'll get no warning that something's wrong until you start getting bad results during testing.
If you want to enforce behavior, you have to do it using instance members; using MustOverride (abstract) functions or an interface.
You can have static (Shared) methods like that, but you can't enforce the implementation of them.
Each static method is local to it's class, you can't overload it in a child class or make it abstract (MustInherit). You have to use an instance method (non-static) to get the object oriented aspects that you want.
Yes, that does smell!
Looks like you should be using an interface instead.
Here is a vb.net article: http://www.developer.com/lang/other/article.php/939411
Why wouldn't you simply declare it as:
Public MustOverride Function GetForms() As List(Of OrderForm)?
Static methods aren't inherited, so the expectation of overriding is not something we want to encourage. In other words, I think you might be barking up the wrong tree here.

Resources