[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Change this code to use Generics

Been reading about generics. I want to change my ArrayList code to use Generics but not sure how I can define it . I have this:
 Public Shared Function GetSystems(ByVal userLogin As String) As ArrayList
            Dim retVal As New ArrayList
            .....
            retVal.Add(...)
            ...
   End Function

I'm returning an ArrayList . How can I change this to use Generics??
0
Camillia
Asked:
Camillia
  • 2
3 Solutions
 
JimBrandleyCommented:
If you want to create a list of strings,
Public Shared Function GetSystems(ByVal userLogin As String) As List<string>
            Dim retVal As New List<string>( 10 )
            .....
            retVal.Add(...)
            ...
   End Function

The namespace reference is System.Collections.Generic

Jim
0
 
Bob LearnedCommented:
VB, Jim, VB:

Public Shared Function GetSystems(ByVal userLogin As String) As List(Of String)
            Dim retVal As New List(Of String)(10)
            .....
            retVal.Add(...)
            ...
   End Function

Bob
0
 
JimBrandleyCommented:
Thanks Bob (sheepish grin)

Jim
0
 
Fernando SotoCommented:
Hi Farzadw;

This is sample code using a generic List of Strings.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim retList As List(Of String)

        retList = GetSystems("My System")
        For Each str As String In retList
            Console.WriteLine(str)
        Next

    End Sub

    Public Shared Function GetSystems(ByVal userLogin As String) As List(Of String)
        Dim retVal As New List(Of String)
        '.....
        retVal.Add("Add System 1 to the Generic list")
        retVal.Add("Add System 2 to the Generic list")
        '...
        Return retVal
    End Function


End Class

Fernando
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now