在最后调用同一个类中从其他构造函数的构造 [英] Calling constructor from other constructor in same class at the end

查看:131
本文介绍了在最后调用同一个类中从其他构造函数的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到这里就可以调用另一个构造的http:/ /stackoverflow.com/questions/829870/calling-constructor-from-other-constructor-in-same-class



但它的调用其他的构造开始的时候,而我想在最后调用。 ?这是可能的。



更新的人前来要求具体:
在我的Program.cs希望能够根据一些背景,做



可以在本:

  // Form1上会显示距离自己的私人信息会员
Application.Run(新Form1的());

或这样的:

  // Form1中会显示从1级成员的消息(超载Form1的消息样,如果存在的Class1)
Application.Run(新Form1的(新1级()));



我Form1中我有



 私人字符串成员=测试样品,如果没有class1的; 

在Class1的我

 私人字符串成员=从1级测试样品 
公共字符串成员;
{
{返回this.member; }
集合{this.member =价值; }
}

在Form1中我有这两个构造

  //当然Form1中()可能包含100行代码
Form1的(){MessageBox.Show(this.member); }

//我不想重复在第二个构造
100线//所以我需要调用一个构造
Form1中(1级){
本。成员= class1.member;
//这是我现在想调用Form1中()
//但下面的语法不起作用
本();
}


解决方案

没有,构造机制非常明确和严格的,你只能'叫'你正在写的一前一其他的构造函数。



但是,你可以用它来你的优势,只是扭转你的想法(办法),使'最后'的构造一个用户调用。要制定出具体,你将不得不张贴一些示例代码。



编辑:



有轻松的解决方法,如:

  Form1类
{
公共Form1中(1级C1)
{
如果(C1!= NULL)this.member = c1.member;
}

公共Form1中():这个(空)
{
}
}

但你不能调用构造函数形式内部另一个,这会造成更多的问题。


I read here it is possible to call another constructor http://stackoverflow.com/questions/829870/calling-constructor-from-other-constructor-in-same-class

But it calls the other constructor at the beginning, whereas I want to call at the end. Is this possible ?

Update for people asking for specific: In program.cs I want to be able, depending on some context, to do

either this:

// Form1 will show a message from its own private member
Application.Run(new Form1());

or this:

// Form1 will show a message from class1 member (kind of overloading form1 message if class1 exists)
Application.Run(new Form1(new Class1()));

I Form1 I have

private string member = "Test Sample if no class1";

In class1 I have

private string member = "Test Sample from class1";
public string member;
{
    get { return this.member; }
    set { this.member = value; }
}

In Form1 I have these 2 constructors

// of course Form1() could contain 100 lines of code
Form1() { MessageBox.Show(this.member); }

// I don't want to duplicate 100 lines in second constructor
// so I need to call first constructor
Form1(class1) {
    this.member = class1.member;
    // this is where I would now like to call Form1()
    // but the syntax below doesn't work
    this();
}

解决方案

No, the constructor mechanism is very clear and strict, you can only 'call' an other constructor before the one you are writing.

But you could use that to your advantage, just reverse your thinking (approach) to make the 'last' constructor the one the user calls. To work out the specifics, you will have to post some sample code.

Edit:

There are easy workarounds, like:

class Form1
{
   public Form1(Class1 c1)
   {
      if (c1 != null) this.member = c1.member;
   }

   public Form1() : this(null)
   {
   }
}

But you are not allowed to call a constructor form 'inside' another, this would create much more problems.

这篇关于在最后调用同一个类中从其他构造函数的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