is this a bug? or am I wrong?
I have a button_click code inside my form as follow:
Code:
MsgBox(ComboPosIni.SelectedIndex)
myEPD = New EPD_Class()
ComboPosIni is a combobox that is inside my form. When executing the msgbox displays 1 as item selected.
And in EPD_Class new() i have this code:
Code:
If (myForm.ComboPosIni.SelectedIndex = 0) then
MsgBox(myForm.ComboPosIni.SelectedIndex)
Return
End If
My problem is that first msgbox show '1' as item selected, and when new() is executed is show '0' as item selected. This is wrong, as both msgbox would have to show the same result.
Is this a bug? how can I solved it?
thanks
Re: is this a bug? or am I wrong?
Hmm... that looks to me like its not a good design.
If I understand it your class is trying to access controls on an arbitrary form called "myForm".
If ain instance of a class needs to access the properties of a form you should really pass a reference to the form in to the class, either as a property of the class, or when initialising it, ie
Code:
Dim _ParentForm as Form
Public Sub New(ByRef ParentForm as Form)
_ParentForm = ParentForm
if _ParentForm.ComboPosIni.Selectedindex = 0 then
Msgbox(myForm.ComboPosIni.SelectedIndex)
end if
End Sub
If the only aspect of the form you are interested in is the combobox itself then have "New" accept a combobox instead of a form.
Note though - is New() as function? You are using "Return" in there - which is generally only used for setting the return value of a function.
Also - your If...Then block can be simplified : you know that myForm.ComobPosIni.SelectedIndex = 0 if you are inside the loop so why not just MsgBox("0")?
Re: is this a bug? or am I wrong?
I think that your problem exists in the referencing of the form itself. You should change your New() sub to accept a reference to the form in question:
Code:
Public Sub New(Byref F as MyForm)
Also, unless this is the main form, you should be creating new instances of the form instead of calling its name, so:
Code:
Dim F as new MyForm
F.Show
instead of:
edit - beaten by Paul, again!
Re: is this a bug? or am I wrong?
Yes this is likely a design issue. You'd have consider, why would this object depend on a specific selection of a combo box on a specific form? This would be like determining what car to drive to work based on the street name of where the store is that you shop for groceries.
A good 'test' is to take your class and put it in a new project: what errors do you get? Does it rely on specific instances of objects? (This isn't a 100% test, but allows you to determine what are valid references and what aren't). Specifically, an object should not have to rely on it being placed on a specific form, containing a specific named control. The classes should be as encapsulated as possible.
In the OPs case - I don't categorically know, due to the limited info supplied - but you might want to think about passing an integer to the class through the constructor. Presumably, the combo box selected index means something; it's not literally the selected index you want at all, but 'something else'. So you could have something like:
Public sub new(byVal numberOfCowsInTheField as integer)
End Sub
And you'd pass in the appropriate value.
Re: is this a bug? or am I wrong?
Thank you all for the answers. Now it works perfect passing argument in the constructor.
cheers
FS