Newbie - Class that has an array

R

Russell

This is got to be an easy one - but since I am new.
How to make sure you are passing the reference to a new array not the same
one.

When I change the array for item1 it also affects item2

I have a 2 classes:
class A
{
float[] mArray;

public float[] mArray
{
get { return this.mArray;}
set { this.mArray = value;}
}
}

class B
{

List<A> myA = new List<A>();

public B()
{
myA.Add(new A());
myA.Add(new A()); // I expect to now have two objects, each
with an array mArray.
}

//need help here also - how to get based on index in list
public float[] mArray0
{
get { return myA[0].mArray}
}
public float[] mArray1
{
get { return myA[1].mArray}
}
}
 
R

Russell

I think I know where to check -
When I assign the values to the mArray I am passing it a reference to an
array - not the actual valves of the array.

How do I go about setting the actual valves in the mArray in the
instance of Class A?
 
M

Michael C

Russell said:
I think I know where to check -
When I assign the values to the mArray I am passing it a reference to
an array - not the actual valves of the array.

How do I go about setting the actual valves in the mArray in the
instance of Class A?

Array.CopyTo

Michael
 
G

Gaurav Vaish \(www.EduJiniOnline.com\)

I am sorry - can you give me an example of the code on this.


get
{
float[] retVal = new float[mArray.Length];
mArray.Copy(mArray, retVal, mArray.Length);

return retVal;
}
 
R

Russell

Thanks

tmpArray.CopyTo(myMotor[1].GPMHeader, value.Rank-1);

in the SET works - thanks.
 
M

Michael C

Russell said:
Thanks

tmpArray.CopyTo(myMotor[1].GPMHeader, value.Rank-1);

in the SET works - thanks.

Doing that in the set could be misleading. The user of your class would
assume it uses a reference to the array instead of a copy.

Michael
 
R

Russell

Being a new user to a C language - mainly a VB user, I do no understand the
wisdom in your statement. Being self taught - if it works - all is good. So
I am not being critical, really wanting to learn:

What would be a better discipline?


Michael C said:
Russell said:
Thanks

tmpArray.CopyTo(myMotor[1].GPMHeader, value.Rank-1);

in the SET works - thanks.

Doing that in the set could be misleading. The user of your class would
assume it uses a reference to the array instead of a copy.

Michael
 
M

Michael C

Russell said:
Being a new user to a C language - mainly a VB user, I do no understand
the wisdom in your statement. Being self taught - if it works - all is
good. So I am not being critical, really wanting to learn:

What would be a better discipline?

It might not matter in your situation but if someone uses your class in the
future they might expect the class to work on their array, not a copy of it,
eg:

Dim X as new Sorter()
X.ArrayToSort = MyArray
X.DoSort()

Would you expect MyArray to be sorted or to have to retrieve the array from
Sorter?

To make it clearer I would change it to a function called AddRange or
something similar (the arraylist has an Add method and an AddRange method).
In this case it makes it clear that a range of values is being added and not
a reference to an array.

Michael
 

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

Top