Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy some part of an array

Status
Not open for further replies.

sinojen

Programmer
Mar 20, 2008
8
0
0
NO
I have a problem; I have an array, I want to copy the first x number of variables from the array to a new array (for example variable 0 to 4). Duplicated variables should not be included.
Let's say the array is: 1 8 2 8 7 33 8 27. And I want to copy 5 variables, then the new array should include 1 8 2 7 3
 
Quick example should help you out. I threw two textboxes and a command button on a form. The code behind the button was:

Code:
        Dim aStart() As String = Split(Me.TextBox1.Text, ",")
        Dim AResult(4) As String

        For i As Int32 = 0 To 4
            Dim ArrayPlace As Int32 = 0
            For j As Int32 = 0 To aStart.Count - 1
                If Not AResult.Contains(aStart(j)) Then
                    AResult(i) = aStart(j)
                    Exit For
                End If
            Next
        Next

        Me.TextBox2.Text = Join(AResult, ",")

Should help you get started.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
thank you, but I it don't work. I use ASP.NET here, and I am a beginner.
the number x is stored in a variable.
 
ASP.Net can be driven by either VB or C#. So do you mean you are using C#??

Also, what I gave you was just a sample....not to be your exact code.

You can easily modify this to include some variables, such as

Code:
Dim AResult(intPlacesToGrab) As String

For i As Int32 = 0 To intPlacesToGrab

And you only need to passthe code the necessary intPlacesToGrab variable. That will make it grab the number of values you want.

Please give us a bit more on your situation, what doesn't work, and what - if any - errors you are getting to better help you.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Microsoft .NET Framework version:2.0.50727.312
ASP.NET version:2.0.50727.833

I do write in Visual BASIC .net

I got an error:
Error BC30456

"Contains" is not a member of "System.Array
 
I am not an ASP.NET person at all (my company is sending me to a class next month on it). The above code was quickly thrown into a Windows Application and worked as expected in several tests.

You might try replacing:

If Not AResult.Contains(aStart(j)) Then

with

If AResult.IndexOf(aStart(j)) = -1 Then

That basically performs the same check and may be available in the Web namespaces. If not, I would take this sample I have provided and move over to the ASP.NET forum ( letting them know this came from a Windows app, and ask them to help you modify it for ASP.

Good luck.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
BC30452: Operator '-' is not defined

I think I ask in the ASP.NET forum. I did not know there was one here, until now. Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top