Link to home
Start Free TrialLog in
Avatar of Racim BOUDJAKDJI
Racim BOUDJAKDJIFlag for Algeria

asked on

Passing arrays to Generic classes

Hi,

I am trying a new generic class.  I would like to define a method that allows to capture as an input parameter either:

> A monodimensional string array
> A monodimensional integer array
> An bidimensional integer jagged array

As I am not familiar with generics I would like to know what would be the best way to go about that? Should I create one generic class?  2 generic classes (one for bidimensional, one for monodimensional?)?
What are the issues at stake?  Providing some code would help to...

Thanks in advance...
Avatar of ptakja
ptakja
Flag of United States of America image

Why not use Overloading in your class:

Public Overloads Sub AcceptArray(ByVal Args as String())

End Sub

Public Overloads Sub AcceptArray(ByVal Args as Integer())

End Sub

Public Overloads Sub AcceptArray(ByVal Args as Integer(,))

End Sub

Avatar of Racim BOUDJAKDJI

ASKER

<<Why not use Overloading in your class>>
I am not really confortable with OO programming concepts (I am more of a database person).    What does overload exactly do?  What is the interest into using it ?  Should I create 1 class? 2 classes? (One for each type of array).  Does overload allow to declare all the possible types that can be parsed to the generic class?  Are all your statements included in the same class?  I apologize in advance if these question may sound obvious to answer...
Could you give me a basic example of a generic class that takes a monodimensional array in that is either string() or integer()...Thanks...
ASKER CERTIFIED SOLUTION
Avatar of ptakja
ptakja
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THanks for your response...
OK this is what I come to ...

1) Created a the following generic class

Imports System.Collections.Generic
Public Class MR1(Of ArrayType)
    Public Function AddValue(ByVal Array1() As ArrayType, ByVal Value As ArrayType) As Integer
        ReDim Array1(0)
        Array1(0) = Value
        AddValue = Array.BinarySearch(Array1, Value)
    End Function
End Class

2) Try to call the following in the main to test class

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As New MR1(Of String),  position() as string, word() as integer
        Console.WriteLine("Index de valeur insérée: {0}", s.AddValue(word, "DDD").ToString)
        Console.WriteLine("Index de valeur insérée: {0}", s.AddValue(position, 2).ToString)
End Sub

As you can see, I try to parse 2 arrays of different types...It works for the first call *s.AddValue(word, "DDD").ToString* but the second try gives me a compile error...


What specifically is the error you get?
I got it now.  I misundersood that I needed to make a second instantiation of the class as being integer so that I could parse an integer...

Instead of ...

Button2.Click
        Dim s As New MR1(Of String),  position() as string, word() as integer
        Console.WriteLine("Index of inserted value: {0}", s.AddValue(word, "DDD").ToString)
        Console.WriteLine("Index of inserted value: {0}", s.AddValue(position, 2).ToString)
End Sub

I put
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As New MR1(Of String), t As New MR1(Of Integer),  position() as string, word() as integer --> was the change
        Console.WriteLine("Index of inserted value: {0}", s.AddValue(word, "DDD").ToString)
        Console.WriteLine("Index of inserted value: {0}", t.AddValue(position, 2).ToString)
End Sub

My problem now is that when I try to insert several value the function seems to have forgotten that the array had already a previous value inserted.  When I run this

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As New MR1(Of String), t As New MR1(Of Integer)
        Console.WriteLine("Index of inserted value: {0}", s.AddValue(word, "A").ToString)
        Console.WriteLine("Index of inserted value: {0}", s.AddValue(word, "B").ToString)
        Console.WriteLine("Index of inserted value: {0}", t.AddValue(position, 2).ToString)
End Sub

..and get the following output...
Index of inserted value: 0
Index of inserted value: 0
Index of inserted value: 0

but I should get the following output.  After all the position or word arrays are in the main class

Index of inserted value: 0 --> first insert in word string array
Index of inserted value: 1--> second insert in word string array
Index of inserted value: 0 --> first insert in position integer array

any ideas?
That's because each time you call the method, you redim your array to only have one element.

Try this:

Public Class MR1(Of ArrayType)
    Public Function AddValue(ByVal Array1() As ArrayType, ByVal Value As ArrayType) As Integer
             
        Redim Array1(Array1.Length+1)
        Array1(Array1.Length) = Value
        AddValue = Array.BinarySearch(Array1, Value)
    End Function
End Class
just to make I have redeclared both position and word as follows..

Public Shared position() As Integer
Public Shared word() As String

Basically, I just want the generic class to insert a record, say where it inserted it by indicating the index at which the value was added but by keeping the sort order...
<<That's because each time you call the method, you redim your array to only have one element.>>

But I don't understand...At the second call of string, it should not even run that branch because some data should be there ????if I put your code, I get an error because I try to redimension an array that has no dimension yet...hope that makes sense...in pseudo code

if class is not defined
           redim for one record at least (index = 0)
           put value in that index
           return 0
else
         see if value already in the array
          if there
                   return its current position
          if not there
                   insert the value
                   sort the array
                   return the position of the value in the sorted array

I am sorry about this.  Totally forgot this question.  Even though the question was not quite answered, it has provided me with some interesting insigfht on VB NET