Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

Array.Copy Not Working

Hello,

So, I'm trying to copy my old array to a new array with array.copy, but every time I try to use it the debugger stops working. This is my code:
Dim Var As Integer
        Dim Length As Integer = UnsortedArray.Length
        Var = CommaCount - UnsortedArray.Length + 1
        MessageBox.Show(Var)
        Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), 99)
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length)

Open in new window


What would array.copy not be working with this?
0
Joan_Held
Asked:
Joan_Held
  • 10
  • 8
1 Solution
 
13598Commented:
Why don't you use a try/catch statement to see if it gives you an error description that is helpful to point you/us in the right direction?
try
dim Var As Integer  
        Dim Length As Integer = UnsortedArray.Length  
        Var = CommaCount - UnsortedArray.Length + 1  
        MessageBox.Show(Var)  
        Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), 99)  
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length)
catch ex as exception
messagebox.show(ex.tostring)
end try
0
 
Joan_HeldAuthor Commented:
Ok, so, it's saying that the source array was not long enough. "System.ArgumentException: Source array was not long enough. Check srcIndex and length, and the array's lower bounds."

Now I'm even more confused.
0
 
13598Commented:
The only thing that would jump at me is that var and length values could be the culprits.
What is your var and length values? You check what var is, check what length is and post please:
try
dim Var As Integer  
        Dim Length As Integer = UnsortedArray.Length  
        Var = CommaCount - UnsortedArray.Length + 1  
        MessageBox.Show(Var & "_" & Length)  
        Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), 99)  
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length)
catch ex as exception
messagebox.show(ex.tostring)
end try
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Joan_HeldAuthor Commented:
Var = 8 and Length = 1520
0
 
13598Commented:
your UnsortedNumArray is set as a length of 99 but you are trying to copy more than 99.
If you only want 99 elements then do this:
 Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), 99)  
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, 99)

If you want all  then do this:
 Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), Length)  
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length)

0
 
13598Commented:
Or since you are starting at var you could do this for all:
 Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), Length - Var)  
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length - Var)

0
 
Joan_HeldAuthor Commented:
Ok, so, now the destination array is not long enough.
0
 
13598Commented:
What is your current code?
0
 
Joan_HeldAuthor Commented:
Dim path As String = ofdOpenit.FileName
        Dim UnsortedArray() As String = File.ReadAllLines(path)
        Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length
        Try
            Dim Var As Integer
            Dim Length As Integer = UnsortedArray.Length
            Var = CommaCount - UnsortedArray.Length + 1
            MessageBox.Show(Var)
            MessageBox.Show(Length)
            Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), Length - Var)
            UnsortedNumArray(0) = "Title"
            Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length - Var)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

Open in new window

0
 
13598Commented:
I am never sure if some things ar 0 based or not due to the lack of consistency.
My guess is that is the problem. Try using this:
Dim path As String = ofdOpenit.FileName  
        Dim UnsortedArray() As String = File.ReadAllLines(path)  
        Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length  
        Try  
            Dim Var As Integer  
            Dim Length As Integer = UnsortedArray.Length  
            Var = CommaCount - UnsortedArray.Length + 1  
            MessageBox.Show(Var)  
            MessageBox.Show(Length)  
            Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), (Length - Var) + 1)  
            UnsortedNumArray(0) = "Title"  
            Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, (Length - Var) + 1)  
        Catch ex As Exception  
            MessageBox.Show(ex.ToString)  
        End Try
0
 
Joan_HeldAuthor Commented:
It's gone back to saying the source array is not long enough.
0
 
13598Commented:
Try this:

Dim path As String = ofdOpenit.FileName  
        Dim UnsortedArray() As String = File.ReadAllLines(path)  
        Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length  
        Try  
            Dim Var As Integer  
            Dim Length As Integer = UnsortedArray.Length  
            Var = CommaCount - UnsortedArray.Length  
            MessageBox.Show(Var)  
            MessageBox.Show(Length)  
            Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), Length - Var)  
            UnsortedNumArray(0) = "Title"  
            Array.Copy(UnsortedArray, Var, UnsortedNumArray, 1, Length - Var)  
        Catch ex As Exception  
            MessageBox.Show(ex.ToString)  
        End Try
0
 
Joan_HeldAuthor Commented:
Ok, so, if the destination array is + 1 the source array is not long enough, but if the destination array is not + 1 then it is not long enough.
0
 
13598Commented:
Did you change Var like this?
  Var = CommaCount - UnsortedArray.Length  
0
 
Joan_HeldAuthor Commented:
Yep
0
 
13598Commented:
Oh I get it I think.
Arrays are 0 based but you are starting at 1 instead of 0?
Try this:
Dim path As String = ofdOpenit.FileName  
        Dim UnsortedArray() As String = File.ReadAllLines(path)  
        Dim CommaCount As Long = Join(UnsortedArray, ",").Split(",").Length  
        Try  
            Dim Var As Integer  
            Dim Length As Integer = UnsortedArray.Length  
            Var = CommaCount - UnsortedArray.Length  
            MessageBox.Show(Var)  
            MessageBox.Show(Length)  
            Dim UnsortedNumArray As Array = Array.CreateInstance(GetType(String), Length - Var)  
            UnsortedNumArray(0) = "Title"  
            Array.Copy(UnsortedArray, Var, UnsortedNumArray, 0, Length - Var)  
        Catch ex As Exception  
            MessageBox.Show(ex.ToString)  
        End Try
0
 
Joan_HeldAuthor Commented:
That was it. Thank You!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Show us a sample input file along with what you expect to end up in "UnsortedNumArray".
0
 
13598Commented:
Only thing since we moved things around is to make sure your first element is the one you wanted. You were starting at var +1 if that is what you need change your code back to:
            Var = CommaCount - UnsortedArray.Length + 1  
         
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now