Solved

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

Posted on 2010-11-23
10
805 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 
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

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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