Ok, so, I'm trying to copy an array starting at a specific based on the number of variables I have. I have the program give me the number of variables. What I need is to copy the original array, UnsortedArray, start at the index of var + 1. I have tried the Array.Copy and Array.CopyTo, but I get the error that my new array, UnsortedNumArray, is used before it has a value.

I know I've mostly overlooked something. Could you please point me in the right direction? How am I going about this wrong?

The code I have to count the number variables is this:

Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length
        Dim Var As Integer
        Var = CommaCount - UnsortedArray.Length + 1
        MessageBox.Show(Var)

Recommended Answers

All 5 Replies

The array you are copying into has to be allocated before you do the copy: Dim myTargetArray As Array = Array.CreateInstance(GetType(String), 15) for example.

Ok, so, it doesn't give me the error that my new array doesn't have a value, but when I go to copy my old array to my new one, Array.Copy(UnsortedArray, UnsortedArray.GetLowerBound(Var), UnsortedNumArray, UnsortedNumArray.GetLowerBound(0), 0) , the debugger stops working. I know it has to do with trying to get it to start copying at the index that is the same number as the var. I have redo the same code except that were "Var" I put 0 and it worked.

Do I need to do an If IsNumeric before I start the copying? If not how would I go about tell the program where to start copying from the var number?

The usage of Var in your array copy, is trying to obtain the lowerbound value of the dimension Var (in your example always greater than 0), while there is only one dimension (0) on the array.

If you want to start copying from a certain point, use it as starting index, also copying 0 elements does nothing. you can try some thing like:

Array.Copy(UnsortedArray, Var, UnsortedNumArray, 0, ElementsToCopy)

Hope this helps.

That did help, but for whatever reason that I can't figure out, the debugger just crashes when it sees the Array.Copy line. I've tried it by taking all the variables out and putting numbers in, and that crashed. I've taken the numbers out and put the variables in and that still crashed. Is there any other way to copy arrays or am I just doing it wrong? This is the code for the copy that I have right now: Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length) .

And yes, lolafuertes that did help.

Lets assume that the UnsortedArray contains the following: {"1,2","3,4"} This will means 2 elements, so the UnsortedArray.Length is 2.

Then the Join(UnsortedArray, ",") Returns "1,2,3,4," and the .Split(",").Length (CommaCount) is of 4.

When you calculate the starting position to copy by means of Var = CommaCount - UnsortedArray.Length + 1 that in this case will be 4 - 2 + 1 = 3

Unfortunatelly in this case, you are trying to start copying past the end of the UnsortedArray resulting in an exception.

If the original value of the UnsortedArray was {"1","2") Joining and splitting will result in a CommaCount of 2. Then Var will have a value of 1. Until here there is no problem.

In this case the problem arises when the elements to copy is greater than one, because you run out of the source once more and got the exception.

Said that, take care to have a destination UnsortedNumArray large enough to hold the number of elements to copy over it. In your example, you start to copy over the second element (1) with Length so the destination must be of Length + 2 elements wide at least, or you will get another exception.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.