0

I have two .cs files and I want to use a button to change value of string channel but it calls error CS0236: (A Field Initializer Cannot Reference The Nonstatic Field, Method, Or Property), please help me, I am looking for a solution two days. Thank you.

in Form1.cs I have

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);

and in IrcClient.cs I have

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace TwitchChatBot
{
    public class IrcClient
    {           
        private string userName;
        private string channel;

        private TcpClient tcpClient;
        private StreamReader inputStream;
        private StreamWriter outputStream;

        public IrcClient(string ip, int port, string userName, string password, string channel)
        {
            this.userName = userName;
            this.channel = channel;

            tcpClient = new TcpClient(ip, port);
            inputStream = new StreamReader(tcpClient.GetStream());
            outputStream = new StreamWriter(tcpClient.GetStream());

            outputStream.WriteLine($"PASS {password}");
            outputStream.WriteLine($"NICK {userName}");
            outputStream.WriteLine($"USER {userName} 8 * :{userName}");
            outputStream.WriteLine($"JOIN #{channel}");
            outputStream.Flush();
        }
Mitthy
  • 3
  • 2
  • Where exactly do you get this error? – Klaus Gütter May 05 '21 at 18:50
  • Does this answer your question? [A field initializer cannot reference the nonstatic field, method, or property](https://stackoverflow.com/questions/14439231/a-field-initializer-cannot-reference-the-nonstatic-field-method-or-property) – Timothy G. May 05 '21 at 18:56
  • @KlausGütter when I change "channel" with string channel – Mitthy May 05 '21 at 18:59
  • It would be good if you show the code demonstrating the problem instead of code that does not. You could put the initialization of the files into the constructor or another method. – Klaus Gütter May 05 '21 at 19:02
  • @KlausGütter I have edited the question, maybe its better like this. – Mitthy May 05 '21 at 19:21

1 Answers1

0

Move the instantiation of your irc class to the constructor instead of having it in the class member itself.

public partial class Form1 : Form
{
  string channel = string.Empty;
  IrcClient irc;
  public Form1()
  {
    InitializeComponent();
    irc = new IrcClient("irc.twitch.tv", 6667, "nickname", "password", channel);
  }
}

Or better yet, add some sort of "Connect" button and create it there once you actually know the channel through a textbox or something.

Jack T. Spades
  • 986
  • 6
  • 9