Solved

Array.Copy Not Working

Posted on 2010-11-15
19
481 Views
Last Modified: 2013-11-26
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
Comment
Question by:Joan_Held
  • 10
  • 8
19 Comments
 
LVL 16

Expert Comment

by:13598
ID: 34137568
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
 

Author Comment

by:Joan_Held
ID: 34137622
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
 
LVL 16

Expert Comment

by:13598
ID: 34137651
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
 

Author Comment

by:Joan_Held
ID: 34137690
Var = 8 and Length = 1520
0
 
LVL 16

Expert Comment

by:13598
ID: 34137739
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
 
LVL 16

Expert Comment

by:13598
ID: 34137769
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
 

Author Comment

by:Joan_Held
ID: 34137789
Ok, so, now the destination array is not long enough.
0
 
LVL 16

Expert Comment

by:13598
ID: 34137806
What is your current code?
0
 

Author Comment

by:Joan_Held
ID: 34137817
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:13598
ID: 34137842
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
 

Author Comment

by:Joan_Held
ID: 34137869
It's gone back to saying the source array is not long enough.
0
 
LVL 16

Expert Comment

by:13598
ID: 34137886
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
 

Author Comment

by:Joan_Held
ID: 34137924
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
 
LVL 16

Expert Comment

by:13598
ID: 34137934
Did you change Var like this?
  Var = CommaCount - UnsortedArray.Length  
0
 

Author Comment

by:Joan_Held
ID: 34137938
Yep
0
 
LVL 16

Accepted Solution

by:
13598 earned 250 total points
ID: 34137961
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
 

Author Comment

by:Joan_Held
ID: 34137985
That was it. Thank You!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34137998
Show us a sample input file along with what you expect to end up in "UnsortedNumArray".
0
 
LVL 16

Expert Comment

by:13598
ID: 34138043
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now