Public shared function question

A

AlBruAn

I have four classes named CheckVoucherReason, WriteOffReason,
CheckVoucherApproval and WriteOffApproval. In CheckVoucherApproval's
function to load values from the db prior to displaying them in the UI, I
make a call to a public shared function in CheckVoucherReason to return the
verbose description based on the reason ID; the call works as planned. In
WriteOffApproval's function to load values from the db, I'm attempting to
make a call to a public shared function in WriteOffReason to return the
verbose description of the reason; however, in this case it won't even
compile. Instead, I'm getting a "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" error message.

My function in CheckVoucherReason is defined as:
Public Shared Function RetrieveByID(ByVal reasonID As Integer) As
CheckVoucherReason

My function in WriteOffReason is defined as:
Public Shared Function RetrieveByID(ByVal reasonID As Integer) As
WriteOffReason

So, they are essentially the same ... differing only in what they return.

My call to the CheckVoucherReason function is:
Dim reason As String = CheckVoucherReason.RetrieveByID(reasonID) contained
within a public shared function in CheckVoucherApproval.

My call to the WriteOffReason function is:
Dim reason As String = WriteOffReason.RetrieveByID(reasonID) contained
within a public shared function in WriteOffApproval.

Usage of the CheckVoucherApproval class or the WriteOffApproval class is
made by the web page the user has displayed.

So, my question is this: why does the function call for WriteOffApproval
throw an error while the function call for CheckVoucherApproval doesn't?
Logically, either both should work or both should fail, but that's apparently
not the case here.
 
J

Jeroen Mostert

AlBruAn said:
I have four classes named CheckVoucherReason, WriteOffReason,
CheckVoucherApproval and WriteOffApproval. In CheckVoucherApproval's
function to load values from the db prior to displaying them in the UI, I
make a call to a public shared function in CheckVoucherReason to return the
verbose description based on the reason ID; the call works as planned. In
WriteOffApproval's function to load values from the db, I'm attempting to
make a call to a public shared function in WriteOffReason to return the
verbose description of the reason; however, in this case it won't even
compile. Instead, I'm getting a "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" error message.
The compiler is always right. So find out which instance member you're using
and make it Shared.
So, my question is this: why does the function call for WriteOffApproval
throw an error while the function call for CheckVoucherApproval doesn't?
Logically, either both should work or both should fail, but that's apparently
not the case here.
One function is not using an instance member. The other is. Since you
haven't posted relevant code but only what you *think* is the problem, it's
hard to say where it's going wrong, exactly. You're fixating on the wrong
location.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top