无法从其他文件创建对象

时间:2012-06-18 08:07:23

标签: c# c#-4.0

我在 TestInput.cs 文件中有这个类,

 public class HDate
 {
        private string StartYear;

        public string StartYear1
        {
            get { return StartYear; }
            set { StartYear = value; }
        }

        public HDate() { }

        public HDate(){
           DateTime today = DateTime.Now;
           //Some Code here
        }

     return StartDate+';'+EndDate; // ???
 }

然后我要从该类创建一个对象。这个文件是 Inputsubmit.cs 文件

 if(ds.Tables[0].Rows.Count < 0)
         {
          HDate hd = new HDate()
          hd.

         }

我得到的是自动为HDate创建新课程..但是我已经知道了另一个文件的课程吗?

3 个答案:

答案 0 :(得分:0)

如果您可以澄清在尝试构建HDate时实际发生的情况,将会很有帮助。但是,您的HDate类有两个具有相同签名的构造函数。编译器将无法确定要调用哪一个,因此您需要先删除其中一个,然后再创建HDate个对象。

答案 1 :(得分:0)

除非你所展示的内容是HDate声明的一个非常简洁的样本,否则HDate类可能无法编译,因此intellisense不会提取它

特别是你有一句话:return StartDate+';'+EndDate; // ???

这只是在类声明中,而不是在方法中,并引用StartDateEndDate,我无法看到声明。

答案 2 :(得分:0)

关于你的类HDate:构造函数HDate()不能返回值!你不能不退出正在退缩的Methode ......

当你创建你的类的实例:( HDate hd = new HDate();)并且它是正确的。你将执行你的构造函数HDate()。但是在你的构造函数中你声明了一个局部变量((今天它是一个局部变量而且它不是一个全局变量))所以你不能使用它。

也许你可以做到:

public class HDate
    {
        private string StartYear;

        public string StartYear1 {get ; set } 
        //you can write that when you use framwork 3.0 or hit

  //public HDate() { }// you can note have 2 constructor with same signature !! 


        public DateTime today;
        public HDate(){
        today = DateTime.Now;
      //Some Code here
      }

// you can make methode :
public string updateDate() // this methode will retrun a string
{
DateTime StartDate, EndDate;
// some code here
return StartDate+" ; "+EndDate ; // string is between " " ! not between ' ' ;)
}
}// end of class.. after you can declarate hd and make hd.updateDate(); good luck
哦,哦!我忘了 !你需要写:使用TestInput.cs;在您的文件中:Inputsubmit.cs:D