?
Solved

‘<form name> is a type and cannot be used as an expression’

Posted on 2010-11-23
10
Medium Priority
?
830 Views
Last Modified: 2012-05-10
Most of the forms in my project start with:

    Public Sub New()
        InitializeComponent()
‘       <do stuff>
    End Sub

There are a couple forms that I am passing a parameter to:

    Public Sub New(ByVal str_Form As String)
        InitializeComponent()
‘       <do stuff>
    End Sub

I’m processing each form in my project with the following statement:

    Default_Window_Locations(frm_Name1)

The statement works fine for forms where I am not passing a parameter, but I get the message ‘<form name> is a type and cannot be used as an expression’ for those forms that I am passing a parameter to.  I’ve tried adding dummy parameters of these forms without any success.

Any suggestions will be greatly appreciated, either by changing how I’m passing parameters or calling Default_Window_Locations.

Thank you in advance for your help!

0
Comment
Question by:CIW_George
10 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34198864
Since this is taking a STRING param
    Public Sub New(ByVal str_Form As String)
Should you be calling
    Default_Window_Locations('frm_Name1')
?
0
 

Author Comment

by:CIW_George
ID: 34198973
Sorry I didn't include the following:

    Private Sub Default_Window_Locations(ByVal frm_Name As Form)

        '   Close frm_Name if open and delete registry '_Size' value.

        If _Forms.Count_Open_Forms(frm_Name.Name) = 1 Then
            frm_Name.Close()
        End If

        _Registry_HCU.Reg_HCU_Key_Delete(const_str_CIW_Registry, frm_Name.Name & "_Size")

    End Sub

Therefore, Default_Window_Locations requires a form reference, while the parameter being passed to New() is a string.  I don't think whether New() requires a string or something else is the issue, it's the fact that New() requires a parameter that that's what is triggering the error.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 34199478
To get all the open forms, use
Application.OpenForms

like this:

            Dim frm As Form
            For Each frm In Application.OpenForms
                   frm.Close()
            Next
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34199694
On which line do you get the error?
0
 

Author Comment

by:CIW_George
ID: 34200634
CodeCruiser -

Thanks for jumping in - this thread seems to be getting off course...

Given three lines:

    Default_Window_Locations(frm_Name1)
    Default_Window_Locations(frm_Name2)
    Default_Window_Locations(frm_Name3)

If frm_Name2 is the form where I am passing a parameter, line 2 of the above example is where I'm getting the error.  frm_Name1 and frm_Name3 all start with Public Sub New(); frm_Name2 starts with  Public Sub New(ByVal str_Form As String).

The error is generated while working with the code, not at runtime.

The subroutine Default_Window_Locations is expecting a form as it's parameter, not a string reference.

In my project, I don't want to close all the open forms; Default_Window_Locations applies to only specific forms.

Not sure how else I can explain the situation, so please ask whatever questions you'd like.

Thanks for your thoughts!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34203441
Add a default constructor to the frm_Name2 (the Public Sub New() constructor).
0
 

Author Comment

by:CIW_George
ID: 34205781
So, for frm_Name2, I'll have two New():

    Public Sub New(ByVal str_Form As String)
       InitializeComponent()
    End Sub

    Public Sub New(ByVal str_Form As String)
       InitializeComponent()
‘       <do stuff>
    End Sub

Or am I missing something?

(Private Sub New should have been Public Sub New in my original example - my typo...)
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 34205805
You will have


    Public Sub New()
       InitializeComponent()
    End Sub

    Public Sub New(ByVal str_Form As String)
       InitializeComponent()
‘       <do stuff>
    End Sub
0
 

Author Closing Comment

by:CIW_George
ID: 34205850
It's amazing how simple some solutions can be!

As always, I can count on your solutions to be clean and functional.

Thank you CodeCruiser!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34205902
Glad to help :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question