Solved

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

Posted on 2010-11-23
10
799 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now