473,397 Members | 2,099 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,397 software developers and data experts.

References without unsafe pointers

Hi Everyone,

Can someone show me how to go about assigning a ref parameter to a variable
so that when updates are made to this variable they are reflected in the
calling object's reference parameter. I want to be able to change the value
of the referenced variable outside of the constructor. eg.

private bool loggedIn;

//Constructor which is called from another form
public void LoginForm(ref bool log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn = true;
}

I want the 'log' variable back in the calling object to be whatever
'loggedIn' is changed to.

Hope this is clear.
Jesse
Nov 15 '05 #1
13 1306
well, you could set up an event handler in the calling application and
subscribe to events through by your login form. Then, when the user uses
the login form, you could throw an event to all event handlers, who could
update their local copies of the value.

HTH,
--- Nick

"Jesse" <je*****@hotmail.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
Hi Everyone,

Can someone show me how to go about assigning a ref parameter to a variable so that when updates are made to this variable they are reflected in the
calling object's reference parameter. I want to be able to change the value of the referenced variable outside of the constructor. eg.

private bool loggedIn;

//Constructor which is called from another form
public void LoginForm(ref bool log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn = true;
}

I want the 'log' variable back in the calling object to be whatever
'loggedIn' is changed to.

Hope this is clear.
Jesse

Nov 15 '05 #2
Jesse <je*****@hotmail.com> wrote:
Can someone show me how to go about assigning a ref parameter to a variable
so that when updates are made to this variable they are reflected in the
calling object's reference parameter. I want to be able to change the value
of the referenced variable outside of the constructor.


You can't, thank goodness. It would make for horrendously
unmaintainable code, with the values of potentially private variables
being changed outside the class in non-obvious ways, etc. Also consider
that the variable may not even *exist* any more by the time the second
call is made.

What you *could* do is create a reference type which contains a
boolean. Pass that into your method and keep a reference to it, and
then change the value of the boolean "inside" the type at a later date.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hey Jon & Nick,

Thanks for the feedback.

What i want to do is have a reuseable loginForm. When the login button on
this loginForm is clicked, the users validility is checked and if all is ok,
the calling form (main form), which will have a boolean for if the user is
logged on or not needs to be updated. How else can you have a reuseable
loginForm if not to be able to update the parent form of this true or false?

I think nick has a good read on it.

Jon could you explain what you mean a bit further (possibly with example)
with regard to:

What you *could* do is create a reference type.
How do i do that?

do you mean:
bool b;
b = new Boolean();

which contains a boolean. Pass that into your method and keep a reference
to it, and then change the value of the boolean "inside" the type at a later
date.

Sorry, if this all seems trivial to you.
Jesse

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
Can someone show me how to go about assigning a ref parameter to a variable so that when updates are made to this variable they are reflected in the
calling object's reference parameter. I want to be able to change the value of the referenced variable outside of the constructor.


You can't, thank goodness. It would make for horrendously
unmaintainable code, with the values of potentially private variables
being changed outside the class in non-obvious ways, etc. Also consider
that the variable may not even *exist* any more by the time the second
call is made.

What you *could* do is create a reference type which contains a
boolean. Pass that into your method and keep a reference to it, and
then change the value of the boolean "inside" the type at a later date.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Jesse <je*****@hotmail.com> wrote:
Thanks for the feedback.

What i want to do is have a reuseable loginForm. When the login button on
this loginForm is clicked, the users validility is checked and if all is ok,
the calling form (main form), which will have a boolean for if the user is
logged on or not needs to be updated. How else can you have a reuseable
loginForm if not to be able to update the parent form of this true or false?

I think nick has a good read on it.

Jon could you explain what you mean a bit further (possibly with example)
with regard to:

What you *could* do is create a reference type.
How do i do that?


By doing:

class BoolWrapper
{
bool value;

public bool Value
{
get { return value; }
set { this.value = value; }
}
}

