如何获取函数中的默认构造函数值

时间:2012-10-14 09:29:53

标签: c# default-constructor

AppXmlLogWritter objParameterized = new AppXmlLogWritter(1234, "LogApplication", "LogFilepath");

AppXmlLogWritter objParmeterlessConstr = new AppXmlLogWritter();

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

如何在此函数中获取默认构造函数值?

3 个答案:

答案 0 :(得分:3)

通过this()

在你的其他构造函数中调用构造函数,如下所示
    public AppXmlLogWritter(int intLogIDPrefix, string strLogApplication, string strLogFilePath)
          :this()
    {
        LogIDPrefix = intLogIDPrefix;
        LogApplication = strLogApplication;
        LogFilePath = strLogFilePath;
    }

答案 1 :(得分:2)

要从另一个构造函数调用类的基础构造函数,请使用this关键字,如下所示:

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

答案 2 :(得分: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");
        }

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

}