Problem when I move an array declaration from within a method, to start of class?

G

garyusenet

Hello everyone I hope you are having a good evening.

This evening I made a method that allows me to find the process id's of
running processes of a given 'friendly name' - using
System.Diagnostic.Process.

Here is my code that works fine: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ProcessFun
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}

private Process[] ProcessesToArray()
{
Process[] ProcessArray =
Process.GetProcessesByName("someprocess");
return ProcessArray;
}

private void DisplayProcessID(Process[] procArray)
{
if (procArray.Length == 1)
{
MessageBox.Show(procArray[0].Id.ToString());
return;
}
if (procArray.Length > 1)
{
string procIDs = "";
foreach(Process p in procArray)
{
procIDs += "\n" + p.Id.ToString();

}
MessageBox.Show(procIDs);
return;
}
MessageBox.Show("No Processes are running!");
return;

}

private void Form1_Load(object sender, EventArgs e)
{

DisplayProcessID(ProcessArray);

}
}
}

I thought it would be better to declare my process Array after the
class declaration so that any methods that are part of the class will
have direct access to it. But when I moved the declaration to
underneath the class declaration, so that the programme now started
like so: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Daplayaround
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}
....

The compiler told me I was not permitted to do this.

----------------------
Error 1
A field initializer cannot reference the nonstatic field, method, or
property...
----------------------

What have I done wrong here?

Also would you please comment on the overall decency of the first piece
of code I pasted, and if i should be using a different nameing
convention or style please let me know. As I am new to this, but trying
to learn things properly.

Thankyou all,

Gary-
 
B

Bob Powell [MVP]

You can't call an instance member method from outside of the constructor
during initialisation.

If the member method was static it'd succeed.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



Hello everyone I hope you are having a good evening.

This evening I made a method that allows me to find the process id's of
running processes of a given 'friendly name' - using
System.Diagnostic.Process.

Here is my code that works fine: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ProcessFun
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}

private Process[] ProcessesToArray()
{
Process[] ProcessArray =
Process.GetProcessesByName("someprocess");
return ProcessArray;
}

private void DisplayProcessID(Process[] procArray)
{
if (procArray.Length == 1)
{
MessageBox.Show(procArray[0].Id.ToString());
return;
}
if (procArray.Length > 1)
{
string procIDs = "";
foreach(Process p in procArray)
{
procIDs += "\n" + p.Id.ToString();

}
MessageBox.Show(procIDs);
return;
}
MessageBox.Show("No Processes are running!");
return;

}

private void Form1_Load(object sender, EventArgs e)
{

DisplayProcessID(ProcessArray);

}
}
}

I thought it would be better to declare my process Array after the
class declaration so that any methods that are part of the class will
have direct access to it. But when I moved the declaration to
underneath the class declaration, so that the programme now started
like so: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Daplayaround
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}
...

The compiler told me I was not permitted to do this.

----------------------
Error 1
A field initializer cannot reference the nonstatic field, method, or
property...
----------------------

What have I done wrong here?

Also would you please comment on the overall decency of the first piece
of code I pasted, and if i should be using a different nameing
convention or style please let me know. As I am new to this, but trying
to learn things properly.

Thankyou all,

Gary-
 
G

garyusenet

Making it Static certainly fixed it.

I know that Static means the method exists on the class and not class
instances, but I don't understand really why it not being static
created a problem here.

I will read more on Static Methods tomorrow morning.

Thankyou very much,

Gary-
You can't call an instance member method from outside of the constructor
during initialisation.

If the member method was static it'd succeed.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



Hello everyone I hope you are having a good evening.

This evening I made a method that allows me to find the process id's of
running processes of a given 'friendly name' - using
System.Diagnostic.Process.

Here is my code that works fine: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ProcessFun
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}

private Process[] ProcessesToArray()
{
Process[] ProcessArray =
Process.GetProcessesByName("someprocess");
return ProcessArray;
}

private void DisplayProcessID(Process[] procArray)
{
if (procArray.Length == 1)
{
MessageBox.Show(procArray[0].Id.ToString());
return;
}
if (procArray.Length > 1)
{
string procIDs = "";
foreach(Process p in procArray)
{
procIDs += "\n" + p.Id.ToString();

}
MessageBox.Show(procIDs);
return;
}
MessageBox.Show("No Processes are running!");
return;

}

private void Form1_Load(object sender, EventArgs e)
{

DisplayProcessID(ProcessArray);

}
}
}

I thought it would be better to declare my process Array after the
class declaration so that any methods that are part of the class will
have direct access to it. But when I moved the declaration to
underneath the class declaration, so that the programme now started
like so: -

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Daplayaround
{
public partial class Form1 : Form
{
Process[] ProcessArray = ProcessesToArray();

public Form1()
{
InitializeComponent();
}
...

The compiler told me I was not permitted to do this.

----------------------
Error 1
A field initializer cannot reference the nonstatic field, method, or
property...
----------------------

What have I done wrong here?

Also would you please comment on the overall decency of the first piece
of code I pasted, and if i should be using a different nameing
convention or style please let me know. As I am new to this, but trying
to learn things properly.

Thankyou all,

Gary-
 
J

Jon Skeet [C# MVP]

Making it Static certainly fixed it.

I know that Static means the method exists on the class and not class
instances, but I don't understand really why it not being static
created a problem here.

Most instance methods use or change the state of an object. When the
object's initializers are being called, the object's state is
incomplete. While the semantics could be strictly defined, the C#
language designers thought it would be safer to prohibit this. It can
be a pain occasionally, but it's usually not an issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top