Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 9th, 2008, 07:32 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Go from 2d Array to 1d array without defining type

Hi,

I want to do something very easy, but I can't find a build in function
and i'm stuck with writing my own.

I'm trying to get a 1D array of column x out of a 2D array...
and that works for any type in the array.
example
Dim arr(0, 1) As Array ' Store Id + some sentence
Dim retArr(1) as array ' Get Ids only

arr(0,0) = 1
arr(0,1) = "rw1"

arr(1,0) = 2
arr(1,1) = "rw2"

retArr=getColumnFromArray(arr,1)

Code:
    Private Function getColumnFromArray(ByVal arr(,) As object, ByVal column As Byte) As Array
        Dim arrReturn() As VariantType
        Dim myType As Type = arr(0, 0).GetType()
        Dim i As Integer
        Dim len As Integer = UBound(arr, 0)

        ReDim arrReturn(len)

        For i = 0 To len
            arrReturn(i) = CType(arr(i, 0), myType)
        Next

        Return arrReturn
    End Function


and I get an error on the line
arrReturn(i) = CType(arr(i, 0), myType)

myType is not defined.

and why do I need an array of the Ids because:

The function that i'm writing loops through a filtered dataview
and get's all the ids. BUT I also want to store how many duplicate ids have been found!

So I ended up using a 2D array BUT I used the function 1Darray.BinarySearch to search for duplicate ids and that function only works for 1D arrays...

The problems came after I was reformatting my code so I could have the IdCount returned as well.
  #2 (permalink)  
Old January 10th, 2008, 11:13 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Which framework are you using? If you're using 2.0+ then this is a solution perfectly suited for Generics. You can build the method to require a type parameter such that the input and output is type specific. I haven't worked with generics in VB so I can't provide an immediate example.

-Peter


Similar Threads
Thread Thread Starter Forum Replies Last Post
[VB 2005] Parser help involving a 2d array champ0342 VB.NET 0 November 13th, 2007 08:03 PM
Working with array-type elements tclotworthy XSLT 2 October 11th, 2007 09:27 AM
Cannot invoke getName() on the array type File[] fuadlutfi85 JSP Basics 1 July 13th, 2007 03:43 AM
Passing a Array in of no specifc type.. John Pennington Pro VB 6 1 September 27th, 2006 05:04 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.