对象引用类,新手问题. [英] Object reference to class, newbie question.

查看:66
本文介绍了对象引用类,新手问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因此,基本上,我已经制作了一个Windows窗体,在上面放置了一个文本框,并将其命名为txtBox.然后,我在表单中的命名空间中有两个类和要处理的类.

Hi,

So basically I''ve made a windows form I''ve put a text box on it and called it txtBox. I then have two classes within the namespace the form and the class I want to work on.

namespace WCFWindowsF
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ClientPart wcfCall = new ClientPart();
        }
    }

    class ClientPart
    {
        static void Zain(string[] args)        
        {
            txtBox.Text = "WCF Simple Demo";
        }
    }
}



发生的错误是名称" txtBox"在当前上下文中不存在."我只是不明白为什么,因为两个类都在同一个命名空间中.非常感激任何的帮助.谢谢.



The error that occurs is ''The name ''txtBox'' does not exist in the current context.'' I just can''t understand why since both classes are within the same namespace. Any help would be most appreciated. Thanks.

推荐答案

您需要inherit源类才能获得如下所示的文本框.

You need to inherit the source Class to get the textbox as shown below.

namespace WCFWindowsF
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            ClientPart wcfCall = new ClientPart();
        }
    }
 
    class ClientPart : Form1
    {
        static void Zain(string[] args)        
        {
            txtBox.Text = "WCF Simple Demo";
        }
    }
}



问候,
Eduard



Regards,
Eduard


将textBox设为公开,您可以在任何地方访问它.或继承 ClientPart
Make the textBox public you can access it anywhere. OR inherit Form class in ClientPart class


中的 Form 类,由于默认情况下该表单的控件是私有的,因此您不能在另一个类中直接访问它.
您可以使用贝娄方法访问另一个类中的文本框.

As control of the form are by default private you cannot access it directly in another class.
you can use bellow method to access textbox in another class.

namespace Test
{
    public partial class Form1 : Form
    {
        public string strTextBox
        {
            get { return txtBox.Text; }
            set { txtBox.Text = value; }
        }
        public Form1()
        {
            InitializeComponent();
            
            
        }

        public class ClientPart : Form1
        {
            static void Zain(string[] args)
            {

                Form1 objform = new Form1();
                objform.strTextBox = "WCF Simple Demo";
                
            }
        }
        
    }
    
    
}



如果解决了您的问题,请标记为解决方案



Please mark as solution if its solved your problem


这篇关于对象引用类,新手问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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