Constructor cannot call itself c#

空扰寡人 提交于 2019-12-20 03:57:08

问题


Constructor "Delay.vkMessages.vkMessages(string, System.DateTime, string, bool, string)" cannot call itself.I have another class, copy of this class, but it works(I can add code).How I can resolve this error?

   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:


Remove the last parameter:

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;
}

That said, your logic is skewed, it should be the other way round (i.e. this constructor should do all the work and the other constructor should call this one:

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; 
}

Finally, you should fix your identifiers to conform to the .NET guidelines. In particular, a class should follow the PascalCase convention of starting with an upper-case letter.



来源:https://stackoverflow.com/questions/6563798/constructor-cannot-call-itself-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!