Error: may cause a runtime exception because it is a field of a marshal-by-reference

R

Rob Dob

Hi,

I have a global structure that I declare within form1.cs, and I need to
reference from within other forms, everything seems to work okay at
runtime,
Everything works okay however I get the following warning when I compile:

Warning 2 Accessing a member on 'MYCMS.Form1.GLCurrentUserInfo' may cause a
runtime exception because it is a field of a marshal-by-reference class
C:\MYCMS\Form2.cs

This is my code and how/where I use it..., I'm not sure if I am using the
correct approach to storing a retreiving variables that I require
globally..,, if not I am very open to suggestions.

thanks...

I create it and make use of it in form1.cs using the folloiw code:

public struct UserInfo
{
public string userid;
public string password;
public string userlevel;
public decimal nemployeeid;
};
public UserInfo GLCurrentUserInfo;

GLCurrentUserInfo.userid = "someUserID";


then within another form, lets say form2.cs I wish to retreive then value
of it using:

Form1 oForm = (Form1)ParentForm;
this.txtAddedBy.Text = oForm.GLCurrentUserInfo.userid;
 
M

Michael Nemtsev

Hello Rob,

What's the reason to use struct for this?

RD> I have a global structure that I declare within form1.cs, and I
RD> need to
RD> reference from within other forms, everything seems to work okay at
RD> runtime,
RD> Everything works okay however I get the following warning when I
RD> compile:
RD> Warning 2 Accessing a member on 'MYCMS.Form1.GLCurrentUserInfo' may
RD> cause a runtime exception because it is a field of a
RD> marshal-by-reference class C:\MYCMS\Form2.cs
RD>
RD> This is my code and how/where I use it..., I'm not sure if I am
RD> using the correct approach to storing a retreiving variables that I
RD> require globally..,, if not I am very open to suggestions.
RD>
RD> thanks...
RD>
RD> I create it and make use of it in form1.cs using the folloiw code:
RD>
RD> public struct UserInfo
RD> {
RD> public string userid;
RD> public string password;
RD> public string userlevel;
RD> public decimal nemployeeid;
RD> };
RD> public UserInfo GLCurrentUserInfo;
RD> GLCurrentUserInfo.userid = "someUserID";
RD>
RD> then within another form, lets say form2.cs I wish to retreive then
RD> value of it using:
RD>
RD> Form1 oForm = (Form1)ParentForm;
RD> this.txtAddedBy.Text = oForm.GLCurrentUserInfo.userid;
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
N

Nick Hounsome

Michael Nemtsev said:
Hello Rob,

What's the reason to use struct for this?

[cut]

LOL

Give up Rob :)

You should be starting to realize by now that in 3 weeks time you'll have no
new sorting solutions but you'll still be getting people jumping into this
thread to tell you not to use structs.

Just because a lot of people say something doesn't make it so.......except
in this case.
 
R

Rob Dob

Hi,

The reason for this struct is that I wish to assign user information read
from a database at login that will be available to the rest of the forms
throughout the application. I like using structs because it keeps
everything neat and tidy. I am new to programming in C#, I am much more
comfortable with vc++/MFC. Normally I would create a global variable, but
I don't really see, where, how I would do this. If this was a webform app
then I realize I could use a sessionid or use those new profile services. I
am looking for suggestions here. I don't think writing to the registry is
an option.

Anyway any, all help would be appreciated..

thanks,
 
R

Rob Dob

Hi,

its okay I figured it out.. all I need to do is change my declaration from:

public UserInfo GLCurrentUserInfo;

TO:

public static UserInfo GLCurrentUserInfo;

and then I can just use it as is without casting the parentform..

thanks.
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top