Dim scope within an IF

G

Gary Shell

I have two forms both derived by inheriting the same base form. I have a
third form that needs to instantiate one of those two forms, set some
Protected properties on the two forms, display the form, test a property of
the from when it is closed and then dispose of the instantiated form.

The third form has two radio buttons used to determine wich form to
instantiate. The folllowing code fails to compile due to the scope of the
DIM statement.

Private Sub btnEditTheClass_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEditTheClass.Click
If RadioButton_BP.Checked Then
Dim frm As New frmBP_Class
ElseIf RadioButton_IO.Checked Then
Dim frm As New frmIO_Class
End If

'pass a connection to SQL
frm.sqlConnection = SqlConnection1
'pass the TheClass to be edited
frm.TheClass = txtTheClass.Text
'set EDIT mode
frm.Mode = frm.Modes.EditMode
'show as a MODAL form
frm.ShowDialog()
'if the data was updated
If frm.DataUpdated = True Then
'display the updated data
txtTheClass.Text = frm.TheClass
End If

'dispose of the form
frm.Dispose()
End Sub

Because of this I have to resort to the following:

Private Sub btnEditTheClass_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEditTheClass.Click
If RadioButton_BP.Checked Then
Dim frm As New frmBP_Class
'pass a connection to SQL
frm.sqlConnection = SqlConnection1
'pass the TheClass to be edited
frm.TheClass = txtTheClass.Text
'set EDIT mode
frm.Mode = frm.Modes.EditMode
'show as a MODAL form
frm.ShowDialog()
'if the data was updated
If frm.DataUpdated = True Then
'display the updated data
txtTheClass.Text = frm.TheClass
End If
'dispose of the form
frm.Dispose()
ElseIf RadioButton_IO.Checked Then
Dim frm As New frmIO_Class
'pass a connection to SQL
frm.sqlConnection = SqlConnection1
'pass the TheClass to be edited
frm.TheClass = txtTheClass.Text
'set EDIT mode
frm.Mode = frm.Modes.EditMode
'show as a MODAL form
frm.ShowDialog()
'if the data was updated
If frm.DataUpdated = True Then
'display the updated data
txtTheClass.Text = frm.TheClass
End If
'dispose of the form
frm.Dispose()
End If
End Sub



YUCH!!!!! That is truly UGLY. But I, for the life of me, can't find any
other way to do this. Suggestions? Clue by fours?

Thanks,

Gary
 
R

Rob Teixeira [MVP]

Private Sub btnEditTheClass_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEditTheClass.Click

Dim frm As BaseClassFormName

If RadioButton_BP.Checked Then
frm = New frmBP_Class
ElseIf RadioButton_IO.Checked Then
frm = New frmIO_Class
End If

' rest of code goes here


-Rob Teixeira [MVP]
 
O

One Handed Man \( OHM - Terry Burns \)

Put the dim statement before the IF and the instantiation within the IF

HTH

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
G

Gary Shell

Initially, I thought I couldn't do that, I tried "Dim frm as Form" but of
course that didn't work as DIM'ing as a generic form meant I wouldn't have
access to my own properties I'd added to my forms. I was in the dark until
I got Rob's "clue by four" upside my head. <grin>

I then realized I could Dim the form using the BASE class of the multiple
inherited forms. Then in the IF/END IF I could instantiate the proper
inherited form based on the radio button selection.

Now the code looks like this:
Private Sub btnEditTheClass_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEditTheClass.Click
Dim frm As TheClassForm.frmTheClass
If RadioButton_BP.Checked Then
frm = New frmBP_Class
ElseIf RadioButton_IO.Checked Then
frm = New frmIO_Class
End If

'pass a connection to SQL
frm.sqlConnection = SqlConnection1
'pass the TheClass to be edited
frm.TheClass = txtTheClass.Text
'set EDIT mode
frm.Mode = frm.Modes.EditMode
'show as a MODAL form
frm.ShowDialog()
'if the data was updated
If frm.DataUpdated = True Then
'display the updated data
txtTheClass.Text = frm.TheClass
End If
'dispose of the form
frm.Dispose()
End Sub

Much "prettier" don'tcha think?

Gary
 
O

One Handed Man \( OHM - Terry Burns \)

Well Done !

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 

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