3个问题:在类中创建对象时出错,以及如何从不同的类中调用方法 [英] 3 questions:error creating objects in class and how to call a method from different class

查看:74
本文介绍了3个问题:在类中创建对象时出错,以及如何从不同的类中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在玩C#,尝试在类中创建方法时遇到问题.

其实我有3个问题.

这是我的原始代码,看看:

Hi guys,

I''m playing around with C# and encountered a problem while trying to create a method in a class.

Actually I''m having 3 question.

This is my original code, have a look:

namespace EApplication
{
    public partial class frmMainPage : Form
    {
        Button btnAddClientInfo = new Button();
        Button btnDataSheet = new Button();

        TabControl tabControl = new TabControl();

        public frmMainPage()
        {
            InitializeComponent();

            btnAddClientInfo.Parent = this;
            btnDataSheet.Parent = this;
            tabControl.Parent = this;
            
            btnAddClientInfo.Click += new EventHandler(this.btnAddClientInfo_Click);
            btnDataSheet.Click += new EventHandler(this.btnDataSheet_Click);

            this.Resize += new EventHandler(frmMainPage_Resize);
        }

        void btnAddClientInfo_Click(Object sender, EventArgs e)
        {
            //tabControl.Visible = true;
            TabPage tabpgAddClient = new TabPage("Add Client");
            AddClient userctrlAddClient = new AddClient();

            tabpgAddClient.Controls.Add(userctrlAddClient);
            tabControl.TabPages.Clear();
            tabControl.TabPages.Add(tabpgAddClient);
            tabControl.Visible = true;

            btnAddClientInfo.Enabled = false;
            btnDataSheet.Enabled = true;
        }

        void btnDataSheet_Click(Object sender, EventArgs e)
        {
            //tabControl.Visible = true;
            TabPage tabpgDataSheet = new TabPage("Data Sheet");
            DataSheet userctrlDataSheet = new DataSheet();

            tabpgDataSheet.Controls.Add(userctrlDataSheet);
            tabControl.TabPages.Clear();
            tabControl.TabPages.Add(tabpgDataSheet);
            tabControl.Visible = true;



            btnDataSheet.Enabled = false;
            btnAddClientInfo.Enabled = true;
        }

        void frmMainPage_Resize(Object sender, EventArgs e)
        {
        }
    }
}



问题1:
可以说,我在类frmMainPage中添加了一个新方法,出现错误.
8条错误消息:



Question 1:
Let say, I add a new method in class frmMainPage, I''m having errors.
8 Error Messages:

MainPage.cs(52,9):error CS0116: A namespace does not directly contain members such as fields or methods


与其他7相同的错误描述.

代码如下:


Same erros description to the other 7.

The code, looks like this:

public frmMainPage()
{
           InitializeComponent();

            ....

           int test ()
           {
              return 1;
           }
}



谁能解释那里的问题是什么,我试图将方法声明为public,但问题仍然存在.

问题2:
我删除了问题1中添加的新方法test(),并创建了一个新方法,如下所示:



Can anyone explain what is it wrong there, I have tried to declare the method as public, still the problem still occurs.

Question 2:
I remove the new method test() added in question 1 and created a new method as below:

public frmMainPage()
{
       InitializeComponent();
      
       ....
}

...

void frmMainPage_Resize(Object sender, EventArgs e)
{
}


public int GetAddClientBottom()
{
    return btnAddClientInfo.Bottom;
}



现在,我希望从其他类中调用此方法,这是一个UserControl(另一个.cs文件).

在这里调用方法的地方:



And now I want this method be called from a different class, well it''s a UserControl(another .cs file).

Here the place where method to be called:

namespace EApplication
{
    public partial class AddClient : UserControl
    {
        public AddClient()
        {
            InitializeComponent();

            //resize the page size
            this.Width = this.Right - 45;
            this.Height = (this.Bottom - 80) - ( frmMainPage. + 15);
        }
    }
}



我想在我加粗的地方调用



I want to call the

GetAddClientBottom()

方法.不幸的是,我无法拨打电话.

任何想法,我都不知道出了什么问题,因为我试图将方法声明为public,但是没有用.还缺少什么?

问题3:

在上面的原始代码中,您可以在某些地方看到此内容:

method, in place where I BOLD it. Unfortunaltely, i''m unable to place the call.

Any ideas, I'', kinda out of idea what gets wrong, because I tried declare the method as public, but no use. Whats still missing?

Question 3:

In the original code above, you can see this in some places:

btnAddClientInfo.Parent = this;
btnDataSheet.Parent = this;
tabControl.Parent = this;
tabControl.Parent = this;



