有谁能帮助一个初学者学习C#语言?我试图调用在同一类但不同方法中创建的对象。这两个方法都是从另一个类中调用的。下面是简化的代码,我省略了方法执行的其他代码。一个错误提示在第二个方法中“listener”对象无法识别。非常感谢你提供的任何帮助。
// 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();
}
}