Multiple Undetermined Number of Variables

G

Guest

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; 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
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
P

Peter Duniho

[...]
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.

In general, if you can't get MSDN sample code to work, then you are
doing it wrong. The MSDN samples are not 100% correct; I've seen bugs
in them myself. But on the whole, the sample code is excellent and in
most cases even where the code could be better, it does work.

So your first thought should always be directed toward how you might be
misunderstanding or misusing the sample code.

Now, as for this particular case: certainly the MSDN code you've
included here sure looks like it ought to do what you want. So either
you haven't used the MSDN code as you've posted it here, or you haven't
described correctly what it is you want, or you have somewhere else
misused the variable declared in the code you've posted.

Since you didn't post any of your own code, it's hard to say exactly
what your mistake is. If you want more specific advice, you should
post a concise example of code that demonstrates the problem you're
having. In this case, I suspect that just the immediate context (that
is, the declaration of the variable and the code that uses the
variable) is sufficient, rather than posting an entire program. Do
make sure that you explain in detail where you are getting an error or
other problem, and what that error or other problem is.

Pete
 
G

Guest

Jessee said:
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.

You must be missing some of the code from the example. That code creates
an array for holding references to one SerialPort object for each serial
port, but it doesn't create the SerialPort objects. You have to loop
through the serial ports to get a SerialPort object for each, and put
them in the array.
 
B

bryan

I"m betting that you are NOT allocating the individual SerialPort instances.

After you do this,
SerialPort[] ports = new SerialPort[SerialPort.GetPortNames().Length];

Do something like this

int index=0;
foreach (string portName in SerialPort.GetPortNames())

ports[index++] = new SerialPort(portName);
 
G

Guest

Thank you everyone,

Wasn't able to work on the code again until just recently, but got it
figured out thanks to all of your help in pointing me in the right direction.

Peter,

You're right, was on the right trail with the MSDN code and it was correct.
Since yesterday though, I can't remember what I did wrong because I swore I
looped through and made all the instances. Obviously, I must not have done
this.

And sorry for not including more code, thought I included all I needed to
without the obvious "using System.IO.Ports;" and such. Sorry about that, I'll
try to post fuller examples in the future with questions or answers. I know
that's sometimes a pet peeve :)


Thank you again for the assistance.

-Jessee

Peter Duniho said:
[...]
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.

In general, if you can't get MSDN sample code to work, then you are
doing it wrong. The MSDN samples are not 100% correct; I've seen bugs
in them myself. But on the whole, the sample code is excellent and in
most cases even where the code could be better, it does work.

So your first thought should always be directed toward how you might be
misunderstanding or misusing the sample code.

Now, as for this particular case: certainly the MSDN code you've
included here sure looks like it ought to do what you want. So either
you haven't used the MSDN code as you've posted it here, or you haven't
described correctly what it is you want, or you have somewhere else
misused the variable declared in the code you've posted.

Since you didn't post any of your own code, it's hard to say exactly
what your mistake is. If you want more specific advice, you should
post a concise example of code that demonstrates the problem you're
having. In this case, I suspect that just the immediate context (that
is, the declaration of the variable and the code that uses the
variable) is sufficient, rather than posting an entire program. Do
make sure that you explain in detail where you are getting an error or
other problem, and what that error or other problem is.

Pete
 

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