为什么需要它?我们不能只将对象添加到表单中吗?

非常感谢. :)

Skunkhead.



Why do we need this?Can;t we just add the objects to the form?

Thanks alot. :)

Skunkhead.

推荐答案

#1:命名空间不直接包含诸如字段或方法之类的成员".您没有将方法放在表单类中,而是将大括号弄乱了,然后将其放在类的外部,但放在名称空间内.

#2:仅当类的方法为static时,才可以调用该类的方法,否则只能在该类的实例上调用该方法.您需要创建一个实例:

#1: "A namespace does not directly contain members such as fields or methods" -- this is what it is. You did not put method in the form class, you messed up your curly brackets and put it outside the class but inside the name space.

#2: You can call the method of the class only if it is static, otherwise you can only call a method on instance to the class. You need to create an instance:

class MyClass {
    internal MyAccessibleMethod() { /* ...*/ }
    public MyAccessibleMethodw() { /* ...*/ }
    int field;
}
MyClass myClassInstance = new MyClass();
myClassInstance.MyAccessibleMethod();



当然,为了实现可访问性,方法应该是内部的或公共的(如果需要从其他程序集访问).
现在,实例方法是如何工作的:与静态方法非常相似,只是有一个名为this的附加参数.此参数表示实例,可用于访问实例成员.它未显示在参数列表中,但在技术上已实现为额外的隐藏参数.在方法内部(包括构造函数)可见:



Of course, for accessibility the methods should be internal or public (if you need access from some other assembly).
Now, how instance methods work: pretty much like static methods, only there is an additional parameter called this. This parameter represents the instance, can be used to access instance members. It is not shown in parameter list but technically implemented as an extra hidden parameter. It is visible inside methods (including constructors):

internal MyAccessibleMethod() { this.field = 3; /*...*/ }



在大多数情况下,可以省略"this.".有时,使用"this."对成员进行资格认证绝对是不可避免的.示例:有一个与field同名的参数.

#3:我刚刚解释了this;在您的示例中,它可以表示Control的任何实例(包括Form).
此代码



In most cases "this." can be omitted. Sometimes, qualification of a member with "this." is absolutely unavoidable. Example: there is a parameter with the same name as field.

#3: I just explained this; in your sample it can represent any instance of Control (including Form).
This code

btnAddClientInfo.Parent = this;


等效于


is equivalent to

this.Controls.Add(btnAddClientInfo);



这是在运行时向控件添加子级的一种方法.可以在已经显示表单的情况下完成-代码将立即显示一个新的孩子.子控件应在插入之前进行设置(位置,文本等).

一个一般性建议:暂时停止执行Forms编程:您对混乱的理解程度过高.取而代之的是,将C#本身以及一些使用更简单的代码(例如,作为控制台应用程序)进行的基本编程工作加长.



This is a way to add children to a controls during run-time; can be done when a form is already shown -- the code will show a new child immediately. The child control should be set up before inserting (location, text, etc).

One general advice: stop doing Forms programming for a while: your level on confusion is too high. Instead, long the C# itself and some basic programming using much more simple coding, maybe as a console application.


Q1:您添加的代码未出现在类内部.您似乎必须在某个类的类部分之外的代码.

问题2:这并不是说缺少什么.这是您有严重的设计缺陷.您的用户控件不应以表单名称调用任何内容.用户控件不应该在意它的形式.如果是这样,则只能在该一种形式上使用,而在其他任何地方都不能使用.

您还依赖于该表单上存在的特定代码段.这意味着您现在放下该控件的每种表单都需要实现一个方法或属性,以便控件可以正常工作.这是一个严重缺陷的设计.我无法告诉您如何解决它,因为简单地说,我不知道您的用户控件在做什么,您的代码在做什么或为什么.

问题3:是的.我不知道为什么您需要该代码,因为您的代码不足以查看发生了什么.
Q1: The code you added doesn''t appear inside of a class. You appear to have to the code outside of a class section somewhere.

Q2: It''s not that there''s something missing. it''s that you have a severe design flaw. Your usercontrol should not call ANYTHING by the form''s name. A usercontrol should not care which form it''s on. If it did, it could only be used on that one form, nowhere else.

You''re also dependant on a certain piece of code existing on that form. This means that every form that you drop this control on now needs to implement a method or property so your control will work. This is a heavily flawed design. I can''t tell you how to fix it because, simply, I have no idea what your usercontrol is doing, what your code is doing, or why.

Q3: Yes. I have no idea why you need that code since there isn''t enough of your code to see what''s going on.


这篇关于3个问题:在类中创建对象时出错,以及如何从不同的类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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