John Oliver (UK)MSP, VSIP

Hi,

In my last post in this thread.>>

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1762338&SiteID=1&mode=1

i have created 2 functions.

To1dArray 'converts a 2D array to a 1D array

To2dArray ' does the reverse, 1dArray to a 2dArray of TYPE Integer only.

How can i make it work with any TYPE please

Regards,

S_DS




Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Adamus Turner

The easy way

Replicate the code blocks for each datatype. Use polymorphism and name all the funtions same and make the distinction from the parameter datatype.

Adamus






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Spidermans_DarkSide - MSP, VSIP

Hi,

In the past i have passed an Object such as a Panel of any name or a PictureBox of any name for a graphics based Function or Subroutine but they all had the same TYPE ( System.Windows.Forms.Control ), can you give an example please

I think i'm thrown by the fact an array is a collection containing objects of another TYPE.

Regards,

S_DS






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

David Anton

(untested)

Public Function To2dArray(Of T)(ByVal 1dArray As T()) As T(,)

...

End Function






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Adamus Turner

In this case, you're passing an array of controls not an array of different datatypes.

Coincidently, you can simply pass the form name and loop through the controls inside the function. An array is not required.

I will post some code later in the day to demonstrate.

Adamus






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Adamus Turner

Private Sub myArray1d(ByVal myarray() As Integer)

'Orginal code

End Sub

Private Sub myArray1d(ByVal myarray() As String)

'Replicated Code with a string array

End Sub

Private Sub myArray1d(ByVal myarray() As Boolean)

'Replicated Code with a Boolean array

End Sub

Private Sub myArray2d(ByVal myarray() As Integer)

'Original Code

End Sub

Private Sub myArray2d(ByVal myarray() As String)

'Replicated Code with a string array

End Sub

Private Sub myArray2d(ByVal myarray() As Boolean)

'Replicated Code with a Boolean array

End Sub

This approach will work for your original post, but are you using any array of controls or an array of variables

Adamus






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

David Anton

Wow - that's a lot of signatures when you really just need:

Code Snippet

Public Function To2dArray(Of T)(ByVal OnedArray As T()) As T(,)






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Adamus Turner

David Anton wrote:

Wow - that's a lot of signatures when you really just need:

Code Snippet

Public Function To2dArray(Of T)(ByVal OnedArray As T()) As T(,)

Yes this will work beautifully as long as no datatype specific work is required within the function.

Adamus






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

David Anton

Oops - you're right - I was getting a little too carried away with generics...






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

rkimble

Hi everyone.

I believe the easy way, and the correct way using generics, is to simply replace the explicit Integer types with (Of t):

Code Snippet

Public Shared Function To1dArray(Of t)(ByVal a2dArray As Array) As Array

'CREATE A LIST

Dim MyList As New List(Of t)

'LOOP ORIGINAL ARRAY, FILLING THE LIST

For i As Integer = 0 To a2dArray.GetUpperBound(0)

For j As Integer = 0 To a2dArray.GetUpperBound(1)

MyList.Add(a2dArray(i, j))

Next

Next

'IF NEEDED CONVERT THE LIST TO A STANDARD ARRAY

Dim a1dArray() As t = MyList.ToArray

Return a1dArray

End Function

Public Shared Function To2dArray(Of t)(ByVal a1dArray As Array, ByVal requiredDimension1 As Integer, ByVal requiredDimension2 As Integer) As Array

'Get the original array size.

Dim arraySize As Integer = a1dArray.GetUpperBound(0) - a1dArray.GetLowerBound(0) + 1

'Set up an array with the required 0 based dimensions.

Dim a2DArray(requiredDimension1 - 1, requiredDimension2 - 1) As t

'Check if the array can be converted!!

If requiredDimension1 * requiredDimension2 <> arraySize Then

Throw New Exception("An array of " & arraySize & " elements cannot be converted to those dimensions, sorry!!")

Exit Function

End If

'Index used to reference the 1D array.

Dim index As Integer = -1

'Convert to a zero based 2D array.

For X As Integer = 0 To requiredDimension1 - 1

