构造函数无法调用C# [英] Constructor cannot call itself c#
问题描述
构造函数 Delay.vkMessages.vkMessages(string,System.DateTime,string,bool,string)不能自我调用。我有另一个类,该类的副本,但它可以工作(我可以添加代码)。
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;
}....
推荐答案
删除最后一个参数:
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;
}
最后,您应该修改标识符以使其符合.NET准则。特别是,一个类应遵循以大写字母开头的PascalCase约定。
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.
这篇关于构造函数无法调用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!