problem of exchange data between classes

  • Thread starter Thread starter yangxiaoxiang
  • Start date Start date
Y

yangxiaoxiang

I find there are some conditions I can't get data defined in another class.
One condition is:
namespace Test
{
class A
{
private int aValue;
public void method1()
{
B b=new B;
int value=b.GetValue(); //Cheers!I can get bValue in class B
}
}

class B
{
private int bValue;
public int GetValue()
{
return bValue;
}
public void method1()
{
//???How can I implement this method to get aValue in class A???
}
}
}

the second condition is in .net remoting:
namespace Test
{
class A
{
private int aValue;
public void method1()
{
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(B),"B",WellKnownOb
jectMode.Singleton);
//???then How can I implement this method to get bValue in class
B???
}
}

class B:System.MarshalByRefObject
{
private int bValue;
public void method1()
{
//???How can I implement this method to get aValue in class A???
}
}
}
 
yangxiaoxiang said:
I find there are some conditions I can't get data defined in another class.
One condition is:
namespace Test
{
class A
{
private int aValue;
public void method1()
{
B b=new B;
int value=b.GetValue(); //Cheers!I can get bValue in class B
}
}

class B
{
private int bValue;
public int GetValue()
{
return bValue;
}
public void method1()
{
//???How can I implement this method to get aValue in class A???
}
}
}
aValue is private so it will be unavailable outside of the class A. You need
to either:
- make aValue public
- create a GetValue function in class A like the one you have in class B
that returns aValue
- or, create a property that returns aValue (preferred method):
class A
{
private int aValue;
public int AValue
{
get { return aValue; }
set { aValue = value; } // you can leave this out if you want it
to be read-only
}
.......
}
 
Back
Top