Inheriting from array of System.Diagnostics.Process[]

G

Gamma

I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[]. So why do I get
"System.InvalidCastException" all the time ? ("Specified cast is not
valid")


using System.Diagnostics;
using System.Windows.Forms;
..
..
..
namespace dbg
{
public class _Process: Process
{
public override String ToString()
{
return this.ProcessName;
}
};
..
..
..
private void InitializeComponent() // of the ListBox
{
 
D

David Browne

Gamma said:
I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[]. So why do I get
"System.InvalidCastException" all the time ? ("Specified cast is not
valid")

I think you're using the wrong OO techniques here. Why are you inheriting
from Process? Do you have a different kind of process? Is your type a
Process? No. Perhaps it's a wrapper for a Process, or has a list of
Process.

David
 
C

Chris Mullins

Gamma said:
I'm trying to create a ListBox that holds list of currently running
processes. So I inherited from Process a classes named _Process, which
it's objects are to be held in the ListBox (I just overrode the
"ToString"). Using the "GetProcesses" method, I get Process[], that I
want to cast to _Process[].

You're pretty close - here's the code I got working:
(I'm assuming you have a Form with "ListBox1" on it...)


private void button1_Click(object sender, EventArgs e)
{
// Get the list of processes
Process[] p = Process.GetProcesses();

// Convert that array to our array. We overload ToString() so that the
// ListBox can actually do soemthing meaninfull.
ProcessWrapper []pw = Array.ConvertAll<Process, ProcessWrapper>
(p, new Converter<Process, ProcessWrapper>(ProcessToWrapper));

// Create a new collection of items for the list box.
ListBox.ObjectCollection o = new ListBox.ObjectCollection(listBox1);

// Add in all our processes.
o.AddRange(pw);
}

// This does the conversion
private ProcessWrapper ProcessToWrapper(Process p)
{
return new ProcessWrapper(p);
}

public class ProcessWrapper
{
private Process _process;
public ProcessWrapper(Process p)
{
_process = p;
}
public override string ToString()
{
return _process.ProcessName;
}
}
 
G

Gamma

Thank you very much for your quick responses. I would like to
understand what did I do wrong, technically. Why am I not able to
inherit a new class from Process ? (although it may not be correct in
terms of OO).

btw - The exception is raised also when I cast a single _Process
instance from Process (not an array!).
 
C

Chris Mullins

You really didn't do too much wrong from an OO perspective.

Your "MyProcess" class, which inherits from "Process" still passes the "IS
A" test that really is what defines inheritence.

Unfortuantly, there's no way in .NET (or any OO language, that I know of) to
take an existing collection of Processes and up-convert it to your more
specialized class.

For this to have worked, the GetAllProcesses() method would have needed to
take another parameter - one that told it the type it needs to create. This
way it could create "MyProcess" entries instead of "Process" entries.

The solution I posted used aggrigation - I wrote a wrapper (not an
extension, but a wrapper - my wrapper does NOT pass the "Is A" test) that
was able to be constructed from a process. I converted all the "Process"
classes to "ProcessWrappers" then bound that wrapper to the list box.
 
G

Gamma

Once again, thank you for your quick response :)

I still don't understand - once I have received the Process[] array, I
had an array of Process(es). Theoretically, I should have been able to
cast even a single object from the array (let's say Process[0]) to a
single _Process object. From some reason, it raised an exception. To be
honest, I'm pretty sure it's a simple thing, like me forgetting to
override some method or something of that kind.

A simple cast, like:

Process p = new Process;
_Process _p;
_p = (_Process) p; // Exception !

raised an exception. That's what I don't understand - what should I do
in order for that example to succeed ?

(Once I manage to cast a single object, I pass it through "foreach" and
the rest is history...)
 
C

Chris Mullins

Gamma said:
A simple cast, like:

Process p = new Process;
_Process _p;
_p = (_Process) p; // Exception !

I'm assuming:
class _Process inherits Process

The problem is that you're backwards. The "Is a" test jumps up:
Because _Process inherits from Process, you can safely say that "_Process IS
A Process". This is polymorphism.

.... BUT (and this is a big but) you CANNOT say that "Process IS A _Process".

Let's extend the _Process class to do something interesting:
public class _Process : Process
{
public void CreateRemoteMainframeProcess(){...}
}

Now what? Your original Process has no definition for
"CreateRemoteMainframeProces", this method was added by the derived _Process
Class. So now, more clearly, "Process" IS NOT "_Process" because it has no
way to create a mainframe process.

I don't think my explination is very good. Hrm.
 
G

Gamma

O.k, that makes sense (and your explanation was perfect), and a bit
embarrassing for not noticing it myself. So I corrected it and tried
again:

Process p = new Process;
Process _p;
_p = (_Process) p; // Exception !

Since I didn't add new propeties, but methods only, I can't see what's
wrong in the rectified segment above. In fact, I'm guessing that the
part that raises the exception is nothing but the cast:

(_Process) p

I miss C++ so much... :-s
 

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