how to declare variable

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;

Daniel
 
Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without this
knowledge.
 
David Wilkinson said:
Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without
this knowledge.


I think u is an 'unsigned int' or an 'int', and u has values out of range
[0,999].

Giovanni
 
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.


David Wilkinson said:
Daniel said:
I'm getting an error on this code that says index out of range. It's an
unhandled exception error. What is the problem?

List<unsigned int> ls(1000);

ls = u ;


Daniel:

What is u? How is it initialized? The question cannot be answered without
this knowledge.
 
I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

So, please show code for your List class, in particular operator[].

Giovanni
 
Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial capacity, not
its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 
There is no other code. Just . . .

List<unsigned int> ls(1000);

and . . .

ls = 1;

in a loop.

This code doesn't do anything right now. I'm merely using test code to be
sure I understand how to write the code.

Daniel

Giovanni Dicanio said:
I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

So, please show code for your List class, in particular operator[].

Giovanni
 
Right after . . .

list->Add( i );

.. . . should I write code like . . .

list = <any int value>;

Is that right?

Daniel

Giovanni Dicanio said:
Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial capacity,
not its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 
Daniel said:
Right after . . .

list->Add( i );

. . . should I write code like . . .

list = <any int value>;

Is that right?


Add() allows to insert new data into the List<T> (actually, a vector...).
If you have N items in the List, you can access them using operator[], using
values from 0 (the first item) to N-1 (the last item).

Giovanni
 
Daniel said:
Right after . . .

list->Add( i );

. . . should I write code like . . .

list = <any int value>;

Is that right?


No, but after

list->Add(i);

you should expect that

list[list->Length - 1] == i;

Note that the parameter to Add is the value stored in the list/array, not
the index. In Giovanni's example they are the same though.
Daniel

Giovanni Dicanio said:
Daniel said:
Sorry.

unsigned int u = 0;

u is incremented in a loop.

I tried . . .

ls[0] = 1;

and . . .

ls[2] = 1;

they both give the same error.

OK, I understand that you are using System.Collection.Generic.List<T>
generic container from .NET, from a C++/CLI code I think.

The constructor to which you pass as integer sets list initial
capacity, not its size.

So the list is still empty from operator[] point of view.

I think you should fill the list with code like this:

<code>

List<int> ^ list = gcnew List<int>();

int count = 1000;
for ( int i = 0; i < count; i++ )
{
list->Add( i );
}

</code>

Giovanni
 
Back
Top