Solved

Array.Copy Not Working

Posted on 2010-11-15
19
488 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

911 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

18 Experts available now in Live!

Get 1:1 Help Now