Skip to main content
GameDev.net gamedev.net
🔒 Locked

array

Started by Tiger99 Nov 23, 2003 at 1:36 AM 3 replies 350+ views
Original Post
Tiger99
Tiger99
Hi I have created two classes A, B A contains a private member - an array. Is it possible for class B to use that array? Thanks
Colin Jeanne
Colin Jeanne
Yes, if B is a friend of A or if you create a function that returns the array (or a const reference to it).


Colin Jeanne | Invader''s Realm
Tiger99
Tiger99
Can you show me some sample codes on how to do it?

Thanks
joelmartinez
joelmartinez
You didn''t specify a language, so I''ll give it to you in C# :-)

public class A
{
private string[] _myArray;

//... other init code

public string[] GetArray()
{
return this._myArray;
}
}

public class B
{
public void DoSomethingWithA(A refToA)
{
string[] localArray = refToA.GetArray();
//Do something with the localArray now
}
}


Hope that helps,

Joel Martinez
http://www.codecube.net/
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]
joelmartinez
joelmartinez
alternatively, A could be coded to use a public property accessor instead of a public method to return the private array
public class A
{
private string[] _myArray;

//... other init code

public string[] MyArray
{
get {return this._myArray;}
}
}

public class B
{
public void DoSomethingWithA(A refToA)
{
string[] localArray = refToA.MyArray;
//Do something with the localArray now
}
}


Joel Martinez
http://www.codecube.net/
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.