Search code examples
c#.netsilverlightconstructor

How can I resolve the error Constructor cannot call itself in C#


I am getting the below error for the following code:

Constructor "Delay.vkMessages.vkMessages(string, System.DateTime, string, bool, string)" cannot call itself.

I have another class, a copy of this class, but it works (I can add code). How can I 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;
            
        }
        ....

Solution

  • 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.