Use Cases for Shared Functions in VB.NET – Reliable Hosting ASP.NET Reviews
ASP.NET Hosting

Use Cases for Shared Functions in VB.NET

Static functions in C# are comparable to shared functions or share methods in Visual Basics.NET (VB). We’ll talk about the Shared function’s use case here. Factory methods, helper functions, logger classes, access control, etc. are a few examples.

Methods in the Factory

By generating instants or objects, the Class’s properties and methods can be utilized outside. However, the function mentioned by the Shared keyword can only be utilized outside of the class name. Making a class and using the named factory Method again is a frequent application operation.

Public Class Employee
    Public Property Name As String

    ' Factory method
    Public Shared Function Create(name As String) As Employee
        Dim emp As New Employee()
        emp.Name = name
        Return emp
    End Function
End Class

Configuration Management

App configuration makes it read-only with a shared function is useful for making common database connections all over the application.

Public Class AppConfig
    Public Shared ReadOnly Property DatabaseConnectionString As String
        Get
            Return "Server=myServer;Database=myDB;User Id=myUser;Password=myPass;"
        End Get
    End Property
End Class

Logging

Accessing the Logger function anywhere in the application shared function can be used for logging the status of an application.

Public Class Logger
    Public Shared Sub Log(message As String)
        ' Code to log message
        Console.WriteLine($"Log: {message}")
    End Sub
End Class

Access Control

Shared functions also can be used in access control logic for admin and user login.

Public Class Admin
    Private Shared _adminCount As Integer

    Public Shared Sub AddAdmin()
        _adminCount += 1
    End Sub

    Public Shared Function GetAdminCount() As Integer
        Return _adminCount
    End Function
End Class

ASP.NET Core 8.0.11 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.