Get Value from a Class to a TextBox in a Form
In this code I can Get SerialNumber and Name in MsgBox. I also would like to put them in three TextBox in a Form. How can I do this ?. Thank you.
Class Code :
Code:
Public Class Item
Public Shared Count As Integer = 1
Private userNameValue As String
Public Shared Sub ShareMethod()
MsgBox("Current value of Count: " & Count)
End Sub
Public Sub New(ByVal Name As String)
' Use Count to initialize SerialNumber.
Me.SerialNumber = Count
Me.Name = Name
' Increment the shared variable
Count += 1
End Sub
Public SerialNumber As Integer
Public Name As String
Public Sub InstanceMethod()
MsgBox("Information in the first object: " &
Me.SerialNumber & vbTab & Me.Name)
End Sub
End Class
Form Code :
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TestShared()
End Sub
Sub TestShared()
' Create two instances of the class.
Dim part1 As New Item("keyboard")
Dim part2 As New Item("monitor")
part1.InstanceMethod()
part2.InstanceMethod()
Item.ShareMethod()
End Sub
End Class
Re: Get Value from a Class to a TextBox in a Form
This seems like a rather odd question. If you want to get the SerialNumber and Name values then go ahead and do it. They're both Public. What's stopping you? If you want to then display those values in TextBoxes, what's stopping you? How do you usually display something in a Textbox? Why would this be any different? I can't help thinking that you have just copied some code form somewhere and given little thought to how it actually works, then asked how to do something without actually thinking about how you might do it first. The answer to your question should be quite obvious.
Re: Get Value from a Class to a TextBox in a Form
Name and SerialNumber are public members so you can retrieve their value from outside the class like this
VB.NET Code:
Dim part1 As New Item("keyboard")
TextBox1.Text = part1.Name
TextBox2.Text = part1.SerialNumber.ToString
Re: Get Value from a Class to a TextBox in a Form
Works !!!. Thank you 4x2y. Best Regards.:wave: