[英]C# object does not contain a definition for method
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. 我为这个愚蠢的问题表示歉意,但我是C#的新手,因此无法从其他与此错误有关的问题中寻求帮助。
I've created a class called "Sender" but I can't access my method when I instantiate an object. 我创建了一个名为“ Sender”的类,但是在实例化对象时无法访问我的方法。 When I try and call my method I get the error: "object does not contain a definition for sendMessage."
当我尝试调用我的方法时,出现错误:“对象不包含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;
}
}
}
This happens because sender is both an Sender object and also one of the arguments of the click handler. 发生这种情况是因为sender既是Sender对象,也是单击处理程序的参数之一。
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
. 您指的是最接近的发件人,其类型为
object
。 To solve this you have to rename either or call it with this
. 要解决此问题,您必须重命名或使用
this
进行重命名。
Example: 例:
this.sender.sendMessage()
You are calling sender.sendMessage
with a argument ( command
) 您正在调用带有参数(
command
)的sender.sendMessage
。
The only function called sendMessage
on the Sender
object does not take any parameters, so a matching method is not found. Sender
对象上唯一名为sendMessage
函数没有任何参数,因此找不到匹配方法。
You also are using the sender
variable, which is a variable within the Click
method (due to the signature). 您还使用了
sender
变量,该变量是Click
方法中的变量(由于签名)。 You should use a different name. 您应该使用其他名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.