C#get set error

时间:2015-07-04 18:35:23

标签: c# get set

此表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using Managed.Adb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        AndroidDebugBridge mADB;
        String mAdbPath;
        List<Device> devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);


        public Form1()
        {
            InitializeComponent();



        }

        private void button1_Click(object sender, EventArgs e )
        {
            //mAdbPath = Environment.GetEnvironmentVariable("PATH");
            mAdbPath = "C:\\Users\\Nadun\\AppData\\Local\\Android\\android-sdk\\platform-tools";
            mADB = AndroidDebugBridge.CreateBridge(mAdbPath + "\\adb.exe", true);
            mADB.Start();

            var list = mADB.Devices;
            textBox1.Text = "" + list.Count;
            foreach (Device item in list)
            {

                Console.WriteLine("");

                listBox1.Items.Add("" + item.Properties["ro.build.product"].ToString() + "-" + item.SerialNumber.ToString() );
            }

            //Console.WriteLine("" + list.Count);
        }



        private void button2_Click(object sender, EventArgs e)
        {


            string text = listBox1.GetItemText(listBox1.SelectedItem);
            Form2 f2 = new Form2(text);
           // f2.Phone = "scs";

            SetPhone sp = new SetPhone();
            sp.PhoneModel = "Test";

            this.Visible = false;
            f2.ShowDialog();
        }




    }
}

这是表格2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {

        private string phone;

        public string Phone
        {
            get { return this.phone; }
            set { this.phone = value; }
        }

        public Form2(string a)
        {
            InitializeComponent();
            textBox1.Text = a;
        }



        private void Form2_Load(object sender, EventArgs e)
        {
            //Form2 f2 = new Form2();
            //f2.phone = "s";
            //textBox1.Text = f2.Phone;
            SetPhone sp = new SetPhone();
            textBox1.Text = sp.PhoneModel;
            Console.WriteLine("sefsef-"+sp.PhoneModel);
        }
    }
}

这是我的班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    class SetPhone
    {
        private string phoneModel;

        public string PhoneModel {

            get { return this.phoneModel; }

            set { this.phoneModel = value; }

        }
    }
}

总是返回空。我不知道为什么。

我正在尝试从“form1”设置值。

我也为此写了课。但是当我从“form2”获取值时,它返回empty.i不知道为什么

1 个答案:

答案 0 :(得分:1)

你在button2_click中调用setter的SetPhone类对象是一个局部变量,所以当你尝试使用另一个局部变量在Form2_Load中访问它时,它是一个全新的对象,Get返回一个空字符串(默认值)。您应该能够在表单之间共享SetPhone变量,可能正在使用构造函数,然后它将保留使用setter

设置的值