D
Dan Bass
Say I've got an ArrayList, which contains a list of integers.
Is it possible to get a reference to one of the integers contained within
the collection so that changes to it are reflected in the arraylist?
Eg... For references.
Class myClass
{
public myClass ( string initialValue )
{
_myString = initialValue;
}
private string _myString = "Hello";
public string MyString
{
// get/set here exposing myString
}
}
// inside a method
ArrayList myClassArray = new ArrayList();
myClassArray.Add ( new myClass("Good") );
myClassArray.Add ( new myClass("Better") );
myClassArray.Add ( new myClass("Fantastic") );
foreach ( myClass currentInstance in myClassArray )
{
// this is a reference to each instance of myClass
// therefore all the _myStrings will contain this in
// the ArrayList
currentInstance.MyString = "Not Too Good";
}
Eg.. for values
ArrayList myIntArray = new ArrayList ();
myIntArray.Add ( 1 );
myIntArray.Add ( 2 );
myIntArray.Add ( 3 );
foreach ( int x in myIntArray )
{
// not a reference because int is value
// QUESTION: how do i get a reference
// to each int without referencing it
// like this: myIntArray[z] = ...
x = 5;
}
Is it possible to get a reference to one of the integers contained within
the collection so that changes to it are reflected in the arraylist?
Eg... For references.
Class myClass
{
public myClass ( string initialValue )
{
_myString = initialValue;
}
private string _myString = "Hello";
public string MyString
{
// get/set here exposing myString
}
}
// inside a method
ArrayList myClassArray = new ArrayList();
myClassArray.Add ( new myClass("Good") );
myClassArray.Add ( new myClass("Better") );
myClassArray.Add ( new myClass("Fantastic") );
foreach ( myClass currentInstance in myClassArray )
{
// this is a reference to each instance of myClass
// therefore all the _myStrings will contain this in
// the ArrayList
currentInstance.MyString = "Not Too Good";
}
Eg.. for values
ArrayList myIntArray = new ArrayList ();
myIntArray.Add ( 1 );
myIntArray.Add ( 2 );
myIntArray.Add ( 3 );
foreach ( int x in myIntArray )
{
// not a reference because int is value
// QUESTION: how do i get a reference
// to each int without referencing it
// like this: myIntArray[z] = ...
x = 5;
}