How To Use The With Statement

ChubbyArse

Newcomer
Joined
Jun 10, 2003
Messages
19
Hi All.

I have a code snippett, that I'd like to use the with statement, to make it more readable.

If frmComponentSearchForm Is Nothing Then
frmComponentSearchForm = New frmPartSearch
frmComponentSearchForm.MdiParent = Me.ParentForm
frmComponentSearchForm.Show()
Else
frmComponentSearchForm.Show()
End If

How would I use the with statement, beacuse as far as I know I can't use the = operator with the object which is the subject of the with:

With frmComponentSearchForm
If withobject Is Nothing Then
withobject = New frmPartSearch
.MdiParent = Me.ParentForm
.Show()
Else
.Show()
End If
End With

Is there a better may of doing this?

Thanks Very Much!
 
the If statement seems fine to me, although you could use a Boolean value to see if a form exsists or not, eg:
Visual Basic:
Private Exsists As Boolean 
'/// somewhere below the windows generated code section ^^^.
'///
If Not Exsists Then
    With frmComponentSearchForm
        .MdiParent = MyBase.ParentForm
        frmComponentSearchForm = New frmPartSearch
       .Show()
       Exsists = True '/// prevent more instances of a New Form.
Else
    frmComponentSearchForm.Show()
End If
 
Thanks

I was really asking how do you set an object, or test for is nothing when the object is in a with statement?
 
here's a quick example of checking if a Form is Nothing or not...
Visual Basic:
    If frmComponentSearchForm Is Nothing Then
        MessageBox.Show("it's nothing!")
        '/// do your stuff here. eg:
    With frmComponentSearchForm
        .MdiParent = MyBase.ParentForm
        frmComponentSearchForm = New frmPartSearch
       .Show()
    Else
        MessageBox.Show("it exsists!")
        '/// just show the Form.
        frmComponentSearchForm.Show()
    End If
not sure if that helps.
 
With frmSearch

'How do I test frmSearch here for nothing?
'If I can at all????

'If I do this within a with statement
frmSearch = new frmSearch
.MDIParent = Me.ParentForm

'The Code falls over on the line above,
'statingobject not set to an instance.

End With

Does a with statement, only allow you access tot he objects properties and methods?
Are you allowed to modify the actual object (set it to a new, check for nothing)?
 
Back
Top