Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c# - 用户在C#中输入了dateTime(entered dateTime by user in c#)

I want to let user enter date and time by classes and DateTime method.At first I want to let user just enter a date of hire without time.

(我想让用户通过类和DateTime方法输入日期和时间。起初,我想让用户仅输入租用日期而没有时间。)

       Console.Write("Please enter date Of hire");
       DateTime dateOfHire = DateTime.Parse(Console.ReadLine());

then send dateOfHire to a method in another calss, Mothod is :

(然后将dateOfHire发送到另一个方法中,Mothod是:)

        public DateTime DateOfHire{
        set { }
        get
        {
            Console.WriteLine( dateOfHire.ToString("D")); // output: 24, december 2042
        }
    }

I got an error in get{} because I have to using return how can I use it?

(我在get{}get{}错误,因为必须使用return才能使用它吗?)

if I wrote return dateOfHire.ToString("D");

(如果我写了return dateOfHire.ToString("D");)

I get error too.

(我也出错。)

  ask by Akane Kodo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you have written it DateOfHire is not a method, it is a property.

(您编写DateOfHire方式不是方法,而是属性。)

The property is trying to act on a local member variable called dateOfHire which does not exist in scope.

(该属性正在尝试对作用dateOfHire不存在的名为dateOfHire的局部成员变量dateOfHire 。)

So, no wonder you are getting an error.

(因此,难怪您遇到错误。)

Try changing DateOfHire to a proper method.

(尝试将DateOfHire更改为适当的方法。)

You will need to pass your local value of dateOfHire (obtained from Parse() ) into the method using a parameter.

(您将需要使用参数将dateOfHire的本地值(从Parse() )传递到方法中。)

public void DateOfHire(DateTime dateOfHire)
{
   Console.WriteLine(dateOfHire.ToString("D"));
}

Also, DateOfHire cannot have a return type of DateTime like your property defines.

(另外, DateOfHire不能具有属性定义的DateTime返回类型。)

This is because you aren't returning anything, you are simply writing the result to the console.

(这是因为您什么都不返回,只是将结果写入控制台。)

So for your method, the void return type is implied.

(因此,对于你的方法,将void返回类型是隐含的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...