473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Marshal-by-refrence Warning.

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.

Mar 30 '07 #1
3 14468
On Mar 30, 7:22 am, preitymathur0...@gmail.com wrote:
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

Mar 30 '07 #2
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.

Mar 30 '07 #3
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/lib...h4(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
Mar 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
3
by: kevin | last post by:
Hi I have a C struct that is of the following typedef struct{ DWORD num_conversions; ... short *sample_values; ....
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
6
by: william.thorpe.b | last post by:
I have recently switched from VS2003 to VS2005 and at the same time from V1 to V2 of the .NET Compact Framework. The target is a Windows CE 5.0 device and an ARMV4I processor. ...
5
by: Anurag | last post by:
I have been chasing a problem in my code since hours and it bolis down to this import marshal marshal.dumps(str(123)) != marshal.dumps(str("123")) Can someone please tell me why? when...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
2
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.