Assign a value to a property depending on initialized Class - vb.net

I try to assign a value (Test1) to a Property (Wealth) dynamically so that depending on the initialized Class the calculated value is different. But all I get as a result is 0. Could anyone explain me why and how I can solve the problem.
Public Class Class1
Private _test1 As Integer
Overridable ReadOnly Property Test1 As Integer
Get
Return _test1
End Get
End Property
Public ReadOnly Property Wealth As Integer
Get
Dim rnd As New Random
Dim val As Integer = rnd.Next(1, 6)
Return val * _test1
End Get
End Property
End Class
Public Class Class2
Inherits Class1
Public Overrides ReadOnly Property Test1 As Integer
Get
Return 3
End Get
End Property
End Class
Initialisation:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As New Class2
MsgBox(t.Wealth.ToString)
End Sub
End Class

Don't use the private variable, you need to reference the property itself.
Public Class Form1
Public Class Class1
Overridable ReadOnly Property Test1 As Integer
Get
Return 0 'Default value'
End Get
End Property
Public ReadOnly Property Wealth As Integer
Get
Dim rnd As New Random
Dim val As Integer = rnd.Next(1, 6)
Return val * Test1 'Changed! Uses the Property name, so that if it is overridden it uses the new version'
End Get
End Property
End Class
Public Class Class2
Inherits Class1
Public Overrides ReadOnly Property Test1 As Integer
Get
Return 3
End Get
End Property
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As New Class2
MsgBox(t.Wealth.ToString)
End Sub
End Class

Sounds like you need a constructor.
Public Class Class1
Public Sub New(int As Integer)
Me.test1 = int
End sub
...
Then when you declare it
Dim t As New Class1(5)
MsgBox(t.Wealth.ToString)

Related

Converting a class with a property of T to and from json

I have a class (MyMessage) which have a property called "Settings" of Type T.
I need to convert MyMessage to json, send it via TCP and when I recieve it, I need to test what class Type T is and then convert the recieved json to the MyMessage Of T class.
This is my code so far - function SendMessage and MessageRecieved is not working ... and I need your help :)...
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As New MyMessage(Of MyMessageSettingsText)
t.Settings.Text = "Yes"
Call SendMessage(t)
Dim n As New MyMessage(Of MyMessageSettingsNumber)
n.Settings.Number = 1
Call SendMessage(n)
End Sub
Private Sub SendMessage(msg As MyMessage)
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(msg)
' Do send using tcp etc
End Sub
Private Sub MessageRecieved(msgJson As String) ' A json string recived from a tcp server
Dim msg As MyMessage = DirectCast(Newtonsoft.Json.JsonConvert.DeserializeObject(Of MyMessage)(msgJson), MyMessage)
If TypeOf (msg.Settings) Is MyMessageSettingsText Then
Dim t As MyMessage(Of MyMessageSettingsText) = CType(msg, MyMessage(Of MyMessageSettingsText))
' do something with t
End If
End Sub
End Class
Public MustInherit Class MyMessageSettingsBase
End Class
Public Class MyMessageSettingsText
Inherits MyMessageSettingsBase
Public Property Text As String
End Class
Public Class MyMessageSettingsNumber
Inherits MyMessageSettingsBase
Public Property Number As Integer
End Class
Public Class MyMessage(Of T As {New})
Public Property Name As String
Public Property Settings As New T
End Class

How to select which object to instantiate without Select Case?

Say I have three classes. A base class Person and two other classes (Employee and Manager) each inherits from Person.
Public Class Person
Public Property Name As String
Public Overridable ReadOnly Property Salary As Decimal
Get
Return 0
End Get
End Property
Public Sub New()
End Sub
End Class
Class Employee:
Public Class Employee
Inherits Person
Overrides ReadOnly Property Salary As Decimal
Get
Return 100
End Get
End Property
Sub New()
MyBase.New()
End Sub
End Class
Class Manager:
Public Class Manager
Inherits Person
Overrides ReadOnly Property Salary As Decimal
Get
Return 1000
End Get
End Property
Sub New()
MyBase.New()
End Sub
End Class
My problem is how to create a new Person(based on a ListBox) that can be either Person/Employeeor Manager and retrieve the Salary Property without going through Select Case or If-else in the ListBox1_SelectedIndexChanged event. To do this selection, I have added another class, named Identify, that takes the selected index of the Listbox and pass it to getProsonType method and return the selected category. Please look at my code below.
The form1 code looks like:
Public Class Form1
Private P As Identify
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
ListBox1.Items.Add("Person")
ListBox1.Items.Add("Employee")
ListBox1.Items.Add("Manager")
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As
EventArgs) Handles ListBox1.SelectedIndexChanged
P = New Identify(CType(ListBox1.SelectedIndex, PersonType))
Dim PGeneral As Person
PGeneral = P.GeneralPerson
Label1.Text = PGeneral.Salary
End Sub
End Class
I have assigned the three types to a public Enum PersonType just to restrict the selection.
Public Enum PersonType
Person = 0
Employee = 1
Manager = 2
End Enum
The Identity Class looks like:
Public Class Identify
Public Property PType As PersonType
Public ReadOnly Property GeneralPerson As Person
Get
Return getProsonType(PType)
End Get
End Property
Private Function getProsonType(ByVal SomeOne As PersonType) As Person
Dim pp As Person
Select Case SomeOne
Case PersonType.Person
pp = New Person()
Case PersonType.Employee
pp = New Employee()
Case PersonType.Manager
pp = New Manager()
End Select
Return GeneralPerson
End Function
Sub New(ByVal PersonType As PersonType)
Me.PType = PersonType
End Sub
End Class
After running the project I get the error System.StackOverflowException. I am not sure if this is the cleanest or the correct way to do this and I looked in many places and reached this dead end!
Please help me to correct this or find a better way.
Thanks in advance.

