Variable changes

  • Thread starter Thread starter AMP
  • Start date Start date
A

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
 
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.
 
| 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.
 
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

A> Hello,
A> I have a Form level variable:
A> public partial class Form1 : Form
A> {
A> byte rxHeader = 0;..............................
A> That gets passed from one function to another:
A> if (comRxHeader(rxHeader, rxNum, (timeout * prolongFactor)) ==
A> 0)..........// rxHeader is 0 when it leaves here
A> comRxHeader changes it to 144. It stays at 144 all through the
A> function, but when it gets back to the sending function(comTxRx) it
A> is
A> 0 again, but there is nothing to change it back to 0.
A> What am I doing wrong?
A> Thanks
A> Mike
---
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
 
"AMP" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top