Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
We have a C# (2013) web app program that references a VB (2013) call. But when trying to access variable and properties we get an 'member ... cannot be accessed with an instance reference'

C# has
APIV3VS2013.APIV3Cl aCl = new APIV3VS2013.APIV3Cl();
.
aCl.TheAccessToken = TheAccessToken;
aCl.TheAccessTokenSecret = TheAccessTokenSecret

Gets errors on both of the above.

In the VB Dll we have


Public Shared Property TheAccessTokenSecret As String
Public Shared TheAccessToken As String

I have been playing with Property, Public, Shared, etc.

How should it be coded to not get the error?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Nov-14 11:59am    
In what line? Where is the definition of the type being accessed?
—SA
QuickBooksDev 17-Nov-14 8:45am    
On both of these
aCl.TheAccessToken = TheAccessToken;
aCl.TheAccessTokenSecret = TheAccessTokenSecret;

It's because of the
VB
Shared
keyword. In C# that translates into
C#
static
and as such it cannot be accessed on the instance reference. You'd have to access it like a static property in C#:
C#
APIV3VS2013.TheAccessToken = TheAccessToken; 
 
Share this answer
 
Comments
QuickBooksDev 17-Nov-14 8:59am    
That worked for those 2.
But as
aCl.TheAccessToken = TheAccessToken;
aCl.TheAccessTokenSecret = TheAccessTokenSecret

Not what you have which gives a
Member cannot be accessed withan instance reference...

But there were others as well.

aCl.TheDataService = TheDataService;
aCl.TheContext = TheContext;

In VB as (after I removed Shared)
Public TheContext As ServiceContext = Nothing
Public TheDataService As DataService = Nothing

But now the VB Doesn't compile. Get max errors with
Cannot ref to an instance member of a class from witin a shared member or shared member initializer without an explicit instance of the class.

On lines like
pOutRetObj = TryCast(TheDataService.Add(TermAdd), Term)
and
If TheContext Is Nothing Then
...
Michael Ulmann 17-Nov-14 17:39pm    
It's very difficult to judge whether shared/static is what you really need in your circumstances. I'd have to know much more about your project and what you are actually trying to achieve.
In general terms most of the time when designing a software following OO principles information wouldn't make much use of static but have the lifetime of an instance.

If you don't want to look into all this I suggest you make it static but not shared in VB and then use the static accessor in C# as I pointed out in my last suggestion.
You were correct.

I have removed the Static and it seems to work but I am trying to figure out the best way it should be coded going from Windows Desktop where there is only 1 users to a Web App where there will be more than 1 at the same time.

This would include:
1. OAuth Consumer Key and Secret which are fixed for the app and I assume static/shared.
2. OAuth Access Key and Secret which are per user and I assume not static/shared.

So is it correct that only Static/Shared variables are those that are unique to the App and anything else (mostly related to the user would) not be Static/Shared?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900