473,379 Members | 1,355 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,379 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 2368
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.