Debugging a DLL

I have a DLL which is referencing another DLL. Inside my DLL there is a function:

        public string[,] querycols1010(int queryID)
        {
            string[][] tempval;

            tempval =  comLink.querycols1010(queryID);

            string[,] colInfo = new string[6, tempval.GetUpperBound(0)];

            for (int k = 0; k <= 5; k++)
            {
                for (int i = 0; i <= tempval.GetUpperBound(0); i++)
                {
                    colInfo[k,i] = tempval[k][i];
                }
            }
            return colInfo;
        }

Open in new window


When I try to use this function from VBA I get an error "Index was outside the bounds of the array". I would like to debug my C# dll code to see what is going on as I have a feeling that there is something wrong with my array conversion, but not sure how to setup the debugging. Help would be appreciated.

Leon
LVL 29
leonstrykerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
tempval =  comLink.querycols1010(queryID);

You assume that after this line the tempval is a valid array.  I'd recommend checking and exiting the function if it isn't.  (You could also display a warning should you wish.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leonstrykerAuthor Commented:
This function is in a DLL, I am not sure how to check it.
0
AndyAinscowFreelance programmer / ConsultantCommented:
tempval =  comLink.querycols1010(queryID);
if(tempval.GetUpperBound(0) < 0)
{
  //Something is wrong ?  Put your code here to cope with that case
}
0
käµfm³d 👽Commented:
The problem is that you have not declared the array with enough space. This line:

string[,] colInfo = new string[6, tempval.GetUpperBound(0)];

Open in new window


...is the offender. Assume that GetUpperBound(0) returned 5. This means that you would be declaring an array with its second dimension capable of holding 5 elements. That means the highest index you can use to access elements within that dimension is 4 (length - 1).

Now, this line:

for (int i = 0; i <= tempval.GetUpperBound(0); i++)

Open in new window


...is--in my example--saying:

for (int i = 0; i <= 5; i++)

Open in new window


You cannot use <= in that case because you would be saying that 5 is the highest index that can be used to access that dimension of the array. But we said above that 4 is the highest index.

You either need to exchange the <= for just a < or you need to change GetUpperBound(0) to GetUpperBound(0) - 1.
0
leonstrykerAuthor Commented:
I figured it out. I reversed the elements of the arrays. The correct version of the code is

            string[,] colInfo = new string[tempval.GetUpperBound(0), 6];

            for (int k = 0; k <= tempval.GetUpperBound(0) - 1; k++)
            {
                for (int i = 0; i <= 5; i++)

Open in new window


Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.