Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Cannot convert lambda expressions

error cannot convert inaccessible

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 googooli86

googooli86

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 31 March 2015 - 11:23 AM

Hi,

I get the following error:

Cannot convert lambda expression to type system delegate because it's not a delegate type (on invoke).
The Second error is: Client.PrivateChat.txtReceive is inaccessible due to its protection level..
Have tried for hours but can not figure this out...

 

Here is my code:

namespace Client
{
    public partial class Main : Form
    {
        private readonly Listener listener;

        public List<Socket> clients = new List<Socket>();

        public void BroadcastData(string data)
        {
            foreach (var socket in clients)
            {
                try { socket.Send(Encoding.UTF8.GetBytes(data)); }
                catch (Exception) { }
            }
        }

        public Main()
        {
            pChat = new PrivateChat(this);
            InitializeComponent();
            listener = new Listener(2014);
            listener.SocketAccepted += listener_SocketAccepted;
        }

        private void listener_SocketAccepted(Socket e)
        {
            var client = new Client(e);
            client.Received += client_Received;
            client.Disconnected += client_Disconnected;
            this.Invoke(() =>
                {
                    string ip = client.Ip.ToString().Split(':')[0];
                    var item = new ListViewItem (ip);
                    item.SubItems.Add(" ");
                    item.SubItems.Add(" ");
                    item.Tag = client;
                    clientList.Items.Add(item);
                    clients.Add(e);
                });
        }

        private void client_Disconnected(Client sender)
        {
            this.Invoke(() =>
                {
                    for (int i = 0; i < clientList.Items.Count; i++)
                    {
                        var client = clientList.Items[i].Tag as Client;
                        if (client.Ip == sender.Ip)
                        {
                            txtReceive.Text += "<< " + clientList.Items[i].SubItems[1].Text + "has left the room >>\r\n";
                            BroadcastData("RefreshChat|" + txtReceive.Text);
                            clientList.Items.RemoveAt(i);
                        }
                    }
                });
        }

        private PrivateChat pChat;

        private void client_Received(Client sender, byte[] data)
        {
            this.Invoke(() =>
                {
                    for (int i = 0; i < clientList.Items.Count; i++)
                    {
                        var client = clientList.Items[i].Tag as Client;
                        if (client == null || client.Ip != sender.Ip) continue;
                        var command = Encoding.UTF8.GetString(data).Split('|');
                        switch (command[0])
                        {
                            case "Connect":
                                txtReceive.Text += "<< " + command[1] + "joined the room >>\r\n";
                                clientList.Items[i].SubItems[1].Text = command[1]; //Nickname
                                clientList.Items[i].SubItems[2].Text = command[2]; //Status
                                string users = string.Empty;
                                for (int j = 0; j < clientList.Items.Count; j++)
                                {
                                    users += clientList.Items[j].SubItems[1].Text + "|";
                                }
                                BroadcastData("Users|" + users.TrimEnd('|'));
                                BroadcastData("RefreshChat|" + txtReceive.Text);
                                break;
                            case "Message":
                                txtReceive.Text += command[1] + " says: " + command[2] + "\r\n";
                                BroadcastData("RefreshChat|" + txtReceive.Text);
                                break;
                            case "pMessage":
                                this.Invoke(() =>
                                    {
                                        pChat.txtReceive.Text += command[1] + " says: " + command[2] + "\r\n";

                                    });
                                break;
                            case "pChat":
                                this.Invoke(() =>
                                    {
                                        pChat.txtReceive.Text += command[i] + " says " + command[2] + "\r\n";
                                    });
                                break;
                        }
                    }
                });
        }


        private void Main_Load(object sender, EventArgs e)
        {
            listener.Start();
        }

       protected void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            listener.Stop();
        }

       private void btnSend_Click(object sender, EventArgs e)
       {
           if (txtInput.Text != string.Empty)
           {
               BroadcastData("Message|" + txtInput.Text);
               txtReceive.Text += txtInput.Text + "\r\n";
               txtInput.Text = "Admin says: ";
           }
       }

       private void disconnectClientToolStripMenuItem_Click(object sender, EventArgs e)
       {
           foreach (var client in from ListViewItem item in clientList.SelectedItems select (Client)item.Tag)
           {
               client.Send("Disconnect|");

           }
       }

     
        private void chatWithClientToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            foreach (var client in from ListViewItem item in clientList.SelectedItems select (Client)item.Tag)
            {
                client.Send("Chat|");
                pChat = new PrivateChat(this);
                pChat.Show();
            }
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSend.PerformClick();
            }
        }

        private void txtReceive_TextChanged(object sender, EventArgs e)
        {
            txtReceive.Selectionstart = txtReceive.TextLength;
        }

     

    }
}





#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 31 March 2015 - 05:47 PM

First, try pChat as public instead of private, then, which line number does it point to for the lambda expression error?



#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 04 April 2015 - 01:49 PM

You need to add a reference to System.Net.Sockets in the project.

It's in Solution Explorer -> your project -> references -> Add new reference.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download