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.
Related
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
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
Question #1: Does this look like the proper way to use inheritance in my class? I plan to add methods to the PartPnl so that both RoughPnl and FinalPnl can use together by using their objects specific data.
Question #2: Using the default values in the properties seems like the best way to get values to my object from my main form. Is that the best way to do that or is there a different approach? The main form will be open the entire time but the object will only exist long enough to do what I need, like create RoughPnl->Use a Method or two->Add Panel to form->Done
Namespace PanelThings
Public Class PartPnl
Private TrvList As TreeView
Private BasePanel As Panel
Private Label As String = Main.cboLabel.Text
Private Qty As String = Main.cboQty.Text
Private Key As String = Nothing
Private HorizontalON As Boolean = Main.chkHorizontal.Checked
Private VerticalON As Boolean = Main.chkVertical.Checked
Private ScaleToPanelON As Boolean = Main.chkScaleToPanel.Checked
Private Scale As String = Main.cboScale.Text
Private RearrangeON As Boolean = Main.chkRearrange.Checked
Public Sub New(objTree As TreeView, basePanel As Panel)
TrvList = objTree
basePanel = basePanel
End Sub
End Class
Public Class FinalPnl
Inherits PartPnl
Private FinalWidth As String = Main.cboWidth.Text
Private FinalLength As String = Main.cboLength.Text
Private FinalColor As String = Main.picFinalColor.Tag
Private LabelON As Boolean = Main.chkLabelsON.Checked
Private SizeON As Boolean = Main.chkSizeON.Checked
Private PorSKey As String = "P"
Public Sub New(objTree As TreeView, basePanel As Panel)
MyBase.New(objTree, basePanel)
End Sub
End Class
Public Class RoughPnl
Inherits FinalPnl
Private RoughWidth As String = Main.cboWidthExtra.Text
Private RoughLength As String = Main.cboLengthExtra.Text
Private RoughColor As String = Main.picRoughColor.Tag
Private RoughON As Boolean = Main.chkRoughUnderFinal.Checked
Public Sub New(objTree As TreeView, basePanel As Panel)
MyBase.New(objTree, basePanel)
End Sub
End Class
End Namespace
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
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)