构造函数不能自称c#

时间:2011-07-03 15:36:15

标签: c# .net silverlight constructor

构造函数“Delay.vkMessages.vkMessages(string,System.DateTime,string,bool,string)”无法调用自身。我有另一个类,这个类的副本,但它有效(我可以添加代码)。我怎么样可以解决这个错误吗?

   using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using ImageCacher;

    namespace Delay
    {
    public class vkMessages : INotifyPropertyChanged
    {
        public string Kto { get; private set; }
        public DateTime Date_Time { get; private set; }
        public string InOrOut { get; private set; }
        public string TexT { get; private set; }
        public bool Read_State { get; private set; }
        public IEnumerable<vkMessages> Messages
        {
            get
            {
                if (null == _vk_messages)
                {
                    _vk_messages = MessageService.GetMessages(InOrOut, () => MessagesLoaded = true);
                }
                return _vk_messages;
            }
        }
        private IEnumerable<vkMessages> _vk_messages;
        public bool MessagesLoaded
        {
            get { return _messagesLoaded; }
            set
            {
                _messagesLoaded = value;
                InvokePropertyChanged("MessagesLoaded");
            }
        }
        private bool _messagesLoaded;

        public vkMessages(string kto, DateTime date_time, string text, bool read_state)
        {
            Kto = kto;
            Date_Time = date_time;
            TexT = text;
            Read_State = read_state; 
        }
        public vkMessages(string kto, DateTime date_time,
            string text, bool read_state,string in_or_out)

            : this(kto,date_time,text,read_state,in_or_out)
        {
            InOrOut = in_or_out;

        }....

1 个答案:

答案 0 :(得分:7)

删除最后一个参数:

public vkMessages(string kto, DateTime date_time,
                  string text, bool read_state,string in_or_out)
    : this(kto, date_time, text, read_state)
{
    InOrOut = in_or_out;
}

那就是说,你的逻辑是倾斜的,它应该是反过来的(即这个构造函数应该完成所有的工作而另一个构造函数应该调用这个:

public vkMessages(string kto, DateTime date_time, string text, bool read_state)
    : this(kto, date_time, text, read_state, false) { }

public vkMessages(string kto, DateTime date_time,
                  string text, bool read_state,string in_or_out)
{
    InOrOut = in_or_out;
    Kto = kto;
    Date_Time = date_time;
    TexT = text;
    Read_State = read_state; 
}

最后,您应该修复标识符以符合.NET准则。特别是,类应遵循以大写字母开头的PascalCase约定。