473,320 Members | 1,876 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,320 software developers and data experts.

Is a form passed by value or reference?

I wrote a method that accepted a form. The signature was as ff:

void foo(ref Form frm) ;

I had to remove the ref keyword to get it to compile. Any idea why? I
though forms would be passed by reference. I certainly dont want copies
of my form to be made before being passed as some of the forms are quite
"heavy".

Look forward to some answers.

Nov 21 '05 #1
3 1697
"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dl**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
I wrote a method that accepted a form. The signature was as ff:

void foo(ref Form frm) ;

I had to remove the ref keyword to get it to compile. Any idea why? I
though forms would be passed by reference. I certainly dont want copies of
my form to be made before being passed as some of the forms are quite
"heavy".

Look forward to some answers.


Common question. An object variable is a pointer to the object. Passing it
by value you are only passing the pointer by value. If you pass the object
variable by reference then you are passing the pointer byref. The only point
to this is if you want to return an object variable from a function. Either
way you are not making a copy of the object, so it's probably better the
pass it by value as it's slightly more efficient.

Michael
Nov 21 '05 #2

"Ann Huxtable" wrote...
Look forward to some answers.
You'll probably get many... ;-)

But to get the explanations in the right order, I have reordered your
question a bit...
I though forms would be passed by reference. I certainly
dont want copies of my form to be made before being passed
as some of the forms are quite "heavy".
You never pass any *copies* of objects at all through the use of parameters.

The default is that you pass everything "by value", but what *is* actually
the value when you "pass a form"? The value you pass is that of the
argument, but the value isn't the actual instance, but a reference to the
instance...
I wrote a method that accepted a form. The signature was as ff:

void foo(ref Form frm) ;

I had to remove the ref keyword to get it to compile.
Any idea why?


You probably called it with something like this:

foo(ref this);

That isn't possible, as the "this" keyword is a *read-only* refernce to the
instance itself, i.e. that the reference isn't allowed to be changed, and as
such the *reference* isn't allowed to change.

But don't worry, just remove the keyword ref, and it will work as you want.

What the "ref" keyword actually means, is (in short) that you as argument
pass along a pointer to the variable you use as an argument, which can have
some bad side-effects.

Let's say you have this method:

void Change(ref Person p)
{
p = new Person("Kerry");
}

Now you create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(ref f);

After the call, f references the Person "Kerry".
Let's say you have this method:

void Change(ref Person p)
{
p = new Person("Kerry");
}

I.e. that with the "ref" keyword, you can change the value of the reference.

This is probably *not* what you want to do with the Form you "send" to the
foo-method.
Let's instead say you have this method:

void Change(Person p)
{
p.Name = "Kerry";
}

Create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(f);

p inside the method is a reference to the *same* instance as f. After the
call, f *still* references the same object as before, but it has changed the
value of a property.
Let's show a last example:

void Change(Person p)
{
p = new Person("Kerry");
}

Create a Person, and "send it" to the method Change.

Person f = new Person("George");

Change(f);

In this case p starts out with the same value as f, i.e. a reference to the
instance "George", but as p is a local variable inside the method, its value
is replaced by a new reference to a new instance. That (new) instance will
be eligable for garbage collection as soon as the method runs out of scope,
and nothing has happened to the instance f refers to, which it still does
afterwards.
HTH.

// Bjorn A
Nov 21 '05 #3


Ann Huxtable wrote:
I wrote a method that accepted a form. The signature was as ff:

void foo(ref Form frm) ;

I had to remove the ref keyword to get it to compile. Any idea why? I
though forms would be passed by reference. I certainly dont want copies
of my form to be made before being passed as some of the forms are quite
"heavy".

Look forward to some answers.


tks guys :-)

Nov 21 '05 #4

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

Similar topics

1
by: Marcello Pietrobon | last post by:
Hello, My background is C++ I would like to know for sure if it is possible to pass a value by reference to a function AND having it changed by the function itself def changeValue( text...
7
by: Stijn Goris | last post by:
hi all, I try to enable some form elements (positie and fotoToevoegen) when a user clicks a checkbox. Sadly enough it doesn't work I use the 'onSelect="EnableInput();'. The function is defined...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
7
by: Nikola Skoric | last post by:
I noticed that System.Windows.Forms.Form constructor treats his parameters as though they are passed by reference even if I omit the "ref" keyword. Why's (and how does he do) that? Shouldn't...
1
by: TopherB | last post by:
Hi, First let me say that my knowledge of HTML and Javascript is fairly limited. But I am stuck in a situation of trying to adapt a website's shopping cart to a new one. Here's the problem, the...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.