vb.net 2008: Newbie detecting if form exists question

  • Thread starter Thread starter Rob W
  • Start date Start date
R

Rob W

Greetings,

At top of my form/class code I dimension a form object
Dim transferdialog As Form

I then on a button check if the form exists or not using the code below:-

If transferdialog Is Nothing Then

Dim transferdialog As New dlgTransferDetails(txtMemNum.Text.ToString)

transferdialog.Show()

MessageBox.Show(transferdialog.transferLocation)

Else

MsgBox("Already Exists")

transferdialog.Show()

End If



On the dialog form dlgTransferDetails the code on the OK button is as
follows:-



Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdOk.Click

Me.DialogResult = System.Windows.Forms.DialogResult.OK

cTransferLocation = Me.txtBranch.Text.ToString

Me.Hide()

End Sub



Why does it always think that transferdialog is nothing when a new instance
of dlgTransferDetails has been created?



Thanks

Rob
 
Greetings,

At top of my form/class code I dimension a form object
Dim transferdialog As Form

I then on a button check if the form exists or not using the code below:-

If transferdialog Is Nothing Then

Dim transferdialog As New dlgTransferDetails(txtMemNum.Text.ToString)

transferdialog.Show()

MessageBox.Show(transferdialog.transferLocation)

Else

MsgBox("Already Exists")

transferdialog.Show()

End If



On the dialog form dlgTransferDetails the code on the OK button is as
follows:-



Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdOk.Click

Me.DialogResult = System.Windows.Forms.DialogResult.OK

cTransferLocation = Me.txtBranch.Text.ToString

Me.Hide()

End Sub



Why does it always think that transferdialog is nothing when a new instance
of dlgTransferDetails has been created?



Thanks

Rob

Because you have two variables named transferdialog. One is a
class-level variable, and the other is local to some method,
presumably the button click event handler. You check the class level
variable for Nothing, then you set the one local to the method. I
would expect you to get an error message - VB normally warns you when
you define a variable that hides a variable of the same name in a
different scope.

Try changing these lines (I also removed the duplicated call to Show):

If transferdialog Is Nothing Then
transferdialog = New dlgTransferDetails(txtMemNum.Text.ToString)
Else
MsgBox("Already Exists")
End If
transferdialog.Show()

Also, this:
At top of my form/class code I dimension a form object
Dim transferdialog As Form
is incorrect. You are defining a variable that can hold a reference
to a form object. This statement just defines the variable, it does
not create or to anything to a form object.

When defining class level variables, I prefer to use keywords like
Private or Public rather than Dim to explicitly indicate their scope,
at least partly because I am never sure what the scope of Dim is.
 
Dim is explicit. If used within a code block, it's block level. If used
outside of a code block, but still insdie a procedure, it's local to the
procedure. And, if used outside of a procedure, it's module level.

By the way, for consistency, drop the MsgBox and just go with
MessageBox.Show.

-Scott
 
Scott said:
By the way, for consistency, drop the MsgBox and just go with
MessageBox.Show.

-Scott

I agree with the sentiment, but I don't think "consistency" is the right
word. I never understood why this was brought forward from VB 6.
 
I should not have used the word scope.

I meant that I find Dim at the class level unclear because it does not
explicitly indicate if the variable is Private, Protected or Public.
 
Thanks for all the advice.

I do use msgbox when debugging purely for speed but from now on will drop
it.

I understand now about when and how to declare variables/objects and their
scope.

I now define transferdialog as type dlgTransferDetails instead of form so I
can access all of its properties.
All is now functioning as I intended.

Next time I post I will ensure to check the basic things first.

Cheers all for the advice and guidance, appreciated.
 
True, but Dim at the module level can only mean module level, which is just
what Private means. So, at that level, Dim and Private are interchangable.
 
Back
Top