类中声明
MainForm severform = new MainForm();//MainForm是窗体
severform.InsertLogInfo(provice, provice + "客户端连接成功!");//插入MainForm窗体中的datagridview
string uploadFolder = severform.GetSavePath();//获取MainForm窗体中保存路径文本框值但是出现上述问题!不知道如何可以实现?

解决方案 »

  1.   

    MainForm severform = new MainForm();//MainForm是窗体 
    这里错了
    你需要的不是MainForm的对象,而且MainForm的实例
      

  2.   

    using System;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public void SetText(string str)
            {
                textBox1.Text = str;
            }        public string GetText()
            {
                return textBox1.Text;
            }        private void button1_Click(object sender, EventArgs e)
            {
                MyClass cls = new MyClass(this);
                cls.SetValve();
            }        private void button2_Click(object sender, EventArgs e)
            {
                MyClass cls = new MyClass(this);
                cls.GetValue();
            }       
        }
        public class MyClass
        {
            private Form1 m_frm = null;
            public MyClass(Form1 frm)
            {
                if (frm != null)
                {
                    m_frm = frm;
                }
            }        public void SetValve()
            {
                if (m_frm!=null) 
                    m_frm.SetText("123");
            }        public void GetValue()
            {
                if (m_frm!=null)
                MessageBox.Show(m_frm.GetText());
            }
        }}
      

  3.   

    MainForm severform;
    这样声明吗?可是提示未将对象的应用设置到对象的实例?
    不是这样声明实例吗?
      

  4.   


    你仔细看看我的例子,有一个MyClass构造函数,有一个参数
      

  5.   

    我这个类继承了其他类,是wcf,用构造函数服务端监听是错错误!
      

  6.   

    提示:The service type provided could not be loaded as a service 
    because it does not have a default (parameter-less) constructor. 
    To fix the problem, add a default constructor to the type, 
    or pass an instance of the type to the host.