By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,204 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,204 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;


Mar 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 Nietzsche
Mar 30 '06 #2

P: n/a

"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
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.
Mar 30 '06 #3

P: n/a
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,
"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
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 Nietzsche

Mar 30 '06 #4

P: n/a
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.


Mar 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.