Reputation: 461
I currently have several Structure
and SortedList
:
Public Structure Membres
Dim NoMembre As String
Dim TypeMembre As String
Dim LangCode As String
Dim MembreNom As String
Dim MembrePrenom As String
Dim MembreAdresse As String
Dim MembreVille As String
Dim ProvCode As String
Dim MembreCodePostal As String
Dim MembreNoTel As String
Dim MembreEMail As String
End Structure
Public Structure Provinces
Dim ProvCode As String
Dim ProvDesc As String
End Structure
Public Structure Langues
Dim LangCode As String
Dim LangDesc As String
End Structure
Public Structure TypesMembres
Dim TypeMembre As String
Dim TypeMembreDesc As String
End Structure
Public FicheMembre As New SortedList(Of String, Membres)
Public FicheProvince As New SortedList(Of String, Provinces)
Public FicheLangue As New SortedList(Of String, Langues)
Public FicheTypeMembre As New SortedList(Of String, TypesMembres)
What I would like to achieve is to pass as a paramater in a function any of these SortedList
as a generic Type
form :
Private Function readTxt(list As SortedList(Of String, Generic Type), path
As String)
Now is it possible to do this and how can I achieve it ?
I have already tried to pass an Object
, a Type
, etc. None worked. Maybe I'm missing something here ?
Here is something I tried but failed:
Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String) As SortedList(Of String, T)
Dim reader As System.IO.StreamReader
reader = IO.File.OpenText(path)
Dim str As String()
Dim i As Integer = 0
Do While reader.Peek <> -1
Dim line As String = reader.ReadLine
Dim key As String = line.Substring(0, line.IndexOf("|"))
If Not (list.ContainsKey(key)) Then
str = line.Split("|")
list.Add(key, getMembre(str))
End If
i += 1
Loop
Return list
End Function
My implementation of the ANSWER:
Private Function GetLists(Str As String(), t1 As Object) As Object
Dim m As Membres
Dim p As Provinces
Dim tm As TypesMembres
Dim l As Langues
Select Case True
Case TypeOf t1 Is Membres
m.NoMembre = Str(0)
m.MembreNom = Str(3)
m.MembrePrenom = Str(4)
m.MembreAdresse = Str(5)
m.MembreVille = Str(6)
m.ProvCode = Str(7)
m.MembreCodePostal = Str(8)
m.MembreNoTel = Str(9)
m.MembreEMail = Str(10)
m.LangCode = Str(2)
m.TypeMembre = Str(1)
Return m
Case TypeOf t1 Is Provinces
p.ProvCode = Str(0)
p.ProvDesc = Str(1)
Return p
Case TypeOf t1 Is TypesMembres
tm.TypeMembre = Str(0)
tm.TypeMembreDesc = Str(1)
Return tm
Case TypeOf t1 Is Langues
l.LangCode = Str(0)
l.LangDesc = Str(1)
Return l
End Select
End Function
Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String, args As Object) As SortedList(Of String, T)
Dim reader As System.IO.StreamReader
reader = IO.File.OpenText(path)
Dim str As String()
Do While reader.Peek <> -1
Dim line As String = reader.ReadLine
Dim key As String = line.Substring(0, line.IndexOf("|"))
If Not (list.ContainsKey(key)) Then
str = line.Split("|")
list.Add(key, GetLists(str, args))
End If
Loop
Return list
End Function
Private Sub GetProvName()
End Sub
This worked for me since I only wanted to know the type of structure passed as a parameter.
Upvotes: 1
Views: 100
Reputation: 19641
To make a method generic, you need to add at least one type parameter. The syntax for type parameters in VB.NET is (Of T1, T2, ...)
.
Therefore, your function should look something like this:
Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String) As String
' Your code here.
End Function
Usage:
ReadTxt(FicheMembre, "SomeString")
ReadTxt(FicheProvince, "SomeString")
'
' etc.
References:
Upvotes: 2