Hi C# friends,...
My code goes like this for creating a library.....

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


namespace ClientLibrary
{
    public class ClientLibrary
    {
        private  bool connectionStatus;
        private  Socket oClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private Thread Receiving_Thread = new Thread(receiveFromServer);
        public  String receivedMessage;
        public void connectToMessageServer()
        {
            try
            {
                oClientSocket.Connect("CCPCOMP1", 5000);
                connectionStatus = true;
                Receiving_Thread.Start();
            }
            catch (Exception exep)
            {
                connectionStatus = false;

            }



        }
        public void sendToServer(String messageToBeSent)
        {
            try
            {


                Encoding ASCII = Encoding.ASCII;
                Byte[] messageToBeSentInBytes = ASCII.GetBytes(messageToBeSent);
                oClientSocket.Send(messageToBeSentInBytes);
            }
            catch (Exception exep)
            {
            }
        }

        public  void receiveFromServer()
        {
            while (true)
            {
                try
                {

                    Encoding ASCII = Encoding.ASCII;
                    Byte[] receiveMessageInBytes = new Byte[100];

                    oClientSocket.Receive(receiveMessageInBytes);
                    receivedMessage = ASCII.GetString(receiveMessageInBytes);
                }
                catch (Exception exep)
                {

                }
            }
        }



    }
}

When i compile it i get the error
Error 4 A field initializer cannot reference the nonstatic field, method, or property 'ClientLibrary.ClientLibrary.receiveFromServer()' c:\MyFTP\ClientLibrary\ClientLibrary\ClientLibrary\Main.cs 15 54 ClientLibrary


PLease help me outtt.....

Firstly, please use CODE tags when posting code, there is a sticky from Dani at the top of the forum pages with details on their use.

Secondly, try changing your code to this:

public class ClientLibrary
{
private bool connectionStatus;
private Socket oClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private Thread Receiving_Thread;
public String receivedMessage;

public ClientLibrary()
{
 Receiving_Thread = new Thread(receiveFromServer);
}

The error you received is caused by initialising a class level variable with a non-static value. By moving the assignment to the constructor (as above) you ensure that all the members of the class have been instantiated before referencing them.

Remember to mark the thread as solved if this has helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.