How to create a new event for my own custom control?

I have a class which inherit from Panel, and below are some member in this class
Public ItemName As String
Public Quantity As Integer
Public Price As Decimal
Public DiscountAmount As Decimal
How can I create a event when Quantity or DiscountAmount changed then run a function?
I try to write in this way but I get error:-
Private Sub info_Changed(sender As Object, e As EventArgs) Handles Quantity.Changed, DiscountAmount.Changed
myFunction()
End Sub
Error:
Handles clause requires a WithEvents variable defined in the
containing type or one of its base types.
You need to declare the events in user control and then consume those. See below code. I have created a user control UserControl1. This control raises events when Price or DiscountAmount is changed. The usercontrol is then used in Form1. You can use the same approach to change the Quantity.
Public Class Form1
Private WithEvents userCntrl As New UserControl1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeValues()
End Sub
Private Sub ChangeValues()
userCntrl.Price = 100
userCntrl.DiscountAmount = 12
End Sub
Private Sub userCntrl_Price_Changed(newValue As Decimal) Handles userCntrl.Price_Changed, userCntrl.DiscountAmount_Changed
MessageBox.Show("New value = " & newValue.ToString)
End Sub
End Class
Public Class UserControl1
Public Event Price_Changed(ByVal newValue As Decimal)
Public Event DiscountAmount_Changed(ByVal newValue As Decimal)
Public ItemName As String
Public Quantity As Integer
Private Price_ As Decimal
Public Property Price() As Decimal
Get
Return Price_
End Get
Set(ByVal value As Decimal)
If value <> Price_ Then
RaiseEvent Price_Changed(value)
End If
Price_ = value
End Set
End Property
Private DiscountAmount_ As Decimal
Public Property DiscountAmount() As Decimal
Get
Return DiscountAmount_
End Get
Set(ByVal value As Decimal)
If value <> DiscountAmount_ Then
RaiseEvent DiscountAmount_Changed(value)
End If
DiscountAmount_ = value
End Set
End Property
End Class

How to display public variables in another form

I've already searched this up many times but none of them worked for me so please help. The code I've been trying to use is:
'Making the variables public in form2
Public Module GlobalVariables
'Making the variables public
Public Property Juvenplp As Integer
Public Property Adultplp As Integer
Public Property Senileplp As Integer
Public Property Juvensr As Single
Public Property Adultsr As Single
Public Property Senilesr As Single
Public Property Birthrate As Single
Public Property genmore As Integer
Public Property i As Integer
End Module
Displaying then in Form4
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
InitializeComponent()
GlobalVariables.Juvenplp = Me.Label7.Text
GlobalVariables.Adultplp = Me.Label8.Text
GlobalVariables.Senileplp = Me.Label9.Text
GlobalVariables.Juvensr = Me.Label10.Text
GlobalVariables.Adultsr = Me.Label11.Text
GlobalVariables.Senilesr = Me.Label12.Text
GlobalVariables.Birthrate = Me.Label14.Text
End Sub
The problem is that they're not being displayed.

How do you pass values from a textbox to a public property in a class?

I need to pass code that user enters into a Textbox to a Public Property within a class. Here is my code.
Form2.vb Code
Public Class Form2
Dim class2A As part2Class = New part2Class()
Dim class2B As part2BClass = New part2BClass()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = CType(TextBox1.Text, Integer)
Dim b As Integer = CType(TextBox1.Text, Integer)
part2Class._Num1 = a
part2Class._Num2 = b
End Sub
Here is my code in part2Class.vb
Public Class part2Class
Public Property _Num1
Public Property _Num2
Public Overridable Function Calculate() As Integer
Return _Num1 + _Num2
End Function
End Class
I get an error saying "Reference to a non-shared member requires an object reference." How do I pass the values from the textboxes to the public property values?
Thanks!
You need to create an instance of a class first before accessing it's members:
Dim objpart2Class as part2Class = new part2Class()
objpart2Class._Num1 = a
objpart2Class._Num2 = b