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

Variable changes

AMP
Hello,
I have a Form level variable:

public partial class Form1 : Form
{
byte rxHeader = 0;..............................

That gets passed from one function to another:
if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
0)..........// rxHeader is 0 when it leaves here

comRxHeader changes it to 144. It stays at 144 all through the
function, but when it gets back to the sending function(comTxRx) it is
0 again, but there is nothing to change it back to 0.
What am I doing wrong?
Thanks
Mike

Sep 16 '06 #1
5 1798
Mike,

Parameters are not passed by reference by default, a copy of them is
made and that is passed to the methods. If your function changes them, you
don't see the changes outside of the function.

To change this, you have to pass the parameter by reference, like so:

public int comRxHeader(ref byte header, ...)

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hello,
I have a Form level variable:

public partial class Form1 : Form
{
byte rxHeader = 0;..............................

That gets passed from one function to another:
if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
0)..........// rxHeader is 0 when it leaves here

comRxHeader changes it to 144. It stays at 144 all through the
function, but when it gets back to the sending function(comTxRx) it is
0 again, but there is nothing to change it back to 0.
What am I doing wrong?
Thanks
Mike

Sep 16 '06 #2

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
| Hello,
| I have a Form level variable:
|
| public partial class Form1 : Form
| {
| byte rxHeader = 0;..............................
|
| That gets passed from one function to another:
| if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
| 0)..........// rxHeader is 0 when it leaves here
|
| comRxHeader changes it to 144. It stays at 144 all through the
| function, but when it gets back to the sending function(comTxRx) it is
| 0 again, but there is nothing to change it back to 0.
| What am I doing wrong?
| Thanks
| Mike
|
You are passing rxHeader by value (effectively passing a copy), you need to
pass a reference to the variable like...

comRxHeader(ref rxHeader...);
the function needs to be declared as ..
...... comRxHeader(ref byte rxh,....) {
rxh = ...; //changes the callers value
}

Willy.

Sep 16 '06 #3
Hello AMP,

when u pass rxHeader variable into your method it passed by value,
it means that you have the local copy of your rxHeader variable inside comRxHeader
method, not the class variable
Any changes of your local variable inside method don't reference to the class
variable.
When u exit the method, your local variable GCed and class variable is set
to the previous value, that is 0 in your case.

To change this behaviour use "ref" keyword before your function param, like
comRxHeader(ref rxHeader, rxNum, (timeout * prolongFactor))

in that case all changes to rxHeader inside method will reference to you
class variable

AHello,
AI have a Form level variable:
Apublic partial class Form1 : Form
A{
Abyte rxHeader = 0;..............................
AThat gets passed from one function to another:
Aif (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
A0)..........// rxHeader is 0 when it leaves here
AcomRxHeader changes it to 144. It stays at 144 all through the
Afunction, but when it gets back to the sending function(comTxRx) it
Ais
A0 again, but there is nothing to change it back to 0.
AWhat am I doing wrong?
AThanks
AMike
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 16 '06 #4
"AMP" <am******@gmail.coma écrit dans le message de news:
11**********************@h48g2000cwc.googlegroups. com...

| I have a Form level variable:
|
| public partial class Form1 : Form
| {
| byte rxHeader = 0;..............................
|
| That gets passed from one function to another:
| if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
| 0)..........// rxHeader is 0 when it leaves here
|
| comRxHeader changes it to 144. It stays at 144 all through the
| function, but when it gets back to the sending function(comTxRx) it is
| 0 again, but there is nothing to change it back to 0.
| What am I doing wrong?

In addition to the other advice about using ref on the parameter, if all the
code that uses this private field (variable) is in the Form1 class, then
there is no need to pass the variable around, as it is visible and
accessible to all members of the class in which it is declared.

public class Test
{
private int number;

void DoSomething()
{
number = 123;
}

void DoSomethingElse()
{
int i = number;
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 16 '06 #5
First, you are passing a Value Type by value, which means you are passing a
copy of the value to the function. When you want a function to modify the
actual value of the parameter being passed, you need to pass by reference or
using an out parameter. However, since the rxHeader field (not variable at
class level, but "field") is available to the entire Form class, you should
not need to pass it at all, unless you pass it to a function outside the
Form.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hello,
I have a Form level variable:

public partial class Form1 : Form
{
byte rxHeader = 0;..............................

That gets passed from one function to another:
if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
0)..........// rxHeader is 0 when it leaves here

comRxHeader changes it to 144. It stays at 144 all through the
function, but when it gets back to the sending function(comTxRx) it is
0 again, but there is nothing to change it back to 0.
What am I doing wrong?
Thanks
Mike

Sep 17 '06 #6

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

Similar topics

5
by: Ikke | last post by:
I have to share a variable between browser sessions. One session for example changes the variable, the other sessions must see the changes (get the new value instead of the original value) it's...
3
by: lamthierry | last post by:
Let's say I have the following source code in C++: // The following is in a .cpp file int val = 0; for ( int i = 0; i < 10; i++ ) val = i; // Now I'm in a python GUI, glade or GTK
6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
5
by: JeffFinnan | last post by:
Is there a way to have a text input field equal some variable value so that it changes if that variable changes? It does not have to by dynamic. <INPUT TYPE="TEXT" NAME="hW" size="4" value=some...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
5
by: Brakeshoe | last post by:
I would like to find out how to turn on the Auto Complete variable names feature while editing source code in Visual Studio. It seems to appear on intermittantly, and when it does the variable...
4
by: John Kraft | last post by:
Hi all, My question is more of a phylisophical one here, but I am wondering what the difference is (effectively and performance wise) between using a shared variable/static variable and using a...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
10
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python:2.4 Based on a search of other posts in this group, it appears as though os.environ is one way to obtain the PATH environment variable. My questions: 1) is it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.