Sign in to follow this  
Kryptus

Sockets Problem in C#

Recommended Posts

Kryptus    100
I am using the following code to make a console based server application for my chat client. I have only started and I am new to C# (I'm moving from VB.NET) and I get two errors reading: Error 1 An object reference is required for the nonstatic field, method, or property 'My_Server_Command_Prompt.Program.ServerListener' Error 2 An object reference is required for the nonstatic field, method, or property 'My_Server_Command_Prompt.Program.ServerListener'
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace My_Server_Command_Prompt
{
    class Program
    {
        public Thread ServerThread;
        public TcpListener ServerListener;

        static void Main(string[] args)
        {
            ServerListener = new TcpListener(IPAddress.Any, 9000);
            ServerListener.Start();
        }
    }
}

Any help? I have no idea how to solve this.

Share this post


Link to post
Share on other sites
Nypyren    12063
Static member functions (Main in this case) cannot access non-static member variables.
In another way of saying it, you must use an instance of a class in order to create and use non-static member variables.

Solution 1: Make the variables static.
Solution 2: Create an instance of your Program class and have its constructor create the TcpListener.


Since your program is currently really simple, I would use Solution 1:


namespace My_Server_Command_Prompt
{
class Program
{
public static Thread ServerThread;
public static TcpListener ServerListener;

static void Main(string[] args)
{
ServerListener = new TcpListener(IPAddress.Any, 9000);
ServerListener.Start();
}
}
}








In case you haven't used static variables before (I'm not sure what VB.Net calls them), here's the basic summary:

Non-static member variable: Each instance of your class has its own copy of the variable. If you don't have any instances of the class, this kind of variable won't exist (the error message that you get is another way of saying this). You must have an instance of your class in order to access this type of variable.


MyClass object1 = new MyClass();
object1.memberVariableA = 10;

Static member variable: This variable exists once, whether you have 200 instances of your class or NO instances of your class. You do NOT need an instance of the class in order to access the variable. Instead, you use the name of the class itself.


MyClass.memberVariableA = 10;

If you are writing a non-static member function, accessing non-static or static member variables is identical:


class MyClass
{
int memberVariableA;
static int memberVariableB;

public void MyFunction()
{
memberVariableA = 10;
memberVariableB = 20;
}
}


[Edited by - Nypyren on July 1, 2005 2:46:53 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this