Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array.Copy Not Working

Posted on 2010-11-15
19
Medium Priority
?
541 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

670 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