Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Rob Dob
Guest
 
Posts: n/a
#1: Mar 30 '06
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;







Michael Nemtsev
Guest
 
Posts: n/a
#2: Mar 30 '06

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


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


Nick Hounsome
Guest
 
Posts: n/a
#3: Mar 30 '06

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



"Michael Nemtsev" <nemtsev@msn.com> wrote in message
news:9cc1c863861838c821f8f2ae177a@msnews.microsoft .com...[color=blue]
> Hello Rob,
>
> What's the reason to use struct for this?[/color]

[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.


Rob Dob
Guest
 
Posts: n/a
#4: Mar 30 '06

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


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" <nemtsev@msn.com> wrote in message
news:9cc1c863861838c821f8f2ae177a@msnews.microsoft .com...[color=blue]
> 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
>
>[/color]


Rob Dob
Guest
 
Posts: n/a
#5: Mar 30 '06

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


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.




Closed Thread