jlnelmes
Play-to-Earn Innovator
LEVEL 1
200 XP
In my tutorial “How to Pass Value from One Form to another Form” I discussed how the other form received a value by using a Public Variable.
There are so many discussions on the Internet that argues whether Public Variable is a good programming practice compare to Property. Well, IMHO public variable is an option for simplicity. You do not need to waste line of code just to get the same result.
However, there are some certain areas that we need to consider like OOP and Security. Public Variables as the name implies is subject to vulnerabilities. Arjay_nacion has also commented to my tutorial which breaks the encapsulation principle of OOP.
Without further ado let’s get on the code.
Add two forms in your project called “Form1” and “Form2”. Add the necessary controls as shown in the following screenshot.
[inline:Pass_Value.jpg=Pass values between Windows Forms]
In Form1 add the following code:
In Form2 add the following code:
I hope this helps.
There are so many discussions on the Internet that argues whether Public Variable is a good programming practice compare to Property. Well, IMHO public variable is an option for simplicity. You do not need to waste line of code just to get the same result.
However, there are some certain areas that we need to consider like OOP and Security. Public Variables as the name implies is subject to vulnerabilities. Arjay_nacion has also commented to my tutorial which breaks the encapsulation principle of OOP.
Without further ado let’s get on the code.
Add two forms in your project called “Form1” and “Form2”. Add the necessary controls as shown in the following screenshot.
[inline:Pass_Value.jpg=Pass values between Windows Forms]
In Form1 add the following code:
- Private
Sub
Button1_Click(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
Button1.
Click
- Dim
Frm2 As
Form2
- Frm2 =
New
Form2
- Frm2.
GetFolioNumber
=
txtFolioNumber.
Text
- Frm2.
Show
(
)
- End
Sub
In Form2 add the following code:
- Dim
FolioNumber As
String
- Public
WriteOnly
Property
GetFolioNumber(
)
- Set
(
ByVal
value)
- FolioNumber =
value
- End
Set
- End
Property
- Private
Sub
Form2_Load(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
MyBase
.
Load
- txtFolioNumber.
Text
=
FolioNumber
- End
Sub
I hope this helps.