【问题标题】:C# get set errorC# 获取设置错误
【发布时间】:2015-07-04 18:35:22
【问题描述】:

此表格 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”获取值时它返回空。我不知道为什么

【问题讨论】:

  • 只显示我们需要的信息,而不是您的整个表格。
  • 这只是一个简单的 Visual Studio 表单。如果你能通过它将会有很大的帮助。
  • 这不是我们查看您的代码的重点。您应该只提供我们解决您的问题所需的内容。当你声明/当你得到或设置/当你使用/时,结果是什么。

标签: c# get set


【解决方案1】:

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

【讨论】:

  • 欢迎,了解 OOP 以及对象如何工作和携带数据始终很重要
猜你喜欢
  • 2010-11-26
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
相关资源
最近更新 更多