1

コンストラクターについていくつかアドバイスをもらいましたが、今ではたくさんあります。これらを関数または他の方法で単純化できますか? これが私のクラスですか?

public class ContentViewModel
    {
        public ContentViewModel() { }
        public ContentViewModel(Content content)
        {
            this.Content = content;
        }
        public ContentViewModel(string pk)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.Created = DateTime.Now;
        }
        public ContentViewModel(string pk, string rk)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.RowKey = rk;
            this.Content.Created = DateTime.Now;
        }
        public ContentViewModel(string pk, string rk, string user)
        {
            this.Content = new Content();
            this.Content.PartitionKey = pk;
            this.Content.RowKey = rk;
            this.Content.Created = DateTime.Now;
            this.Content.CreatedBy = user;
        }
        public Content Content { get; set; }
        public bool UseRowKey { 
            get {
                return this.Content.PartitionKey.Substring(2, 2) == "05" ||
                       this.Content.PartitionKey.Substring(2, 2) == "06";
            }
        }
        public string TempRowKey { get; set; }

    }

私はc#4を使用しています。名前付き引数とオプション引数について誰かが言いました。それは私を助けますか?

4

4 に答える 4

5

これを試して:

    public ContentViewModel() { }
    public ContentViewModel(Content content)
    {
        this.Content = content;
    }
    public ContentViewModel(string pk): this(new Content)
    {
        this.Content.PartitionKey = pk;
        this.Content.Created = DateTime.Now;
    }
    public ContentViewModel(string pk, string rk): this(pk)
    {
        this.Content.RowKey = rk;
    }
    public ContentViewModel(string pk, string rk, string user): this(pk, rk)
    {
        this.Content.CreatedBy = user;
    }

またはあなたが試すことができます:

    public ContentViewModel(Content content = null, string pk = null, 
                            string rk = null, string user = null)
    {
        this.Content = content ?? new Content();
        if (pk != null) this.Content.PartitionKey = pk;
        if (rk != null) this.Content.RowKey = pk;
        if (user != null) this.Content.CreatedBy = user;
        this.Content.Created = DateTime.Now;
    }        

これにより、コンストラクター設定を呼び出すか、パラメーターを欠落させて、左から右に提供するか、@Robert Harvey が提案する名前付きパラメーターを使用できます。
例:

var c = new ContentViewModel();
var c = new ContentViewModel(pk: 'ppp', user: 'marco');
var c = new ContentViewModel(null, 'pk', null, 'marco');
于 2012-07-08T05:58:15.297 に答える
2

すべての引数を持つ単一のコンストラクターを記述します。

    public ContentViewModel(string pk, string rk, string user)
    {
        this.Content = new Content();
        this.Content.PartitionKey = pk;
        this.Content.RowKey = rk;
        this.Content.Created = DateTime.Now;
        this.Content.CreatedBy = user;
    }

次に、他のコンストラクターからそのコンストラクターを呼び出し、不足している引数の既定値を渡します。

    public ContentViewModel(string pk) : this(pk, "","") { }

または、 C# 4.0 でオプションの引数を使用できます。

于 2012-07-08T05:58:06.853 に答える
1

はい、例えばあなたのコンストラクタ

public ContentViewModel(string pk)
{
 this.Content = new Content();
 this.Content.PartitionKey = pk;
 this.Content.Created = DateTime.Now;
}

電話できました

public ContentViewModel(string pk, string rk)

次の構文を使用して、rk のデフォルト値を指定します。

public ContentViewModel(string pk) : this(pk, "defaultRkValue"){}

「defaultRkValue」を意味のあるデータに置き換えます

本当に夢中になりたい場合は、2 つの引数のコンストラクターが 3 つの引数のコンストラクターなどを呼び出すことができます。

于 2012-07-08T05:58:46.140 に答える
0

あなたの場合、コンストラクターを1回だけ持つことができます:

public ContentViewModel(string pk, string rk = null, string user = null)
{
    this.Content = new Content();
    this.Content.PartitionKey = pk;
    this.Content.RowKey = rk;
    this.Content.Created = DateTime.Now;
    this.Content.CreatedBy = user;
}

次のように呼び出すこともできます。

var content = new ContentViewModel("pk");

また

var content = new ContentViewModel("pk", "rk");

また

var content = new ContentViewModel("pk", "rk", "user");

あるいは

var content = new ContentViewModel("pk", user: "user"); //without rk field
于 2012-07-08T06:03:54.890 に答える