None Shared member requires an object reference - vb.net

I'm having a problem because I changed my code from vb6 to .net and I cant seem to sort out this issue I am having. Please assist.
The error message I'm getting:
Error 5 Reference to a non-shared member requires an object reference.
This is happening quite a few places in the code. This is my code. The problem is where it says FrmInvItem.Inv.`
Option Strict Off
Option Explicit On
Public Class ClsInv
Public Function RunProcess(ByVal ConnectStr As String, ByRef Parstr As String) As Integer
Dim frmInvIt As frmInvItem
RunProcess = frmInvItem.Inv(ConnectStr, Parstr)
frmInvIt.Close()
End Function

I assume Inv is not shared, but you are calling it without an instance of the class in which it is. So you either have to make it shared or create an instance of frmInvItem:
Dim frmInvIt As New frmInvItem() ' create instance
RunProcess = frmInvIt.Inv(ConnectStr, Parstr) ' use it on this instance
frmInvItem is the class, you can call a method via classname only if the method is shared.
Shared procedures are class methods that are not associated with a
specific instance of a class. For example, the Cos method defined
within the Math class is a shared method. You can call a shared
procedure as a method of an object or directly from the class.

You are running a method called Inv on a the frmInvIt without creating an instance.
You should create an instance to run your method against:
http://msdn.microsoft.com/en-us/library/77s47661.aspx

Related

Using interface function defined in DLL

I'm using an interface defined in a DLL.
When I call...
m.GetMasterVolumeLevelScalar(btVol)
... I get a Null Reference Exception because "m" is nothing.
However, I can't use "new" on this interface.
How would I use this interface correctly?
I did read on implements, but I didn't find an example similar to this interface.
Thank you.
Edit: I know now that I need to type
Implements Vannatech.CoreAudio.Interfaces.IAudioEndpointVolume
and the functions will automatically be added to my class.
However, I'm not sure what to do with the NonImplementedException for example here:
Public Function GetMasterVolumeLevelScalar(ByRef level As Single) As Integer Implements IAudioEndpointVolume.GetMasterVolumeLevelScalar
Throw New NotImplementedException()
End Function
I got it:
I simply need to type
Implements Vannatech.CoreAudio.Interfaces.IAudioEndpointVolume
By doing that, all functions will automatically be added to the class in which I typed this.
I just didn't scroll down enough to see that.

best matching overloaded method is not being called

I have the following methods:
Public Function RenderRateTable() As String
Private Function RenderRateTable(ToddVersionedObject As ToddVersionedObject,
FieldInfo As FieldInfo) As String
Private Function RenderRateTable(ArrayIndexes As List(Of ArrayIndexesAttribute.ArrayIndex),
ThreeDimensionalArray As ThreeDimensionalDecimalArrayType) As String
Private Function RenderRateTable(ArrayIndexes As List(Of ArrayIndexesAttribute.ArrayIndex),
TwoDimensionalArray As ArrayOfDecimalArraysType) As String
Note that the types that I'm overloading (ArrayOfDecimalArraysType, ThreeDimensionalDecimalArrayType) are not classes that inherit from the same base class. They are different structures that don't inherit from anything. They were written by someone else and I can't change them.
When I call it from within the same class like this
Dim MyThreeDimensionalDecimalArrayType As ThreeDimensionalDecimalArrayType
RenderRateTable(MyArrayIndexes, CType(MyThreeDimensionalDecimalArrayType, Object))
it doesn't go to the right method. It just goes to RenderRateTable().
What's even stranger is, the call is made with two arguments (parameters), but these are (ignored?) and it calls a method that takes no parameters and no run-time error is thrown!
I have Option Strict Off and the variable I'm passing to the parameter ThreeDimensionalArray is of type Object.
I'm trying to get it to where I have a bunch of overloads and it picks the right method based on the type of the Object passed.
Polymorphic method calling instead of an Select Case statement. I hate conditional blocks like that.
UPDATE
I got the code working by declaring the overloads Public, but I still don't understand:
Why the dispatcher wouldn't find the right Private method when the
call is within the same class.
Why the dispatcher would call a method with no parameters when the call is made with 2 parameters, and not throw an error.
You have RenderRateTable() as Public and the rest as Private, which will prevent it from being able to use the correct overload when called from outside the class.
It is very likely that Option Strict On would have pointed out that as a problem: I recommend that you use it to make your programming endeavours easier :)

Can't access methods in DLL

Using asp.net/vb.net. Created a solution with 2 projects, "MainProject" and "MyCommonStuff". The 2nd project ("MyCommonStuff") is really a common utilities class, the resulting dll I hope to use for other projects as well.
MyCommonStuff is defined very simply....
Public Class MyCommonStuff Stuff
Public Shared Function GetInfo() as string
:
:
End Function
Public Shared Sub Test
:
:
End Sub
:
End Class
In MainProject I set a reference to this MyCommonStuff project.
I want to access some of the MyCommonStuff methods in my code. But for some reason the methods are not being recognized.
For example, in a button in the MainProject I tried this....
dim m as new MyCommonStuff
x = m.GetInfo()
Intellisense doesn't pick up any of the subs/functions for m. What am I doing wrong? Thanks!
The thing is that you've made your methods static ("Shared" in VB). You need to either remove the Shared keywords:
Public Function GetInfo() As String
':
':
End Function
Public Sub Test()
':
':
End Sub
or keep the Shared keywords and use it like this:
x = MyCommonStuff.GetInfo()
Here is some information about Shared members. Most notably:
Specifies that one or more declared programming elements are
associated with a class or structure at large, and not with a specific
instance of the class or structure.
In other words, if you want to use your methods from an instance of your MyCommonStuff class, e.g. m in m.GetInfo(), you need to leave the Shared keyword off. If, on the other hand, you have a method that is common across all instances of your class or for which you don't even need an instance, you would use the Shared keyword and access the method like I said above, e.g. MyCommonStuff.GetInfo().
Make sure you're importing you common stuff's namespace.

Implications of "Public Shared" Sub / Function in VB

Can anyone explain me in VB i need to use Public Shared Sub so it can be accessed from another form.
But what this "Public" and "Shared" means?
Who is public?
With who is shared?
If it is public shared does this means some other software or "some hacker app" can easier have access to this sub and it's values?
In VB.NET, Shared is equivalent to static in C# - meaning the member belongs to the class, not an instance of it. You might think that this member is 'Shared' among all instances, but this is not technically correct, even though VB.NET will resolve a Shared member though an instance invocation.
public class1
public shared something as string
public somethingelse as string
end class
The following code illustrates how VB.Net allows you to access these:
...
class1.something = "something" 'belongs to the class, no instance needed
dim x as new class1() with {.somethingelse = "something else"}
Console.WriteLine(x.somethingelse) 'prints "something else"
Console.Writeline(class1.something) 'prints "something" <- this is the correct way to access it
Console.Writeline(x.something) 'prints "something" but this is not recommended!
...
Public means any linking assembly can see and use this member.
The Public accessor keyword simply means that the method, property, etc. is visible and callable from outside of the DLL or Assembly that defined it.
The Shared keyword means that the method, etc. is not "instanced". That is, it is part of the Class definition only, and not part of the objects that are created ("instanced") from that Class definition. This has two principal effects:
The Shared method can be called at anytime, without actually having an object/instance of that Class. and,
Shared methods cannot access any of the non-Shared parts of the Class definition (unless an object instance is passed to it). They can only directly access the other Shared parts of the Class definition.

Using CreateControlCollection() from within a WebMethod

my code calls a WebMethod. Within that WebMethod I have the need to create a new control collection. In VB I would normally write:
Dim cc As System.Web.UI.ControlCollection = CreateControlCollection()
But because I'm inside a WebMethod, I get the error :
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Any suggestions please ?
Thanks folks !
The error message implies you are trying to access an instance-level member or method from a shared method. If you want to access an instance-level method or member from a shared method, first you must create an instance using the new operator and you can access its public members and methods from there on.