472,969 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,969 software developers and data experts.

Passing a byte[] by ref, and here I thought I knew what was going on.

If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}

Jan 30 '07 #1
3 2300
retVal and data are 2 different references (pointers)
data = retVal sets both to reference the same array (i.e. the pointer
memory addresses are equal), you have 2 pointers to the same object
retVal = null clears the retVal reference, but won't touch the data
reference.

If you set data = null then the array reference passed in would be
null

On Jan 30, 4:25 pm, "DaTurk" <mmagd...@hotmail.comwrote:
If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}
Jan 30 '07 #2
Hi DaTurk,

yes, passing by reference is passing an address in memory. The address of a
variable wich stores a reference to an array of byte.

Talking about reference could be misleading in C#. You could better think of
a ref and out parameter as passing a variable not a value.

So data here is a (reference type) variable, wich original belonged to the
caller.
retValue is another (reference type) variable, wich is local to this method

In the first line inside the try block you create a new array of byte and
put a reference to it in the variable retVal
After that you copy this reference to th variable data.
In the finally block you put null into the variable retVal; the variable
data is not effected by this.
"DaTurk" <mm******@hotmail.comschrieb im Newsbeitrag
news:11**********************@s48g2000cws.googlegr oups.com...
If I call this method, and pass it a byte[] by ref, and initialize
another byte array, set the original equal to it, and then null the
reference, why is the original byte array not null as well? I thought
passing by reference, your passing the address in memory.

public bool DoSomething(ref byte[] data)
{
byte[] retVal = null;

try
{
retVal = new byte[data.Length];

data = retVal;
}
catch (Exception ex)
{
}
finally
{
retVal = null;
}
}

Jan 30 '07 #3
So, let me see if I'm understanding, thanks for the responses by the
way, your not actually passing the address to the data in memory.
Your passing the address, of the pointer to the actual address in
memory, meaning your passing the variable by ref. I get it.

Feb 5 '07 #4

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

Similar topics

9
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) +...
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...
32
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
7
by: =?Utf-8?B?YmVyaWNr?= | last post by:
New to this, I used to pass an array like this function BytesToString(byref myarray() as byte, somethingelse as long) as long and m = BytesToString(fooBar(), bluenose) This would send...
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.