Solved

Array.Copy Not Working

Posted on 2010-11-15
19
525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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
 
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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

734 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