Converting a class with a property of T to and from json - vb.net

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

Related

How to raise an events when global variable changed in vb.net

I've global variables in module level with below COM-interface types.
Imports System.IO
Imports simpackcomslvLib
Imports simpackcompostLib
Module Globals
Public Srv As SpckCOMApp
Public Mdl As IScrModel
Public Post As PostComApp
Public Res As PostComProject
End Module
In another classes some of my procedures change their object values. I'd like to run some of my procedures which made some changes on my tool's GUI when the for example Mdl value is changed.
I tried with below method which made for an integer type parameter but i didnt succeded for my case, i think because of their object(I types belongs to COM-interface.
Public Class myVar
Private mValue As Integer
Public Event VariableChanged(ByVal mvalue As Integer)
Public Property Variable() As Integer
Get
Variable = mValue
End Get
Set(ByVal value As Integer)
mValue = value
RaiseEvent VariableChanged(mValue)
End Set
End Property
End Class
Usage of above code in an example
Public Class Form1
Private WithEvents test As New myVar
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
test.Variable = CInt(TextBox1.Text)
End Sub
Private Sub VariableChanged(ByVal NewValue As Integer) Handles test.VariableChanged
MessageBox.Show(NewValue)
End Sub
End Class
Is there anyway to implement my below variables in a module such a way also using in module level is wrong should i move them under the class?
Module Globals
Public Srv As SpckCOMApp
Public Mdl As IScrModel
Public Post As PostComApp
Public Res As PostComProject
End Module

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

Assign a value to a property depending on initialized Class

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)

retrieve data out of a public structure and collection

I'm using a collection and structure to store some parsed data in a class, but when i try to retrieve the data it is null. On form1 i'm i can get the response string which is all the raw data. Am I adding the parsed data to the collection correctly? Did I call it on form1 correctly?
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Dim dloader as new Downloader(blah)
'this does not work
Dim test as new _ListInfo
msgbox(test.Name) ' produces an empty message box
'this works
msgbox(dloader.Download)
End Sub
Here is my code for the class:
Public Structure _Info
Dim Name As String
End Structure
Public Class Downloader
Dim _ListCollection As New Collection(Of _ListInfo)
Public ReadOnly Property ListCollection() As Collection(Of _ListInfo)
Get
ListCollection = _ListCollection
End Get
End Property
Public Function Download() As String
'doing the download
ParseList()
Return _ResponseString
End Function
Private Sub ParseList()
_ListCollection.Clear()
Dim Name As String
Dim MyInfo As _ListInfo
MyInfo.Name = Name
_ListCollection.Add(MyInfo)
End Sub
Why do you expect it to work? You are just newing-up a structure and accessing a property. Don't you want to do something like:
Dim dloader as new Downloader(blah)
dloader.Download()
' Show first name.
MsgBox(dloader.ListCollection(0).Name)