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

Problem with value passing

Hi,

I send a parameter from a Form (Form1) to an other form (Form2):

Form2 frm2 = new Form2();
frm2.number_frm2 = number_frm1;
frm2.Show();

The problem i got is that the variable 'number_frm2' always have the value 0
(it's an integer). I have declared it as 'public' but i can't get the right
value. I hope someone can help me, thanks.
Nov 17 '05 #1
8 1357
What is number_frm1 and where is it set? If it's a public member of an
instance of your Form1 class, you need to specify that. For example:

frm2.number_frm2 = frm1.number_frm1;

Also, you should use properties rather than exposing member variables of
your classes directly. For example, in Form1:

private int number_frm1;

// some code to assign a value to number_frm1

public int Number {
get {
return number_frm1;
}
set {
number_frm1 = value;
}
}

You can omit the set accessor if you want this value to be read-only. In
either case, you can then use the following to access this variable in
Form2:

frm2.number_frm2 = frm1.Number;

Hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"yo_mismo" <qw*@rty.com> wrote in message
news:hA******************@news.ono.com...
Hi,

I send a parameter from a Form (Form1) to an other form (Form2):

Form2 frm2 = new Form2();
frm2.number_frm2 = number_frm1;
frm2.Show();

The problem i got is that the variable 'number_frm2' always have the value
0
(it's an integer). I have declared it as 'public' but i can't get the
right
value. I hope someone can help me, thanks.

Nov 17 '05 #2

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:Ob**************@TK2MSFTNGP14.phx.gbl...
Also, you should use properties rather than exposing member variables of
your classes directly. For example, in Form1:

private int number_frm1;

// some code to assign a value to number_frm1

public int Number {
get {
return number_frm1;
}
set {
number_frm1 = value;
}
}


Why?
Nov 17 '05 #3
Oh my, now you've gone and opened that can of worms again :) To be sure,
opinions on the use of properties do differ widely. I happen to think they
are a good idea for a number of reasons:

1. It's good programming practice to limit direct access to data fields from
outside the class.
2. You can make properties read-only by not providing a set accessor. (Of
course you could also use a readonly field.)
3. You can perform additional calculations inside your accessors. For
example, you could perform data validation inside the set accessor.
4. You can easily notify other classes/objects about state changes
5. Versioning

To be sure, properties aren't perfect (for example, it would be nice to have
overloaded get/set accessors and/or different access modifiers), but I
generally find them more useful than public instance fields.

Let the flamewars begin.... :)

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"Scott Roberts" <sc***********@no-spam.intelebill.com> wrote in message
news:O2**************@TK2MSFTNGP09.phx.gbl...

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:Ob**************@TK2MSFTNGP14.phx.gbl...
Also, you should use properties rather than exposing member variables of
your classes directly. For example, in Form1:

private int number_frm1;

// some code to assign a value to number_frm1

public int Number {
get {
return number_frm1;
}
set {
number_frm1 = value;
}
}


Why?

Nov 17 '05 #4

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Oh my, now you've gone and opened that can of worms again :)
:-)
1. It's good programming practice to limit direct access to data fields from outside the class.
Says who? And more importantly, why?
2. You can make properties read-only by not providing a set accessor. (Of
course you could also use a readonly field.)
3. You can perform additional calculations inside your accessors. For
example, you could perform data validation inside the set accessor.
4. You can easily notify other classes/objects about state changes
5. Versioning
Yes, but the code you posted does none of that.

Here's the crux of my question, compare the following:

Your code:

private int number_frm1;

public int Number {
get {
return number_frm1;
}
set {
number_frm1 = value;
}
}
My code:

public int Number;
In what way(s) do these differ? In what way(s) and for what reason(s) is one
"better" than the other. Keep in mind that, if necessary, my code can always
be modified to look exactly like yours at any future date without any
side-effects (other than those I intend, of course, through the
implementation of a getter/setter).
Let the flamewars begin.... :)


Flaming is for those who can't think. :)
Nov 17 '05 #5
Scott Roberts <sc***********@no-spam.intelebill.com> wrote:
1. It's good programming practice to limit direct access to data fields
from outside the class.
Says who? And more importantly, why?