For Y As Integer = 0 To requiredDimension2 - 1

index += 1

a2DArray(X, Y) = a1dArray(index)

Next

Next

'Pass the new array back!!

Return a2DArray

End Function

Here's a example of using those two functions with 3 different array types: Integer, String, and Control:

Code Snippet

'Use the functions with Integers

Dim INTar(3) As Integer

INTar(0) = 1

INTar(1) = 3

INTar(2) = 54

INTar(3) = 5

Dim INTar2(,) As Integer

INTar2 = To2dArray(Of Integer)(INTar, 2, 2)

INTar = To1dArray(Of Integer)(INTar2)

'Use the functions with Strings

Dim STRar(3) As String

STRar(0) = "One"

STRar(1) = "Three"

STRar(2) = "Fifty Four"

STRar(3) = "Five"

Dim STRar2(,) As String

STRar2 = To2dArray(Of String)(STRar, 2, 2)

STRar = To1dArray(Of String)(STRar2)

'Use the functions with Controls

Dim CTLar(3) As Control

CTLar(0) = New TextBox

CTLar(1) = New Label

CTLar(2) = New Panel

CTLar(3) = New Button

Dim CTLar2(,) As Control

CTLar2 = To2dArray(Of Control)(CTLar, 2, 2)

CTLar = To1dArray(Of Control)(CTLar2)

SDS, is that what you were asking for






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Spidermans_DarkSide - MSP, VSIP

Hi Reed,

Many, many thanks again.

That was exactly what i was looking for.

Rather than put the functions into a CLASS as you had them, and create a DLL i whacked them into a Module ( removing the Shared keyword ) so as to test your example in a FORM_LOAD event Sub.

You have now given me another idea, a quick way to set up a multi-2D array of any FORM control like a Button or a TextBox so as to set up a grid of any size, with each control having the same height and width.

So to set up a grid of multiple buttons of say size 20,20 for a game i'm going to attempt.>>

Code Snippet

Private Sub multiControl _

(ByVal controlSize As Size, _

ByVal someControl As Control, _

ByVal xItemsAcross As Integer, _

ByVal yItemsDown As Integer, _

ByVal gridLocation As Point)

End Sub

So a button grid could be any size like this 3 x 3 one for noughts and crosses.>>

X O X

O X O

X O X

The game i have in mind may use various size button-grids for different levels.

Regards

S_DS






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Riquel Dong ¨C MSFT

Hi S_DS,

Based on your post, I change your Class arrayFunctions . I have modified the code in the following code snippt.

Now I test Class arrayFunctions. I can convert many types array , like the integer, string, and Button. If you want to convert object array to original type. You can ues the Array.Copy() method. It copies a range of elements in one source array to destination array and performs type casting and boxing as required.

Code Snippet

Dim MyList As New List(Of Object)

.........

Dim a1dArray() As Object = MyList.ToArray

.........

Dim a2DArray(requiredDimension1 - 1, requiredDimension2 - 1) As Object

.........

Array.Copy(sourcearray, detinationarray, lenght)

Hope this can help you !

Riquel






Re: Visual Basic Language Help needed to create a 'generic' conversion function please. To2dArray

Spidermans_DarkSide - MSP, VSIP

Riquel Dong ¨C MSFT wrote:

Hi S_DS,

Based on your post, I change your Class arrayFunctions . I have modified the code in the following code snippt.

Now I test Class arrayFunctions. I can convert many types array , like the integer, string, and Button. If you want to convert object array to original type. You can ues the Array.Copy() method. It copies a range of elements in one source array to destination array and performs type casting and boxing as required.

Code Snippet

Dim MyList As New List(Of Object)

.........

Dim a1dArray() As Object = MyList.ToArray

.........

Dim a2DArray(requiredDimension1 - 1, requiredDimension2 - 1) As Object

.........

Array.Copy(sourcearray, detinationarray, lenght)

Hope this can help you !

Riquel

Hi,

It is often something i overlook ( typing a dot ) to see what other built-in methods are available.

I will look into the Array.Copy method, thankyou.

Regards,

S_DS