buidling构造函数时出错 [英] Error in buidling a constructor
问题描述
我正在尝试将一个字符串从form1传递给form2。
我我正在使用构造函数来执行此操作
public class logFilePath
{
public string DataLogFile { get ; set ; }
public logFilePath( string dataLogFile)
{
this .DataLogFile = dataLogFile;
}
}
我正在尝试为此类声明类的新实例form2
public logFilePath busDetails = 新 logFilePath();
我收到错误,如下所示
错误1'SCANLA.Common.logFilePath'不包含带0参数的构造函数
唔不 - 它没有' t。
当您创建参数化构造函数时,编译器不会生成默认的无参数版本(如果您想要阻止人们创建实例而不通过您的代码)。
有两个三种方法来解决这个问题:
1)创建一个无参数的显式构造函数,它什么都不做:
public logFil ePath()
{
}或者:
2)使字符串参数可选(V4.0及以上)
public logFilePath( string dataLogFile = null )
{
this .DataLogFile = dataLogFile;
}或者:
3)调用你的构造函数并传递一个字符串:
public logFilePath busDetails = new logFilePath( Hello World!);
添加第三个选项 - OriginalGriff [ / edit]
http://msdn.microsoft.com/ en-us / library / ms173115.aspx [ ^ ]
你必须为构造函数提供一个参数:
logFilePath busDetails = new logFilePath( 此处的数据日志文件路径) ;
或添加默认构造函数:
public class logFilePath
{
public logFilePath () // 默认构造函数
{
}
..
Hi,
I am trying to pass a string from form1 to form2.
I am using a constructor for doing this
public class logFilePath
{
public string DataLogFile { get; set; }
public logFilePath(string dataLogFile)
{
this.DataLogFile = dataLogFile;
}
}
I am trying to declare new instance of class for this class in form2
public logFilePath busDetails = new logFilePath();
I am getting a error as shown below
Error 1 'SCANLA.Common.logFilePath' does not contain a constructor that takes 0 arguments
Well no - it doesn't.
When you create a parameterised constructor, the compiler does not produce a default parameterless version (in case you wanted to prevent people creating an instance without going through your code).
There aretwothree ways to get round this:
1) Create an explicit parameterless constructor, that does nothing:
public logFilePath() { }
Or:
2) Make the string parameter optional (V4.0 and above)
public logFilePath(string dataLogFile = null) { this.DataLogFile = dataLogFile; }Or:
3) Call your constructor and pass a string:
public logFilePath busDetails = new logFilePath("Hello World!");
[edit]Added third option - OriginalGriff[/edit]
http://msdn.microsoft.com/en-us/library/ms173115.aspx[^]
You have to supply an argument for the constructor :
logFilePath busDetails = new logFilePath("data log file path here");
or add a default constructor:
public class logFilePath { public logFilePath() // default constructor { } ...
这篇关于buidling构造函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!