creating multiple variables with a loop

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I am trying to do the following with a loop because the amount of
channels can change.
uint Chan_1 = 0;
uint Chan_2 = 0;
uint Chan_3 = 0;
uint Chan_4 = 0;
uint Chan_5 = 0;
uint Chan_6 = 0;
uint Chan_7 = 0;
uint Chan_8 = 0;
uint Chan_9 = 0;
uint Chan_10 = 0;
uint Chan_11 = 0;
uint Chan_12 = 0;
uint Chan_13 = 0;
uint Chan_14 = 0;
uint Chan_15 = 0;
uint Chan_16 = 0;
Help?
Thanks
Mike
 
Hello,
I am trying to do the following with a loop because the amount of
channels can change.
uint Chan_1 = 0;
            uint Chan_2 = 0;
            uint Chan_3 = 0;
            uint Chan_4 = 0;
            uint Chan_5 = 0;
            uint Chan_6 = 0;
            uint Chan_7 = 0;
            uint Chan_8 = 0;
            uint Chan_9 = 0;
            uint Chan_10 = 0;
            uint Chan_11 = 0;
            uint Chan_12 = 0;
            uint Chan_13 = 0;
            uint Chan_14 = 0;
            uint Chan_15 = 0;
            uint Chan_16 = 0;
Help?
Thanks
Mike

It's not clear what you want to do, you cuold simply have one variable
MaxNumberOfChannels = 16. If you need to keep X differents values then
you use a List

Also, I recommend you to get a book of programming. One of the
programming for Dummies will be a good start
 
AMP said:
I am trying to do the following with a loop because the amount of
channels can change.
uint Chan_1 = 0;
uint Chan_2 = 0;
[...]
uint Chan_16 = 0;

using System.Collections.Generic;

.  .  .

List<uint> chans = new List<uint>();

for (uint i = 0; i < 16; i++) // or however many
{
   chans.Add(0);

}

Then access the list members with chans[0], chans[1], etc.

Eq.

Thanks
Thats working good so far but I ran into this:
for (int r = 0; r < 16; r++)
{
UInt32.TryParse((multiArrayofData[x, 1, 0] +
multiArrayofData[x, 1, 1]),
System.Globalization.NumberStyles.HexNumber, null, out chans[r]);


}

with this error:
A property or indexer may not be passed as an out or ref parameter.
How do I get arount this?
 
AMP said:
I am trying to do the following with a loop because the amount of
channels can change.
uint Chan_1 = 0;
uint Chan_2 = 0;
[...]
uint Chan_16 = 0;
using System.Collections.Generic;
.  .  .
List<uint> chans = new List<uint>();
for (uint i = 0; i < 16; i++) // or however many
{
   chans.Add(0);

Then access the list members with chans[0], chans[1], etc.

Thanks
Thats working good so far but I ran into this:
for (int r = 0; r < 16; r++)
                {
                          UInt32.TryParse((multiArrayofData[x, 1, 0] +
multiArrayofData[x, 1, 1]),
System.Globalization.NumberStyles.HexNumber, null, out chans[r]);

                }

with this error:
A property or indexer may not be passed as an out or ref parameter.
How do I get arount this?- Hide quoted text -

- Show quoted text -

Paul, Thanks
Ignacio, Depending on the language you could create variables by using
Var+i .
PHP Example: $_POST['ref'.$x.'_email']
I was originnaly using a Array but Paul came up with a better Idea.
I write in 3 different languages every day and there are differences
that I dont always know about.
This is why I use the message boards, Not to tell me to get a book.
Mike
 
AMP said:
UInt32.TryParse((multiArrayofData[x, 1, 0] + multiArrayofData[x, 1,
1]), System.Globalization.NumberStyles.HexNumber, null, out chans[r]);
with this error: A property or indexer may not be passed as an out or
ref parameter.

Well, chans[r] is an indexer (check the documentation if you aren't
familiar with that idea), and as it says you aren't allowed to do that.
I suppose it's because indexers are allowed to be read-only.

So just use a temporary variable instead.

uint temp = chans[r];
UInt32.TryParse( .... out temp);
chans[r] = temp;

It does sound like you could benefit from a book or tutorial, because
these aren't very advanced C# topics.

Eq.

Actually,
This works fine:
chans[r]= UInt32.Parse((multiArrayofData[x, b, 0] +
multiArrayofData[x, b, 1]),
System.Globalization.NumberStyles.HexNumber);
Mike
 

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

Back
Top