Solved

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

Posted on 2010-11-23
10
808 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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