c# 如何私有成员变量

axf478 2017-09-13 03:02:52
楼主小白,在用C#写一个类似串口的form程序,现在想写一个函数访问定义的textbox变量发现无法访问,提示错误“需要对象应用”,求问解决方法。下面贴出代码和错误提示:

在form1_design.cs 文件里定义了一些控件:
private System.Windows.Forms.Label label_Port;
private System.Windows.Forms.ComboBox comboBox_Port;
private System.Windows.Forms.Label label_Baudrate;
private System.Windows.Forms.ComboBox comboBox_Baudrate;
private System.Windows.Forms.Button button_Open;
private System.Windows.Forms.Button button_Reset;
private System.Windows.Forms.Label label_Data_Received;

在程序中想给这些控件赋值:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Drawing.Drawing2D;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace Radio_Test_GUI
{
public partial class Form_Test_Gui : Form
{
private SerialPort Serial = new SerialPort();
private StringBuilder Builder = new StringBuilder();
private long recv_count = 0;
private long send_count = 0;
private bool listening = false;
private bool clossing = false;

public Form_Test_Gui()
{
InitializeComponent();
}

private static void ReadXml() // 自己写的程序,
{
comboBox_Port.Text = GetAppConfig("Portname"); //出错的地方
comboBox_Baudrate.Text = GetAppConfig("Baudrate"); //出错的地方
}
}

提示的错误:
错误 1 非静态的字段、方法或属性“Radio_Test_GUI.Form_Test_Gui.comboBox_Port”要求对象引用 F:\VS_Project\Radio_Test_GUI 界面优化收发\Radio_Test_GUI\Radio_Test_GUI\Form1.cs 33 13 Radio_Test_GUI

错误 2 非静态的字段、方法或属性“Radio_Test_GUI.Form_Test_Gui.comboBox_Baudrate”要求对象引用 F:\VS_Project\Radio_Test_GUI 界面优化收发\Radio_Test_GUI\Radio_Test_GUI\Form1.cs 34 13 Radio_Test_GUI

求大神指点!
...全文
392 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2017-09-13
  • 打赏
  • 举报
回复
实例成员不能在静态方法中引用。静态成员在静态方法中引用,也可以在非静态方法中引用。
exception92 2017-09-13
  • 打赏
  • 举报
回复
private static void ReadXml() // 自己写的程序, static去掉。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