Reputation: 6979
I am trying to define a property inside the main class in my Windows Service. The property will be used to retrieve the name of the process whenever required.
For eg:
public string PName
{
return SomeProcess.Name;
}
public string PID
{
return SomeProcess.ProcessId;
}
public Process SomeProcess
{
private Process[] myProcess = Process.GetProcessesByName("notepad"); //Process is underlined here with red wavy line saying "A get or set accessor method is expected"
get
{return myProcess[0];}
}
The problem is inside the SomeProcess property written in the comment. What am I doing wrong here?
Upvotes: 1
Views: 176
Reputation: 1880
I think you are at very beginner level, you should refer to syntax of the language code, find the below code for C#
public class ProcessInfo
{
private Process[] myProcess = Process.GetProcessesByName("notepad"); //Process is underlined here with red wavy line saying "A get or set accessor method is expected"
public Process SomeProcess
{
get
{
return myProcess[0];
}
}
public string PName
{
get
{
return SomeProcess.ProcessName;
}
}
public int PID
{
get
{
return SomeProcess.Id;
}
}
}
Upvotes: 0
Reputation: 63065
if you want to declare private variable declare inside get
method in property.
as per your code, you need to check whether GetProcessesByName
return process or not before accessing myProcess[0]
. you can avoid all these validation by using FirstOrDefault
as below. if no results it will return null.
public Process SomeProcess
{
get
{
return Process.GetProcessesByName("notepad").FirstOrDefault();
}
}
There is a problem in other properties too. you access properties of SomeProcess
without check null.
public string PName
{
return SomeProcess==null? string.Empty:SomeProcess.Name;
}
public string PID
{
return SomeProcess==null? string.Empty:SomeProcess.ProcessId;
}
Upvotes: 0
Reputation: 50672
Make it like this:
private Process[] myProcess = Process.GetProcessesByName("notepad");
public Process SomeProcess
{
get
{
return myProcess[0];
}
}
or
public Process SomeProcess
{
get
{
Process[] myProcess = Process.GetProcessesByName("notepad");
return myProcess[0];
}
}
EDIT
Note that you need to decide when you want to get the processes. If you do it as I showed in my first sample the process will be retrieved when the class is instantiated. The second way is more robust as it will retrieve the process when you ask for the value of the property.
I stated both answers because you asked what the error means and is more about private and local variables.
Upvotes: 4
Reputation: 11403
Try this:
public Process SomeProcess
{
get
{
Process[] myProcess = Process.GetProcessesByName("notepad");
return myProcess[0];
}
}
Or this:
private Process[] myProcess = Process.GetProcessesByName("notepad");
public Process SomeProcess
{
get
{
return myProcess[0];
}
}
Either declare myProcess
as a local variable inside the getter of SomeProcess
or declare it as a private field inside the class if you want to use it elsewhere in the class. You can use accessors (private/public/etc.) on fields/methods/classes, not on local variables.
Upvotes: 1
Reputation: 30698
Accessors (public/private/protected/internals) cannot be appplied on function local variables.
Restrictions on Using Accessibility levels
Upvotes: 0