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();
        }
5
  • Where exactly do you get this error? Commented May 5, 2021 at 18:50
  • Does this answer your question? A field initializer cannot reference the nonstatic field, method, or property Commented May 5, 2021 at 18:56
  • @KlausGütter when I change "channel" with string channel Commented May 5, 2021 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. Commented May 5, 2021 at 19:02
  • @KlausGütter I have edited the question, maybe its better like this. Commented May 5, 2021 at 19:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.