Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 675831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:42+00:00 2026-05-14T00:51:42+00:00

I’ve recently begun learning C# but have encountered an annoying problem. Every variable I

  • 0

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

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:51:42+00:00Added an answer on May 14, 2026 at 12:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.