Solved

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

Posted on 2010-11-23
10
802 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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