I’ve recently begun learning C# but have encountered an annoying problem. Every variable I want available to all functions in my program I have to put a “static” in front of and also every function. What I’d like to know is how to avoid this, if possible?
Also, small side question: creating public variables inside functions?
This is what my program looks like right now, and I want to basically keep it like that, without having to add “static” everywhere:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace NetworkExercise
{
class Client
{
public IPAddress addr;
public int port;
public string name;
public Thread thread;
public TcpClient tcp;
public NetworkStream stream;
public Client(IPAddress addr, int port, string name, NetworkStream stream)
{
}
}
class Program
{
//NETWORK
TcpListener tcpListener;
Thread listenThread;
ASCIIEncoding encoder = new ASCIIEncoding();
//DATA
byte[] buffer = new byte[4096];
string servIp;
int servPort;
//CLIENT MANAGEMENT
int clientNum;
static void Main(string[] args)
{
beginConnect();
}
public void beginConnect()
{
Console.Write("Server IP (leave blank if you're the host): ");
servIp = Console.ReadLine();
Console.Write("Port: ");
servPort = Console.Read();
tcpListener = new TcpListener(IPAddress.Any, servPort);
listenThread = new Thread(new ThreadStart(listenForClients));
listenThread.Start();
}
public void listenForClients()
{
tcpListener.Start();
Console.WriteLine("Listening for clients...");
while (true)
{
Client cl = new Client(null, servPort, null, null);
cl.tcp = tcpListener.AcceptTcpClient();
ThreadStart pts = delegate { handleClientCom(cl); };
cl.thread = new Thread(pts);
cl.thread.Start();
}
}
public void handleClientCom(Client cl)
{
cl.stream = cl.tcp.GetStream();
}
}
}
Using global variables is generally considered bad practice as they increase coupling and damage maintainability, so you should rethink your approach if you find yourself using them often. If all of your code uses the same few variables, you can have a very difficult time debugging it, since you have to track the global state of the system and you’ve no idea which part of the program has been modifying it.
Also, you should almost never use public fields in classes, for similar reasons. Doing so allows client code to become tied directly into the implementation of your class such that if you change the class’s internal mechanics then the client code breaks. What you should be using is properties.
To answer your question, though: no, if you want a member to be globally accessible, it must be static. Otherwise it exists only on an object, to which you would need a reference in order to access it.
Some key ideas you might want to read about here are dependency injection and encapsulation.