Var on diff Form

Better to place a property in the form you want to read from...

VB.NET:
Friend Property 'Variable' As 'Type'
Get
Return 'the variable you want to access'
End Get
Set(Byval Value as 'Type')
'Your variable' = value
End Set
End Property


If you try to write to a private variable in a different class, the compiler will sometimes complain. Something to the tune of...

'Blah Blah 'varaible' blah blah because 'variable' has System.MarshalByRef as a base class Blah'
 
Last edited:
Form1.vb
Public Class frmDesign

Friend Property clrHi() As Color
Get
Return clrHi
End Get
Set(ByVal value As Color)
clrHi = Color.Tan
End Set
End Property

Customize.vb
Private Sub btnCcolor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCcolor.Click
Dim clrhi As Color

If cdHi.ShowDialog() = Windows.Forms.DialogResult.OK Then
clrhi = cdHi.Color
End If

End Sub

'clrHi = Color.Tan' from Form1 is green underlined, this fine?
 
No, thats not quite right, but nearly. In order to access any varaible from any class that isn't declared with the 'Shared' keyword you need a instance if that class.

VB.NET:
Form1.ClrHi = Color.Tan

Also, in your property block, because of the way you have done it you will get a stackoverflow exception. You need to create a private class level variable to hold the value of the property.

VB.NET:
Private _ClrHi as Color
 
Friend Property ClrHi as color
Get
Return Me._ClrHi
End Get
Set(ByVal Value As Color)
Me._ClrHi = value
End Set
End Property
 
Last edited:
Back
Top