By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

Inheriting from array of System.Diagnostics.Process[]

P: n/a
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
{
Sep 30 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a


"Gamma" <Ta******@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
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

Sep 30 '06 #2

P: n/a

"Gamma" <Ta******@gmail.comwrote:
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;
}
}

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Sep 30 '06 #3

P: n/a
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!).

Oct 3 '06 #4

P: n/a
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"Gamma" <Ta******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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!).

Oct 3 '06 #5

P: n/a
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...)

Oct 5 '06 #6

P: n/a
"Gamma" <Ta******@gmail.comwrote in message
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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Oct 6 '06 #7

P: n/a
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

Oct 9 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.