Program to create/write and set properties at runtime in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 3050
The below program will do so

Imports System.Collections.Generic

Namespace ConsoleApplication1

Class DynamicProperties
Private properties As New Dictionary(Of String, Object)()

Public Default Property Item(name As String) As Object
Get
If properties.ContainsKey(name) Then
Return properties(name)
End If
Return Nothing
End Get
Set
properties(name) = value
End Set
End Property

End Class

Class Program
Private Shared Sub Main(args As String())
Dim year As Integer = 2015
Dim month As Integer = 3

Dim year_MnthProperty = New DynamicProperties()

'set the property names with values
Select Case month
Case 1
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 100
Exit Select
Case 2
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 200
Exit Select
Case 3
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 300
Exit Select
Case 4
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 400
Exit Select
Case 5
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 500
Exit Select
Case 6
year_MnthProperty(String.Concat(GetMonthName(month), year)) = 600
Exit Select
End Select

'read the property values
Dim propertyName = String.Concat(GetMonthName(month), year)
Dim propertyValues = year_MnthProperty(String.Concat(GetMonthName(month), year))
End Sub

Private Shared Function GetMonthName(month As Integer) As String
Dim monthName As String = String.Empty
Select Case month
Case 1
monthName = "January"
Exit Select
Case 2
monthName = "February"
Exit Select
Case 3
monthName = "March"
Exit Select
Case 4
monthName = "April"
Exit Select
Case 5
monthName = "May"
Exit Select
Case 6
monthName = "June"
Exit Select
End Select
Return monthName
End Function
End Class
End Namespace

Comments or Responses

Login to post response