2012-10-14 64 views
0
AppXmlLogWritter objParameterized = new AppXmlLogWritter(1234, "LogApplication", "LogFilepath"); 

AppXmlLogWritter objParmeterlessConstr = new AppXmlLogWritter(); 

objParameterized.WriteXmlLog("0", "LogFlag"); 

如何獲取此函數中的默認構造函數值?如何獲取函數中的默認構造函數值

回答

3

調用構造如下圖所示,通過這個()

public AppXmlLogWritter(int intLogIDPrefix, string strLogApplication, string strLogFilePath) 
      :this() 
    { 
     LogIDPrefix = intLogIDPrefix; 
     LogApplication = strLogApplication; 
     LogFilePath = strLogFilePath; 
    } 
0

不是很清楚你說的是什麼價值,但是如果你參考randomNumber,你已經有了類裏面。

如果你要調用的函數是消耗型AppXmlLogWritter的功能,你可以這樣定義屬性:在您的其它構造

public class AppXmlLogWritter{ 


     public int RandomNumber {get;set}; //PUBLIC PROPERTY 


     public AppXmlLogWritter() 
     { 
      Random random = new Random(); 
      RandomNumber = random.Next(9999); 
      LogDateTime = DateTime.Now.ToString("yyyyMMdd HHmmss"); 
     } 

    .... .. 
    .... ..  

} 
+0

怎麼樣日期時間我也得到日期時間功能空白 – lax

+0

@如果你的問題是你的默認ctor沒有被調用,只需添加'this()'參數化ctor的聲明。 – Tigran

2

從另一個構造函數調用類的基類的構造使用this關鍵字,像這樣:

public AppXmlLogWritter(int intLogIDPrefix, string strLogApplication, string strLogFilePath) 
    : this() 
{ ... }