Re: Multiple Undetermined Number of Variables
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 29 Oct 2007 16:11:09 -0400
Jessee,
If you want to monitor all the ports, then you will have to create a
number of SerialPort instances that correspond to the number of ports on the
system.
In your loop that gets the port names, you would add an entry to a
Dictionary<string, SerialPort> using the name of the serial port as a key.
I assume you would also sign up for whatever events you want to listen for
while creating the ports (note, you want to use the SerialPort constructor
that takes the name of the serial port).
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Jessee Holmes" <JesseeHolmes@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:32699D29-1D55-4B92-9832-584146E12346@xxxxxxxxxxxxxxxx
I'm not sure I'm going about this the right away, and I've attempted to
search every online topic that I can which makes me think this is not
possible or I'm simple coding this incorrectly.
What I'd like to do is this:
1) See hot many COM ports are on a computer: foreach (string port in
SerialPort.GetPortNames()) ....
2) Uniquely and simultaneously use these ports: SerialPort
comport[portNumber] = new SerialPort(); <---- PROBLEM
The problem is I wouldn't know at first if a computer had 1 port or 5
ports
so my code needs to be dynamic with this (therefore I use GetPortNames()
to
establish how many there are).
I can't just say SerialPort comport = new SerialPort(); and assign which
port I'm working with programmaticly, because I want to monitor ALL COM
ports
at the same time. Would hate to make 50 different variables too since that
would limit my program to looking at only 50 ports at once. (lol yeah I
know
that's a lot).
Lastly, I've tried MSDN's suggestion: SerialPort[] comport = new
SerialPort[SerialPort.GetPortNames().Length]; But I don't think that works
since I can't access the variable afterwards: comport[portNumber]; unless
I'm
doing it wrong.
Maybe I'm making this more difficult than it needs to be and missing
something obvious here, maybe I'm just too much of a beginner still in C#,
or
maybe this simply isn't possible at all with what I'm trying to do within
C#.
(Booo Urns)!
Can anyone shed some light on this for me? Doesn't have to be in relation
to
this example. Just a simple, string varName[i]; within a for loop to make
10
variables or something would suffice. I read that this is impossible
online
somewhere, so I'm afraid there is no solution; but I'd like to check the
community first to see if others have ran across this problem or if I'm
just
simply dumb. :(
Thank you for your time. Much appreciated.
-Jessee
.
- Prev by Date: Re: CLR Shared Memory, C# DLL load
- Next by Date: Re: Matrix - Transformation
- Previous by thread: Re: Generics Dictionary - modify ContainsKey and Key Lookup
- Next by thread: Re: Multiple Undetermined Number of Variables
- Index(es):
Relevant Pages
|