问题描述
我有两个 .cs 文件,我想使用按钮来更改字符串通道的值,但它调用错误 CS0236:(字段初始值设定项无法引用非静态字段、方法或属性),请帮助我,我是找了两天的解决办法。谢谢。
在 Form1.cs 中我有
settings_file = open('Settings.txt','r')
homescreen_order_string_1 = ['assignments ']
homescreen_order_string_2 = ['checklists ']
homescreen_order_string_3 = ['custom text ']
lines = settings_file.readlines()
lines_to_read_1 = [3]
def readline(lines):
if lines in homescreen_order_string_1:
return "assignments"
elif lines in homescreen_order_string_2:
return "checklists"
elif lines in homescreen_order_string_3:
return "custom text"
else:
return "err"
with open('Settings.txt','r') as settings_file:
for position,lines in enumerate(settings_file):
if position in lines_to_read_1:
if readline(lines) == "assignments":
print("yay") #placeholder for function to display assignments
break
elif readline(lines) == "checklists":
print('yay 2') #placeholder for functions to display checklists
break
elif readline(lines) == "custom text":
print("yay3") #placeholder for function to display custom text
break
elif readline(lines) == "err":
print("err")
print(lines) #this is just for bug testing,to see what the system is actualy seeing.
break
else:
pass
#here is the .txt file I am trying to pull from:
#-----------------------------------------
#Settings:
# order of homescreen print:
#assignments
#checklists
#custom text
#
# Custom Text:
#you have no custom text!-
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 Twitchchatbot
{
public partial class Form1 : Form
{
string channel;
//this line is a problem
Ircclient irc = new Ircclient("irc.twitch.tv",6667,"nickname","password",channel);
解决方法
将 irc 类的实例化移动到构造函数中,而不是将它放在类成员本身中。
public partial class Form1 : Form
{
string channel = string.Empty;
IrcClient irc;
public Form1()
{
InitializeComponent();
irc = new IrcClient("irc.twitch.tv",6667,"nickname","password",channel);
}
}
或者更好的是,添加某种“连接”按钮并在您通过文本框或其他东西真正了解频道后在那里创建它。