Pass a reference to an instance of that in, and then keep it stashed
away in your class. Then use myBoolWrapper.Value = true (or whatever).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Hey Jon, can i send you my code? it's only two forms.
I'm lost. Today is first attempt at C#.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
Thanks for the feedback.

What i want to do is have a reuseable loginForm. When the login button on this loginForm is clicked, the users validility is checked and if all is ok, the calling form (main form), which will have a boolean for if the user is logged on or not needs to be updated. How else can you have a reuseable
loginForm if not to be able to update the parent form of this true or false?
I think nick has a good read on it.

Jon could you explain what you mean a bit further (possibly with example) with regard to:

What you *could* do is create a reference type.
How do i do that?


By doing:

class BoolWrapper
{
bool value;

public bool Value
{
get { return value; }
set { this.value = value; }
}
}

Pass a reference to an instance of that in, and then keep it stashed
away in your class. Then use myBoolWrapper.Value = true (or whatever).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Jesse <je*****@hotmail.com> wrote:
Hey Jon, can i send you my code? it's only two forms.
I'm lost. Today is first attempt at C#.


Right, that last sentence explains a lot - and it's not your fault,
it's the fault of tutorials which try to get you to do too much to
start with.

I suggest you forget your current project for a day or so - concentrate
on learning the basics of C# first. That *doesn't* include forms. Write
some basic, dummy console programs which just create and manipulate
instances of classes, so you get a feel for things like the difference
between reference types and value types, how to declare a class etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
I've done Java, C++, and a bit of VB and i know how to use pointers in C++
and ByVal and ByRef in VB. I know what references are. When a reference is
passed it means that when you change the value of that reference, that same
memory is modified and not a copy.

With the boolWrapper class, this is what i think you mean, tell me if i am
wrong.

1. in the calling main form - declare this BoolWrapper class (in the same
file and namespace but out of the mainForm class definition).
2. in the calling main form - create an instance of this class.
3. in the calling main form - place a reference to this instance in the
parameter when calling the login form.

Then what happens in the loginform?

I really appreciate the help.
Jesse

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
Hey Jon, can i send you my code? it's only two forms.
I'm lost. Today is first attempt at C#.


Right, that last sentence explains a lot - and it's not your fault,
it's the fault of tutorials which try to get you to do too much to
start with.

I suggest you forget your current project for a day or so - concentrate
on learning the basics of C# first. That *doesn't* include forms. Write
some basic, dummy console programs which just create and manipulate
instances of classes, so you get a feel for things like the difference
between reference types and value types, how to declare a class etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Jesse <je*****@hotmail.com> wrote:
I've done Java, C++, and a bit of VB and i know how to use pointers in C++
and ByVal and ByRef in VB. I know what references are. When a reference is
passed it means that when you change the value of that reference, that same
memory is modified and not a copy.
You need to be very careful about the difference between passing a
reference by value and passing a value by reference, however.
With the boolWrapper class, this is what i think you mean, tell me if i am
wrong.

1. in the calling main form - declare this BoolWrapper class (in the same
file and namespace but out of the mainForm class definition).
Well, preferrably in a completely different file, in my view, but it
doesn't matter.
2. in the calling main form - create an instance of this class.
Yes.
3. in the calling main form - place a reference to this instance in the
parameter when calling the login form.
Yes.
Then what happens in the loginform?


You store the reference you're passed, and then modify the value within
the object when you want to make the fact that you've logged in
available to the main form. For instance:

private BoolWrapper loggedIn;

//Constructor which is called from another form
public void LoginForm(BoolWrapper log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn.Value = true;
}

I still don't think it's a particularly good architecture, mind you,
but without know more details it would be hard to say what the best
thing to do would be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
yeah, that is what i thought you meant. But when i do this i get an error
in the loginForm saying:

