Newbie questions :)

  • Thread starter Thread starter Edgardo Rossetto
  • Start date Start date
E

Edgardo Rossetto

Hi, I have the following problem:

I need to set the property of a child control of a form, but from an
external form. The truth is i have no idea on how to procced to do this,

- do I need to make the owner form aware of the event occured in the
external form in order to reflect the change? (change this property)

- or somehow i can refer to the the instance of the owner form from the
external form so i can change this property?

Either ways i dont know how to do this, as you can see I'm lost :)

Thanks,
Edgardo
 
Hi Edgardo,

As I see it:

The owner form must have a reference to the child form. The child form
should expose a property or method to modify the control in question. When
the owner form deems it necessary, it calls the property or the method on
the reference to the child form. I am still not sure what do you mean by
'external form'?
 
Dmitriy said:
The owner form must have a reference to the child form. The child form
should expose a property or method to modify the control in question.
When the owner form deems it necessary, it calls the property or the
method on the reference to the child form. I am still not sure what do
you mean by 'external form'?

By external I mean just a non-modal owned form.

I want to change a property on the owner form (from the child), but I
don't know how to reference a property or method of it, do I explain
myself better?

using, for example this.Owner. (from a the child method) doesn't expose
any properties or methods of the owner (main) form.

Edgardo
 
Dmitriy said:
Hi Edgardo,

As I see it:

The owner form must have a reference to the child form. The child form
should expose a property or method to modify the control in question.
When the owner form deems it necessary, it calls the property or the
method on the reference to the child form. I am still not sure what do
you mean by 'external form'?

OK, let me see if I got this:

partial class Form1 : Form
{
private Form childForm;
private Form1 holder;

public Form1()
{
InitializeComponent();
holder = this;
}

...

private void DoSomething()
{
childForm = new Form2(ref holder);
this.AddOwnedForm(Form2);
childForm.Show();
}

...
}

partial class Form2 : Form
{
private Form1 parentForm;

public Form2(ref Form1 parent)
{
InitializeComponent();
this.parentForm = parent;
}

...

private void SomethingElseHappens()
{
...
this.parentForm.PublicProperty = true;
}

...
}

is this the *right* way to do it?, I first passed the parent object
(class Form1) by value but then the object would be copied and it would
waste more memory right?

Can any geek enlight me please?

I'm trying to do things right, not just do them.

Thanks,
Edgardo
 
Your approach seems right except for you don't need to use 'ref' at all.
..NET object are passed by reference, meaning the object data is not copied
when you pass the object around. 'ref' is necessary if you need to
substitute another *instance* of the object.
 
Dmitriy said:
Your approach seems right except for you don't need to use 'ref' at all.
.NET object are passed by reference, meaning the object data is not
copied when you pass the object around. 'ref' is necessary if you need
to substitute another *instance* of the object.
Ahhh

Thanks a lot! :)
 
I find this explanation confusing. As you know, you cannot pass an
actual
object to a method in C#. As you know, you pass a reference by value to
a
method, by default. If you need an added level of indirection such as in
a
swap routine you can pass a reference by reference. You can mimic this
byref
behavior in Java, by packing a reference in a wrapping class.

http://www.geocities.com/jeff_louie/OOP/oop5.htm

Regards,
Jeff
..NET object are passed by reference, meaning the object data is not
copied
when you pass the object around. 'ref' is necessary if you need to
substitute another *instance* of the object.
 
Jeff Louie said:
I find this explanation confusing. As you know, you cannot pass an
actual
object to a method in C#. As you know, you pass a reference by value to
a
method, by default. If you need an added level of indirection such as in
a
swap routine you can pass a reference by reference. You can mimic this
byref
behavior in Java, by packing a reference in a wrapping class.

But C# is not Java.

In C# an object parameter is always a 'pass by value' parameter, as are all
reference type parameters. The difference between a reference type parameter
and a value type parameter is that the reference type parameter is already a
reference to an object and therefore no copying of the object occurs, it is
the object itself that is passed.

