这种情况最近开始发生,每次我使用一段代码都认为它是错误的。
例如:
假设我想做一个套接字
public class PacketReader
{
Socket MainSocket;
MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); // acts like MainSocket was never declared
// Bind the socket to the selected IP address
MainSocket.Bind(newIPEndPoint(IPAddress.Parse(cmbInterfaces.Text),0));
// Set the socket options
MainSocket.SetSocketOption(SocketOptionLevel.IP, //Applies only to IP packets
SocketOptionName.HeaderIncluded, //Set the include header
true); //option to true
byte[] byTrue = newbyte[4]{1, 0, 0, 0};
byte[] byOut = newbyte[4];
//Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
MainSocket.IOControl(IOControlCode.ReceiveAll, //SIO_RCVALL of Winsock
byTrue, byOut);
//Start receiving the packets asynchronously
MainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
newAsyncCallback(OnReceive), null);
}
然后当我尝试在函数或类的某些部分中使用它时,它会将其返回为“名称是一个字段,但它用作一种类型”。当我所做的只是在课堂上声明它时,我不知道如何将它用作一种类型。
所以我的问题是,我该如何防止这种情况发生。我需要快速找到解决方案,否则我会因为这个愚蠢的问题而使一个月内到期的项目失败。