[英]How to call an object from another method in C#
任何人都可以幫助新手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();
}
}
為了通過兩種不同的方法調用它,兩種方法都需要訪問該值。 由於它們屬於同一類型,因此分享listener
值的最簡單方法是使其成為一個字段
public class Lru_Listen {
HttpListener listener;
public void ListenForAag() {
listener = new HttpListener();
}
public void LruListenAccReq() {
HttpListenerContext context = listener.Getcontext();
}
}
問題完全在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
。
你需要閱讀范圍。 基本上你的監聽器不存在,除了在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();
}
}
您也可以返回偵聽器並在第二種方法中接收它。
public HttpListener ListenForAag()
{
listener = new HttpListener();
return listener;
}
public void LruListenAccReq(HttpListener listener)
{
HttpListenerContext context = listener.Getcontext();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.