vb.net 2008: Newbie detecting if form exists question

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
 
J

Jack Jackson

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.
 
S

Scott M.

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
 
F

Family Tree Mike

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.
 
J

Jack Jackson

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.
 
R

Rob W

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.
 
S

Scott M.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top