can us talk about exchange data between classes?

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???
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

yangxiaoxiang,

If you want b to access members of a, then b needs to have an instance
of a. Now you can create this instance in your method, or you can alter
your method to take a parameter of type a which is then accessed to get the
value.

As for remoting, if you want to get the value of B, then you can just
call GetValue after creating a new instance of B (a proxy should be created
and you should be able to access B remotely).

To get the value of A, the above applies here as well.

Hope this helps.
 
Y

yangxiaoxiang

Thanks,Nicholas Paldino!
But now I find my example may be not so accurate,it should be exactly like
this(a console(or winform) application):

namespace Server
{
class A
{
public int aValue;
[STAThread]
static void Main(string[] args)
{
aValue=10;
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(B),"B",WellKnownOb
jectMode.SingleCall);
System.Console.WriteLine("hit to exit");
System.Console.ReadLine();
}
}

public class B:System.MarshalByRefObject
{
public B()
{
}
public int GetAValue()
{
//???How can I get aValue,I don't know how can I get the instance of
class A(a console or a winform application)???
}
}
}

reguards,
yangxiaoxiang
 

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