References without unsafe pointers

  • Thread starter Thread starter Jesse
  • Start date Start date
J

Jesse

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
 
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 said:
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.
 
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
 
Jesse said:
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).
 
Hey Jon, can i send you my code? it's only two forms.
I'm lost. Today is first attempt at C#.
 
Jesse said:
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.
 
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
 
Jesse said:
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.
 
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.BoolWrapper)'
 
Jesse said:
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.BoolWrapper)'

That'll be because FormLogin is public, but BoolWrapper isn't, at a
guess.
 
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.
 
Jesse said:
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.
 
Ok. I think i get it.
Thanks for all the help.

Jon Skeet said:
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.
 
Back
Top