Home > Enterprise >  I am getting an object must be marked static error when nothing in my c# program is marked static?
I am getting an object must be marked static error when nothing in my c# program is marked static?

Time:12-07

So I am writing a C# program in which the server will encode a selected file into bytes and then send it to a client computer. However, for a reason I can't find, I am getting a "An object reference is required for the non-static field, method, or property 'Encoding.GetBytes(string)' " error, which is very odd as seen in the code below, nothing in the entire code is set to public or static so not sure why it would be generating this error? The line where Visual Studio throws the error is marked below. Any thoughts are appreciated. Thank you.

void SendFile_Click(object sender, EventArgs e)
        {

            try
            {
                FileSendProgress.Maximum = 100;
                IPAddress ipAd = IPAddress.Parse(ClientIP.Text); //use local m/c IP address, and use the same in the client

                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);

                /* Start Listeneting at the specified port */
                myList.Start();

                MessageBox.Show("The server is running at port 8001...");
                MessageBox.Show("The local End point is  :"   myList.LocalEndpoint);
                MessageBox.Show("Looking for other computer");

                Socket s = myList.AcceptSocket();
                FSStatus.Text = "Connnected to client computer......Sending data now, hold on to your hats. "   FileSendProgress.Value.ToString();
                FileSendProgress.Value  = 5;
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(DURL.Text)); // This is the line where the error is thrown.
                MessageBox.Show("The selected file : "   FileToSend.Text   "is now on it's way to the client computer. It may take a while if a large file is being sent.");

                byte[] b = new byte[100];
                int k = s.Receive(b);
                for (int i = 0; i < k; i  )
                    Console.Write(Convert.ToChar(b[i]));
                FileSendProgress.Value  = 45;

                FSStatus.Text = "Encoding........ "   FileSendProgress.Value.ToString();
                /* clean up */



                byte[] fileContent = null;
                System.IO.FileStream fs = new System.IO.FileStream(FileToSend.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
                long byteLength = new System.IO.FileInfo(FileToSend.Text).Length;
                fileContent = binaryReader.ReadBytes((Int32)byteLength);
                byte[] bb = new byte[1024];

                FSStatus.Text = "Transmitting now...... "   FileSendProgress.Value.ToString();
                string file = Encoding.UTF8.GetString(bb.AsSpan(0, k));
                FileSendProgress.Value  = 15;
                s.Send(ASCIIEncoding.GetBytes(file));
                    fs.Close();
                    fs.Dispose();

                    binaryReader.Close();
                    s.Close();
                    myList.Stop(); 
                FileSendProgress.Value  = 35;
             
                FSStatus.Text = "File Sent "   FileSendProgress.Value.ToString();


            }

                catch (Exception n)
                {
                    MessageBox.Show("Please make sure the file is selected, or the file is not supported. "   n);
                }
            
          
            }

CodePudding user response:

GetBytes() is not a static method. You probably want to have:

asen.GetBytes(file)

instead of

ASCIIEncoding.GetBytes(file)

CodePudding user response:

Try Encoding.ASCII.GetBytes(file) instead of ASCIIEncoding.GetBytes(file).

  • Related