Inconsistent accessibility: parameter type 'CustomForms.BoolWrapper' is less
accessible than method
'CustomForms.FormLogin.FormLogin(CustomForms.BoolW rapper)'

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
I've done Java, C++, and a bit of VB and i know how to use pointers in C++ and ByVal and ByRef in VB. I know what references are. When a reference is passed it means that when you change the value of that reference, that same memory is modified and not a copy.


You need to be very careful about the difference between passing a
reference by value and passing a value by reference, however.
With the boolWrapper class, this is what i think you mean, tell me if i am wrong.

1. in the calling main form - declare this BoolWrapper class (in the same file and namespace but out of the mainForm class definition).


Well, preferrably in a completely different file, in my view, but it
doesn't matter.
2. in the calling main form - create an instance of this class.


Yes.
3. in the calling main form - place a reference to this instance in the
parameter when calling the login form.


Yes.
Then what happens in the loginform?


You store the reference you're passed, and then modify the value within
the object when you want to make the fact that you've logged in
available to the main form. For instance:

private BoolWrapper loggedIn;

//Constructor which is called from another form
public void LoginForm(BoolWrapper log)
{
loggedIn = log;
}

private void loginIsTrue()
{
loggedIn.Value = true;
}

I still don't think it's a particularly good architecture, mind you,
but without know more details it would be hard to say what the best
thing to do would be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10
Jesse <je*****@hotmail.com> wrote:
yeah, that is what i thought you meant. But when i do this i get an error
in the loginForm saying:

Inconsistent accessibility: parameter type 'CustomForms.BoolWrapper' is less
accessible than method
'CustomForms.FormLogin.FormLogin(CustomForms.BoolW rapper)'


That'll be because FormLogin is public, but BoolWrapper isn't, at a
guess.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Hey Jon,

Ok, i have it working thanks to your good self. What i don't understand is
why this BoolWrapper class needs to be substituted for just a public bool
value.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
yeah, that is what i thought you meant. But when i do this i get an error in the loginForm saying:

Inconsistent accessibility: parameter type 'CustomForms.BoolWrapper' is less accessible than method
'CustomForms.FormLogin.FormLogin(CustomForms.BoolW rapper)'


That'll be because FormLogin is public, but BoolWrapper isn't, at a
guess.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12
Jesse <je*****@hotmail.com> wrote:
Ok, i have it working thanks to your good self. What i don't understand is
why this BoolWrapper class needs to be substituted for just a public bool
value.


Because a boolean is a value type, not a reference type, and although
you can pass things *by* reference, that only affects the formal
parameter of the method, not the value itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #13
Ok. I think i get it.
Thanks for all the help.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jesse <je*****@hotmail.com> wrote:
Ok, i have it working thanks to your good self. What i don't understand is why this BoolWrapper class needs to be substituted for just a public bool value.


Because a boolean is a value type, not a reference type, and although
you can pass things *by* reference, that only affects the formal
parameter of the method, not the value itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #14

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

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
15
by: Web Developer | last post by:
Hi, Can someone provide a short and concise statement(s) on the difference between pointers and references. A graphical representation (via links?) of both would be much appreciated as well. ...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
17
by: Bradley1234 | last post by:
Sorry if this is obvious, but Ill ask... Is there a new way of using pointer operations in C# ? Ive got a Deitel book on C# that neither mentions the word "pointer" nor "unsafe" in the index....
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
4
by: naveid | last post by:
I have an array (List<T>) containing tens of thousands of items. I need to maintain copies of the array, sorted in a few different ways. I'm working on Windows Mobile so memory is a constraint. To...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
0
by: jappenzeller | last post by:
We're using the HttpRuntime Cache for our objects and we're trying to do the following. We pull lists of objects from the database and put them in cache. We put a reference to the list in...
0
by: TonyJ | last post by:
Hello! You can write int vektor = new int; This array will be stored on the heap and you will have a reference(vektor) to this array. As long as you keep a reference to this array will GC never...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.