Reputation: 561
I am writing a data received event For Serial IO port...the following is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO; //add namespaces
using System.IO.Ports;
public class Program
{
//define a delegate class to handle DataReceived events
internal delegate void SerialDataReceivedEventHanderDelegate(object sender,SerialDataReceivedEventArgs e);
internal void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//place code to read and process data here
}
static void Main()
{
string myComPortName=null;
//1. find available COM port
string[] nameArray = null;
nameArray = SerialPort.GetPortNames();
if(nameArray.GetUpperBound(0)>0) {
myComPortName = nameArray[0];
}
else {
Console.WriteLine("Error");
return;
}
//2. create a serialport object
// the port object is closed automatically by use using()
SerialPort myComPort = new SerialPort();
myComPort.PortName = myComPortName; //the default paramit are 9600,no parity,one stop bit, and no flow control
private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler(ComPort.DataReceived);
myComPort.DataReceived+=SerialDataReceivedEventHandler1;
}
}
Why in VS2010, Line: private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 = new SerialDataReceivedEventHandler(ComPort.DataReceived);
is showing me: 1. invalid expression term 'private' 2. the modifier 'static' is not valid for this term 3. should I use Comport here? or just DataReceived...since its the function name
Thanks for your reply.
Upvotes: 0
Views: 3575
Reputation: 2523
private qualifier applies only for a member (member variables/methods) in your class definition. The SerialDataReceivedEventHandler is just a variable which you have inside a method.
Upvotes: 0
Reputation: 4346
private
and static
are not valid in the scope of a method.
You can only declare static items at class level - i.e. here:
public class Program
{
private static object _memberField;
private static void MemberMethod()
{
// not here:
// private static object _insideMethod; // <- will not work
}
}
Upvotes: 2
Reputation: 4932
That line is inside the main method. Private and static are only allowed outside of methods.
Upvotes: 0
Reputation: 141588
private
is used to control the accessibility of members of a type, or types themselves. It is not applicable to a variable that is part of a method, since it's already most accessible to the method itself.
Additionally, the static modifier is not valid as well. It also applies to members of a type to indicate whether or not that member is instance aware.
Your line:
private static SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler(ComPort.DataReceived);
Should be:
SerialDataReceivedEventHandler SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler(ComPort.DataReceived);
However, that line seems entirely unnecessary in the first place. You could simplify it to:
myComPort.DataReceived += ComPort.DataReceived;
Upvotes: 0
Reputation: 44288
dont need private static, just go :-
var SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler(ComPort.DataReceived);
but if you are wanting that to be a member of the Program class, Move a declaration out to :-
public class Program
{
private static SerialDataReceivedEventHandler;
then you need in Main
SerialDataReceivedEventHandler1 =
new SerialDataReceivedEventHandler(ComPort.DataReceived);
but more likely, you really want to start your own class, because "Program" isn't really the best place.... and dealing with static classes and static methods is a bit messy
Upvotes: 1