1

I apologize for the stupid question but I'm new to C# and have not been able to find help from the other questions related to this error.

I've created a class called "Sender" but I can't access my method when I instantiate an object. When I try and call my method I get the error: "object does not contain a definition for sendMessage." I don't know where I am going wrong. Please point me in the right direction.

Thanks for your help!

This is my Sender class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; namespace CS5200_HW1_GUI { public class Sender { private UdpClient myUdpClient; private IPEndPoint localEP; string ipAddress = "129.123.41.1"; string _port = "12001"; public void sendMessage() { } // byte[] sendBuffer = Encoding.Unicode.GetBytes(command); // int result = myUdpClient.Send(sendBuffer, sendBuffer.Length, localEP); } } 

This is where I'm trying to call the method:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; namespace CS5200_HW1_GUI { public partial class Form1 : Form { //private UdpClient myUdpClient; IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 0); string command = string.Empty; string newWord = String.Empty; string guess = String.Empty; string dashes = String.Empty; string newAddress = string.Empty; string newPort = string.Empty; int score = 0; int length = 0; StringBuilder sb = new StringBuilder(); Random random = new Random(); Sender sender = new Sender(); private void button1_Click(object sender, EventArgs e) { command = "NewGame"; sender.sendMessage(command); //I get the error here newWord = receiver.receiveMessage(); length = newWord.Length; } } } 
1
  • sendMessage method doesnt take an argument should be sendMessage() not sendMessage(command) Commented Sep 5, 2014 at 16:39

2 Answers 2

7

This happens because sender is both an Sender object and also one of the arguments of the click handler.

Pay attention to the method signature:

private void button1_Click(object sender, EventArgs e) 

You are referring to the closest sender, which is of type object. To solve this you have to rename either or call it with this.

Example:

this.sender.sendMessage() 
Sign up to request clarification or add additional context in comments.

3 Comments

I totally missed the scope - good catch @Areks! He's also got a problem with the parameter passing.
Thank you! As soon as I am allowed to do so, I will accept your answer.
You are welcome. But pay attention to Bradley's answer. He's also correct about they way you are calling the method. You are passing an argument to a method which can not accept any (because you declared it that way).
3

You are calling sender.sendMessage with a argument (command)

The only function called sendMessage on the Sender object does not take any parameters, so a matching method is not found.

You also are using the sender variable, which is a variable within the Click method (due to the signature). You should use a different name.

3 Comments

then the error will be like there is no overloaded method
@Shaharyar I noticed the other error too late :( It will still be a problem after he fixes the other error though.
Yes you are right. There are two errors, but method not found error comes before the overloaded method error. +1 for catching another error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.