任何人都可以帮助新手C#初学者吗?我试图调用在同一个类中创建的对象但是使用不同的方法。这两种方法都是从另一个类调用的。以下是简单的代码。我遗漏了方法执行的其他代码。错误表示在第二种方法中无法识别“侦听器”对象。感谢您提供的任何帮助。
// this 1st class calls methods of a 2nd class
public class Lru_operation
{
// create an object of the 2nd class
public Lru_Listen LruListen = new Lru_Listen();
// this method calls two methods from other class
public void LruOperation()
{
LruListen.ListenForAag(); // first method call
LruListen.LruListenAccReq(); // second method call
}
}
// this is the 2nd class
public class Lru_Listen
{
// 1st method creates an object from a different class (HttpListener)
public void ListenForAag()
{
HttpListener listener = new HttpListener();
}
// 2nd method calls 1st method's object to perform
// a method task from a different class
public void LruListenAccReq()
{
HttpListenerContext context = listener.Getcontext();
}
}
答案 0 :(得分:4)
为了通过两种不同的方法调用它,两种方法都需要访问该值。由于它们属于同一类型,因此分享listener
值的最简单方法是将其设为字段
public class Lru_Listen {
HttpListener listener;
public void ListenForAag() {
listener = new HttpListener();
}
public void LruListenAccReq() {
HttpListenerContext context = listener.Getcontext();
}
}
答案 1 :(得分:2)
问题完全在Lru_Listen
类中 - 您声明的变量是ListenForAag
成员的本地变量。如果你把它变成一个类级变量(一个字段),你将不会遇到这个问题:
// Make an instance variable:
HttpListener listener;
// 1st method creates an object from a different class (HttpListener)
public void ListenForAag()
{
// Set the instance variable
listener = new HttpListener();
}
// 2nd method calls 1st method's object to perform
// a method task from a different class
public void LruListenAccReq()
{
HttpListenerContext context = listener.Getcontext();
}
请注意,在这种情况下,最好在构造函数中设置它而不是方法:
// this 1st class calls methods of a 2nd class
public class Lru_operation
{
// create an object of the 2nd class
// Note that this can be private, since it's only used in this class
private Lru_Listen lruListen = new Lru_Listen();
// this method calls two methods from other class
public void LruOperation()
{
// No longer required
// lruListen.ListenForAag(); // first method call
lruListen.LruListenAccReq(); // second method call
}
}
// this is the 2nd class
public class Lru_Listen
{
HttpListener listener;
// use the constructor
public Lru_Listen()
{
listener = new HttpListener();
}
public void LruListenAccReq()
{
HttpListenerContext context = listener.Getcontext();
}
}
这保证了侦听器将始终正确设置,即使该类用户(可能是您)忘记明确调用ListenForArg
。
答案 2 :(得分:0)
你需要阅读范围。基本上你的监听器不存在,除了在范围内 ListenForAag函数。假设您需要在函数中实例化侦听器。但是你可能会更好地使用构造函数。
public class Lru_Listen
{
HttpListener listener;
// 1st method creates an object from a different class (HttpListener)
public void ListenForAag()
{
listener = new HttpListener();
}
// 2nd method calls 1st method's object to perform
// a method task from a different class
public void LruListenAccReq()
{
HttpListenerContext context = listener.Getcontext();
}
}
答案 3 :(得分:0)
您可以返回侦听器并在第二种方法中接收它。
public HttpListener ListenForAag()
{
listener = new HttpListener();
return listener;
}
public void LruListenAccReq(HttpListener listener)
{
HttpListenerContext context = listener.Getcontext();
}