Marshal-by-refrence Warning.

P

preitymathur0422

I m working on a MDI application. Here on the parent form I had two
variables declared as

public int curUserID = 0, ViewUserID = 1;

the form is inherited by the "Form" class.
I m using these variables on all the other forms.
But this gives the warning - "Accessing a member on
'LabTest.frmSBTUSA.ViewUserID' may cause a runtime exception because
it is a field of a marshal-by-reference class"
From ur writing i know that making the variables static will solve the
problem.
But it i change the declaration statement from the above as

public static int curUserID = 0, ViewUserID = 1;


Now, this statement gives me a error instead of warning. The error is
- "Static member 'LabTest.frmSBTUSA.ViewUserID' cannot be accessed
with an instance reference; qualify it with a type name instead"


Many other variables are also declare along with these variables and
also used from all the other forms of the application. The marshal-by-
refrence error is not reported while using all those other variables.

Pls help me.
 
J

Jon Skeet [C# MVP]

I m working on a MDI application. Here on the parent form I had two
variables declared as

public int curUserID = 0, ViewUserID = 1;

the form is inherited by the "Form" class.
I m using these variables on all the other forms.
But this gives the warning - "Accessing a member on
'LabTest.frmSBTUSA.ViewUserID' may cause a runtime exception because
it is a field of a marshal-by-reference class"

I don't know much about that warning, but best practice frowns on
public variables.
I suggest you use properties instead. That should solve the issue.
From ur writing i know that making the variables static will solve the
problem.

Changing a variable from being an instance variable to a static
variable is a significant change - it's probably okay if you were
never going to have more than one instance of the form, but you should
really consider it carefully before using it just to get round a
warning.
But it i change the declaration statement from the above as

public static int curUserID = 0, ViewUserID = 1;

Now, this statement gives me a error instead of warning. The error is
- "Static member 'LabTest.frmSBTUSA.ViewUserID' cannot be accessed
with an instance reference; qualify it with a type name instead"

And it's absolutely rigth - you'd need:

MyFormName.ViewUserID instead.
Many other variables are also declare along with these variables and
also used from all the other forms of the application. The marshal-by-
refrence error is not reported while using all those other variables.

You've got public variables all over the place? Start using
properties...
To see why you get the warning for some variables but not others, I'd
need to see a short but complete program. See http://pobox.com/~skeet/csharp/complete.html
for what I mean by that.

Jon
 
P

preitymathur0422

Thanx for ur reply.
From my next application i will practice using properties instead of
using public variables.

Right now,
i will try to solve this issue by using properties for these
variables.

Thanx.

Regards,
-Preeti.
 
R

Raaj

I m working on a MDI application. Here on the parent form I had two
variables declared as

public int curUserID = 0, ViewUserID = 1;

the form is inherited by the "Form" class.
I m using these variables on all the other forms.
But this gives the warning - "Accessing a member on
'LabTest.frmSBTUSA.ViewUserID' may cause a runtime exception because
it is a field of a marshal-by-reference class"
The compiler is trying to complain "somewhere in your code you are
tying to access a value type on a marshalbyref type". Refer to this
KB on how to resolve this http://msdn2.microsoft.com/en-us/library/x524dkh4(vs.80).aspx

Right now,
i will try to solve this issue by using properties for these variables.

You got this resolved by emitting the underlying data member as
property because value type fields in a marshalbyref object are
serialized/deserialized from one application domain to another either
though the method calls or via properties.

I suppose the object that represents marshalbyref in your code is the
form itself, because the form derives from marshalbyref type.

Raaj
 

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