Link to home
Start Free TrialLog in
Avatar of paulwhelan
paulwhelan

asked on

Array has contents in one function but not in another?

Hi
I copied and pasted some code and cant understand why one works and the other doesn't.

Im working off code for a wizard to add data types and need to add one for a string data type.

On the number and string pages I do this

private bool AllowNext(bool bShowDialogs)
        {

            if (_oWizard._oCurrentDataTypeWizardPage._oList.Count > 0)
            {
                int[] iInfoArray = new int[_oWizard._oCurrentDataTypeWizardPage._oList.Count];
                _oWizard._oCurrentDataTypeWizardPage._oList.CopyTo(iInfoArray);

            }
           
            return true;
        }

_oList is where the data is held that is entered on the first page of the wizard.

Yet on the number page its always 6 (6 values are entered on first page of the number wizard)
and on the string page its always 0 (even though there are 7 values entered on the first page of the string wizard)

I just dont understand why the _oList.Count is finding values for the Number data type but not for my new string data type?

Anything thats not clear please ask.
Thanks
Paul

Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Paul,

What type is oList? Can you put the code for the first page up? Or send it to me (address in profile on this)?

Does oList contain numbers and strings?

Cheers

Wint.
Avatar of devsolns
devsolns

yeah need more code, im not understanding the method above.  your creating an array

int[] iInfoArray = new int[_oWizard._oCurrentDataTypeWizardPage._oList.Count];

within the methods scrope, not using it afterwards and not returning in so what purprose does it serve?  your also always returning  true so im not following what your doing here...
Avatar of Mike Tomlinson
Not sure exactly what is going on but I notice you have an "off by one" logic error.

If you have 6 items, then you have a "Count" of 6.

When you declare an array by passing in that Count of 6, you actually get an array of length 7, because you are passing in the upperbound and the array uses 0 based indexing:

    0,1,2,3,4,5,6 = 7 entries
Avatar of paulwhelan

ASKER

Thanks guys this might help.

From another file i see

System.Collections.ArrayList _oList = new System.Collections.ArrayList();

(this used to be
//internal string[] _sInfo = new string[3];)

_oList should be able to contain decimals, datetimes, strings (basically different types)

Whay does it mean to define it as ArrayList?

(this is code Im modifying - I didnt write most of it!)

Thanks
PAul
An ArrayList is a collection class that can hold any type of variable (primitives and objects).

It automatically grows in size to accomodate new entries via the Add() method.  You can remove things from the middle and it will shift all the elements down to fill the gap.

It is like an array in that you can access things by Index but it is more flexible.
That arraylist is able to hold pretty much anything.  How does it do this?  Its an arraylist that holds objects.  Since all objects derive (behind the scenes) from the object class its able to hold any object.  Now for primitive types it uses boxing which boxes primitive types into objects on the heap so its able to hold those as well.  An arraylist may bring on a lot of overhead and they are not type safe at all.  A recommendation would be to put a type safe wrapper around them or use Generics if you have .NET 2.0.
ASKER CERTIFIED SOLUTION
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help guys I will look into it and get back to you tomorrow
Cheers
Paul