Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to create a set of objects in VB.NET

3 Answers

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main()
        Dim fruits As HashSet(Of String) = New HashSet(Of String)()
        fruits.Add("Apple")
        fruits.Add("Banana")
        fruits.Add("Strawberry")

        For Each fruit In fruits
            Console.WriteLine(fruit)
        Next
    End Sub
End Class


' run:
'
' Apple
' Banana
' Strawberry
'

 



answered Apr 22, 2025 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main()
        Dim fruits As HashSet(Of String) = New HashSet(Of String) From {
            "Apple",
            "Banana",
            "Strawberry"
        }

        For Each fruit In fruits
            Console.WriteLine(fruit)
        Next
    End Sub
End Class



' run:
'
' Apple
' Banana
' Strawberry
'

 



answered Apr 22, 2025 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

' Define a class representing a fruit
Public Class FruitClass
    ' Property to store the name of the fruit
    Public Property Name As String

    ' Constructor to initialize the FruitClass object with a name
    Public Sub New(name As String)
        Me.Name = name
    End Sub

    ' Override the ToString method to return the name of the fruit
    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

' Main module where program execution begins
Module Program
    Sub Main()
        ' Create a HashSet collection to store unique FruitClass objects
        ' The HashSet ensures that duplicate fruits (based on their property values) are not added
        Dim fruits As New HashSet(Of FruitClass) From {
            New FruitClass("Apple"),     ' Create a FruitClass object with the name "Apple"
            New FruitClass("Banana"),    ' Create a FruitClass object with the name "Banana"
            New FruitClass("Strawberry") ' Create a FruitClass object with the name "Strawberry"
        }

        ' Iterate through the HashSet and print the name of each fruit
        For Each fruit In fruits
            Console.WriteLine(fruit) ' Print the fruit object, which calls the overridden ToString method
        Next
    End Sub
End Module




' run:
'
' Apple
' Banana
' Strawberry
'

 



answered Apr 22, 2025 by avibootz
edited Apr 22, 2025 by avibootz

Related questions

1 answer 155 views
155 views asked Aug 5, 2022 by avibootz
1 answer 177 views
1 answer 150 views
150 views asked Apr 12, 2016 by avibootz
1 answer 208 views
1 answer 183 views
...