I need an insight into 'Static'

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

I am trying to understand why this works ...

using (Process ProcRegEdit = Process .Start("REGEDIT.EXE", "/s
C:\\Ajay\\VS2005\\C#\\REGISTRY\\ajay.reg"))
{

ProcRegEdit.WaitForExit(); // Wait indefinitely
if (ProcRegEdit.HasExited)
{
ProcRegEdit.Close();
}

... but this does not.

using (Process ProcRegEdit = new Process())
{
ProcRegEdit.Start("REGEDIT.EXE", "/s
C:\\Ajay\\VS2005\\C#\\REGISTRY\\ajay.reg");
ProcRegEdit.WaitForExit(); // Wait indefinitely
if (ProcRegEdit.HasExited)
{
ProcRegEdit.Close();
}

}
/* NOTES
This line ...

ProcRegEdit.Start("REGEDIT.EXE", "/s
C:\\Ajay\\VS2005\\C#\\REGISTRY\\ajay.reg");

.... generates a build error with the following message

Static member 'member' cannot be accessed with an instance reference;
qualify it with a type name instead
*/

Thanks.
 
AA2e72E said:
I am trying to understand why this works ...

This line ...

ProcRegEdit.Start("REGEDIT.EXE", "/s
C:\\Ajay\\VS2005\\C#\\REGISTRY\\ajay.reg");

... generates a build error with the following message

Static member 'member' cannot be accessed with an instance reference;
qualify it with a type name instead

You've used ProcRegEdit.Start instead of Process.Start. You aren't
allow to try to use a static member (which Start is) as an instance
member - it can lead to confusion and very misleading code.


As a side note, it would be clearer which calls were static and which
were instance if you'd use camel case for your variable - procRegEdit
instead of ProcRegEdit.
 
AA2e72E said:
[...]

This line ...

ProcRegEdit.Start("REGEDIT.EXE", "/s
C:\\Ajay\\VS2005\\C#\\REGISTRY\\ajay.reg");

... generates a build error with the following message

Static member 'member' cannot be accessed with an instance reference;
qualify it with a type name instead

[...]


The general idea behind a static member is to have a helper method that
doesn't need an instance in memory to do it's job. You can think of them as
synonymous to library routines. They are members of the Class and not the
Object.

In your case that works, you are starting regedit in a new process and
giving that process a friendly name.

In your case that does not work, you are (in logic) creating a process then
trying to start regedit in it. But regedit (or whatever the case may be)
needs it's own process.

It's a bit more complicated but this is the basic idea.
 
Thanks for the explanations.

I think with the statement <they are members of the Class and not the Object
 
Back
Top