I'm a little confused by your references. I assume that "frmOptions" is actually
"form2" from your initial discussion. I also assume that "TimerMinutes" is
a member of this same "form2/frmOptions." Using the Shared keyword says,
"No matter how many instances of form2/frmOptions I create, just use a single
version of TimerMinutes among them all. Because of this, it's not possible
to reference shared members using an instance variable. You should instead
reference them using the generic class name.
form2.TimerMinutes
But this is probably not what you want. If you have multiple form2/frmOptions
open, you probably want each one to expose its own value. I would remove
the "Shared" keyword. Then you can reference "F3.TimerMinutes" directly.
If you really want a global variable, use a "Module..End Module" block and
add the global variable inside of it.
Module SomeGlobals
Public TimerMinutes As ComboBox
End Module
Personally, since I only care about the value exposed by the ComboBox, I
would create TimerMinutes as a string, or integer, or whatever, but not a
full ComboBox. Form1 doesn't care that the value came from a ComboBox; it
only cares about the value. Part of encapsulating things in classes is to
hide the inner workings. Why should Form1 care how TimerMinutes came about
within Form2? It should only care about the value itself. Therefore, I would
expose a simple string or integer (whatever meets your needs) and have Form1
access this member instead.
Class Form2
Public TimerMinutes As Integer = 0
Private Sub ComboBox1_SelectedIndexChanged(sender, e)
If (ComboBox1.SelectedIndex <-1) Then
Me.TimerMinutes = CInt(ComboBox1.SelectedItem.ToString)
End If
End Sub
End Class
Class Form1
Private Sub SomeRoutine()
Dim F3 As New Form2
...
MsgBox("Timer Minutes = " & F3.TimerMinutes)
...
End Sub
End Class
As another poster indicated, you should enable Option Strict and Option Explicit.
Letting VB decide how to convert ComboBox.Text to Integer is just asking
for trouble. You should be explicit and use CInt() or one of the other conversion
functions.
-----
Tim Patrick -
www.timaki.com
Start-to-Finish Visual Basic 2005
Hello,
I'm have defined a combobox on form2 and I'm trying to use that value
in form1. I have defined a global value so that form1 can retrieved
the modified value as:
Public Shared TimerMinutes As ComboBox
In form2, this value is modifed with ComboBox1_SelectedValueChanged()
and ComboBox1_SelectedIndexChanged() methods. Once the value has
changed I set the TimerMinutes to ComboBox1:
TimerMinutes = ComboBox1
After this global value has been modified, I try to retrieve the value
in form2 by
Dim F3 As New frmOptions
Dim temp As Integer
temp = F3.TimerMinutes.Text
The value that in TimerMinutes is always the default value. It never
has the modified value. Any help is appreciated.