我在visual studio express 2013中创建了一个C#dll。我正在为我的程序的一部分创建一个dll,以便可以在需要时调用它。我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ClassLibrary2
{
public class take_off
{
UdpClient udpClient = new UdpClient(5556);
IPAddress send_to =IPAddress.Parse("192.168.1.1");
IPEndPoint sending_end_point =new IPEndPoint(send_to,5556);
int seq;
for(int i=0;i<30;i++)
{
System.Console.WriteLine("send landing command");
string buff1= String.Format("AT*REF={0},290717696\r",seq);
seq=seq+1;
UdpClient.Send(buff1, sending_end_point);
}
}
}
我收到的错误很少:
Error 1 Identifier expected
Error 9 Invalid token '(' in class, struct, or interface member declaration
Error 11 Invalid token ')' in class, struct, or interface member declaration
Error 10 Invalid token ',' in class, struct, or interface member declaration
Error 8 Invalid token '+' in class, struct, or interface member declaration
Error 6 Invalid token '++' in class, struct, or interface member declaration
Error 7 Invalid token '=' in class, struct, or interface member declaration
Error 4 Invalid token '30' in class, struct, or interface member declaration
Error 3 Invalid token 'for' in class, struct, or interface member declaration
Error 5 Type expected
Error 2 Type or namespace definition, or end-of-file expected
Error 12 Type or namespace definition, or end-of-file expected
我写的语法有错误吗?我是C#的新手。 为什么会导致错误?
答案 0 :(得分:5)
您无法将代码放入您的课程中。您需要使用代码定义af方法。喜欢
public void MyMethod()
{
//you code here
}
答案 1 :(得分:2)
语法错误太多了。在尝试做某事之前,你应该学习基础知识。
以下是您可以尝试的没有错误的代码,它可以帮助您继续前进。感谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ClassLibrary2
{
public class take_off
{
public void testfunction()
{
UdpClient udpClient = new UdpClient(5556);
IPAddress ipAddress = IPAddress.Parse("192.168.1.1");
IPEndPoint sending_end_point = new IPEndPoint(ipAddress, 5556);
int seq = 0;
for(int i=0; i<30; i++)
{
seq = seq + 1;
System.Console.WriteLine("send landing command");
string buff1= String.Format("AT*REF={0},290717696\r",seq);
byte[] bytesToSend = Encoding.ASCII.GetBytes(buff1);
udpClient.Send(bytesToSend,bytesToSend.Length,sending_end_point);
}
}
}
}
现在,您可以在包含DLL文件的任何代码中调用此testFunction。感谢。