473,491 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference into an array

Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I should
be able to use this reference to obtain the value of the 3rd element even
when the value stored there changes.

Thanks,

TJ


Nov 17 '05 #1
5 1428
"Tom Jones" <bi***********@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ


myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than using
the array index. The latter isn't possible without using unsafe code and
unsafe code will mean pinning the array.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Nov 17 '05 #2

"Tom Jones" <bi***********@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I should be able to use this
reference to obtain the value of the 3rd element even when the value stored there changes.


For Value types like int you will need to Re-access the array every time since value types are
copied when you read them:
int x = myArray[2]; // copies the third element to x

Bill
Nov 17 '05 #3

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:eL*************@TK2MSFTNGP15.phx.gbl...
"Tom Jones" <bi***********@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ


myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than
using the array index. The latter isn't possible without using unsafe code
and unsafe code will mean pinning the array.
Regards


Hi Richard,

Thanks for the reply. Yes I am talking about a "pointer". I know that I
can do this:

unsafe static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 };

fixed(int* pi = &myArray[3])
{
Console.WriteLine(*pi); // displays "4"
myArray[3] = 6;
Console.WriteLine(*pi); // displays "6"
}
}

Why is creating a reference to a reference considered so "bad" that I have
to resort to this type of thing?

Thanks,
BT
Nov 17 '05 #4


"Tom Jones" wrote:

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:eL*************@TK2MSFTNGP15.phx.gbl...
"Tom Jones" <bi***********@hotmail.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ

myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than
using the array index. The latter isn't possible without using unsafe code
and unsafe code will mean pinning the array.
Regards


Hi Richard,

Thanks for the reply. Yes I am talking about a "pointer". I know that I
can do this:

unsafe static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 };

fixed(int* pi = &myArray[3])
{
Console.WriteLine(*pi); // displays "4"
myArray[3] = 6;
Console.WriteLine(*pi); // displays "6"
}
}

Why is creating a reference to a reference considered so "bad" that I have
to resort to this type of thing?


it's not that this is "bad". just when you program in a managed
environment, there are certain rules you need to follow. with the presence
of a garbage collector, you just can't randomly hold pointers to memory
locations because things get moved around all the time. that's the tradeoff,
you gain certain features in the managed world by giving up other features.
Thanks,
BT

Nov 17 '05 #5
Tom,
Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 }; How can I define a reference to say the 3rd element of that array?


How would you use such as thing if it was supported? Can't you just
define your own type that encapsulates the array reference and an
index? Something like

class ArrayElement<T>
{
private T[] _array;
private int _idx;

public ArrayElement(T[] array, int idx)
{
Debug.Assert(array != null);
_array = array;
_idx = idx;
}

public T Value
{
get { return _array[_idx}; }
set { _array[_idx] = value; }
}

// Add conversion operators and stuff if you want to
// make it a bit more transparent.
}

ArrayElement<int> third = new ArrayElement<int>(myArray, 2);
third.Value = 42;
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #6

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

Similar topics

5
2856
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
6
13998
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
3
119601
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
3
2670
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
7
16094
by: rossum | last post by:
Is there any way of creating a read only reference to an array? My class includes a private array of bytes with a Property to access it. However, I do not want users to use the returned reference...
11
3349
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
9
2425
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
7
3391
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
11
3331
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...
275
12024
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7190
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...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7360
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...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.