Says most OO practitioners, and for the reaons below.
2. You can make properties read-only by not providing a set accessor. (Of
course you could also use a readonly field.)
3. You can perform additional calculations inside your accessors. For
example, you could perform data validation inside the set accessor.
4. You can easily notify other classes/objects about state changes
5. Versioning


Yes, but the code you posted does none of that.


Not yet - but the code with the property can be changed to do any of
those (except making it readonly) without changing the interface used
by other code. The public field way of doing things can't.
Here's the crux of my question, compare the following:

Your code:

private int number_frm1;

public int Number {
get {
return number_frm1;
}
set {
number_frm1 = value;
}
}
My code:

public int Number;

In what way(s) do these differ? In what way(s) and for what reason(s) is one
"better" than the other. Keep in mind that, if necessary, my code can always
be modified to look exactly like yours at any future date without any
side-effects (other than those I intend, of course, through the
implementation of a getter/setter).


No, there are side-effects:

1) You're forced to recompile all code using the class
2) Any code which uses the field as a ref or out parameter would no
longer compile.
3) Any code which assumes that it's a field (using reflection, possibly
serialization etc) breaks.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Scott Roberts <sc***********@no-spam.intelebill.com> wrote:
1. It's good programming practice to limit direct access to data fields
from outside the class.


Says who? And more importantly, why?


Says most OO practitioners, and for the reaons below.


Oops, I meant to say before: also says Microsoft:

http://msdn.microsoft.com/library/en-
us/cpgenref/html/cpconFieldUsageGuidelines.asp?frame=true

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
1) You're forced to recompile all code using the class
I don't follow you. Are you referring to code in the same assembly that uses
the class? Of course you have to recompile the entire assembly if you change
the field, just as you would if you changed the getter/setter of a property.

Or do you mean you have to recompile external assemblies if you change a
field into a property? If so, I wasn't aware of this and it's good info to
have!
2) Any code which uses the field as a ref or out parameter would no
longer compile.
Good point. I would not expect code to use an object field/property as a ref
or out parameter, but I suppose it's possible.
3) Any code which assumes that it's a field (using reflection, possibly
serialization etc) breaks.


Good point. I don't use much reflection or serialization personally, but
that doesn't mean others don't.
Nov 17 '05 #8
Scott Roberts <sc***********@no-spam.intelebill.com> wrote:
1) You're forced to recompile all code using the class


I don't follow you. Are you referring to code in the same assembly that uses
the class? Of course you have to recompile the entire assembly if you change
the field, just as you would if you changed the getter/setter of a property.

Or do you mean you have to recompile external assemblies if you change a
field into a property? If so, I wasn't aware of this and it's good info to
have!


External assemblies. If they're trying to access it as a field and it's
now a property, that's a breaking interface change.
2) Any code which uses the field as a ref or out parameter would no
longer compile.


Good point. I would not expect code to use an object field/property as a ref
or out parameter, but I suppose it's possible.


I wouldn't expect it to either - because I'd never expose the field...
3) Any code which assumes that it's a field (using reflection, possibly
serialization etc) breaks.


Good point. I don't use much reflection or serialization personally, but
that doesn't mean others don't.


Another handy feature of properties is that it's really easy to find
out when they're being used during debug sessions - you can stick break
points on property accesses, but you can't do the same to find every
time a field is being used.

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

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

Similar topics

6
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
1
by: Hans [DiaGraphIT] | last post by:
Hi! I have problem with passing data to custom action. I don't know what wrong I'm doing. I'm trying to follow the steps in the walkthrough: "Passing Data to a Custom Action" ...
5
by: Kevin Yu | last post by:
hi all since the DateTime can't be assign null, the min value is set to 1901 or something, if in a application design, the date field can be null, so in between the UI and the DB, the business...
5
by: Steve | last post by:
Hi, I currently have a problem passing a variable value from one page to another. Once a form submit button is pressed java pops up a window and displays some information. The problem being is...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
0
by: saaprasad2002 | last post by:
i opened an excel application using follwing code to load test data to run mstest cases Public Class ExcelMatrixReader Public oXL As Excel.Application Public oWB As Excel.Workbook ...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.