Without 'ref' on a reference type parameter, you cannot change the address
of the object you are dealing with inside the method.

If you use 'ref' on a reference type parameter, then you allow code inside
the method to change the address of the object passed in.

But in both cases, it is the 'real' object, nota copy that is passed in to
the method and all operations on properties or methods of that objet affect
the original object outside of the method.

Joanna
 
I think that's what Jeff's saying too. He only says that the
terminology is confusing. And he's kinda right too, it is not the
object that gets passed, it's the reference to that object. And that
reference gets passed by value (by default). Adding ref makes the
reference passed by reference (like a pointer to a pointer in C/C++).

Regards
Senthil
 
Yeah don't tell me, I've been programming in PHP for 4-5 years now,
always "C style", so all this OOP things gets confusing...

Even now PHP has somewhat a Java-like implementation for the objects.
If you use 'ref' on a reference type parameter, then you allow code
inside the method to change the address of the object passed in.

I don't understand what you mean by this, what is the address of the
object? Can you explain a little further?

Thanks for your replies
 
I don't understand what you mean by this, what is the address of the
object? Can you explain a little further?

Imagine that an object is like a person

Whan a person is born they have to live somewhere so you put them in a
house.

If you want to be able to find that person ever again you will keep a record
of where that house is.

IOW, you keep a reference to its address.

So, with an Object; when you call the constructor of a class, you will
receive what you think is an object, but in actual fact all you have is the
address of the 'house' where the object lives. That 'house' is actually an
area of memory.

So now you have the address of the object, but you have to hold on to that
address somehow and you will usually put the address into a local variable
in a method or a field inside another object.

When you do what you think of as, passing an object to a method as a
parameter, you are in actual fact passing the address of where the object is
living in memory, for the parameter to hold on to.

Now if you pass the object without using the 'ref' modifier, the code inside
the method is allowed to do anything it wants to the inside of the object
held at the address in the parameter, but it is not allowed to change the
address that the parameter is holding on to.

This is because, if you always allowed methods to change the address held in
a parameter,
it has the effect of not only making the parameter hold on to a different
address, it also implies that everything else outside of the method will
also be looking at that new address where a totally different object is
living.

However, there are times when you want to allow a method to change the
address that the parameter is holding on to. If you use the 'ref' modifier,
you are telling the method that it is free to change the address the
parameter is holding on to, to the address of a different object.

Does that help?

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
 
Joanna,

Nice written, I add something I hope you don't mint.

A person can have a reference to the house where he lives.

A house can have a collection of references to all persons who are living in
a house.

Cor
 
Joanna said:
"Edgardo Rossetto" <[email protected]> a écrit dans le message de Ol#[email protected]...




Imagine that an object is like a person

Whan a person is born they have to live somewhere so you put them in a
house.

If you want to be able to find that person ever again you will keep a record
of where that house is.

IOW, you keep a reference to its address.

So, with an Object; when you call the constructor of a class, you will
receive what you think is an object, but in actual fact all you have is the
address of the 'house' where the object lives. That 'house' is actually an
area of memory.

So now you have the address of the object, but you have to hold on to that
address somehow and you will usually put the address into a local variable
in a method or a field inside another object.

When you do what you think of as, passing an object to a method as a
parameter, you are in actual fact passing the address of where the object is
living in memory, for the parameter to hold on to.

Now if you pass the object without using the 'ref' modifier, the code inside
the method is allowed to do anything it wants to the inside of the object
held at the address in the parameter, but it is not allowed to change the
address that the parameter is holding on to.

This is because, if you always allowed methods to change the address held in
a parameter,
it has the effect of not only making the parameter hold on to a different
address, it also implies that everything else outside of the method will
also be looking at that new address where a totally different object is
living.

However, there are times when you want to allow a method to change the
address that the parameter is holding on to. If you use the 'ref' modifier,
you are telling the method that it is free to change the address the
parameter is holding on to, to the address of a different object.

Does that help?

Joanna

Yes, a lot! :)

Thank you Joanna.

Edgardo
 
Back
Top