2013-12-17 75 views
4

任何人都可以幫助新手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();   
    } 
} 

回答

4

爲了在2種不同的方法中調用這兩種方法,需要訪問該值。由於他們是在同一類型的共享出來的listener值的最簡單的方法是讓現場

public class Lru_Listen { 
    HttpListener listener; 

    public void ListenForAag() { 
    listener = new HttpListener(); 
    } 

    public void LruListenAccReq() { 
    HttpListenerContext context = listener.Getcontext();   
    } 
} 
+2

[ಠ_ಠ](http://damnyourfastfingers.com) – Will

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明確。

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();   
} 
} 
0

您也可以返回偵聽器並在第二種方法中接收它。

public HttpListener ListenForAag() 
{ 
    listener = new HttpListener(); 
    return listener; 
} 

public void LruListenAccReq(HttpListener listener) 
{ 
    HttpListenerContext context = listener.Getcontext();   
} 
相關問題