Results 1 to 11 of 11

Thread: [RESOLVED] Passing a shared instance to Form1 sub new.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    546

    Resolved [RESOLVED] Passing a shared instance to Form1 sub new.

    Error

    Code:
    Error	1	'Form1' is a type in 'WindowsApplication1' and cannot be used as an expression.
    I am doing some work in applicationevents that passes a class to form1 sub new. I am sure it is obvious why it wont allow this to you guys.

    Form1

    Code:
    Public Class Form1
    
        Private Shared mSomeInstance As SomeClass
    
        Public Sub New(SomeClass As SomeClass)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            mSomeInstance = SomeClass
        End Sub
    
    End Class
    applicationevents

    Code:
    Namespace My
    
        Partial Friend Class MyApplication
    
            Private Shared mSomeClass As New SomeClass
            Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
                ' do some work to add values to some class
            End Sub
        End Class
    
    
    End Namespace

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Passing a shared instance to Form1 sub new.

    What are you trying to achieve by passing the class in the constructors of Form1?

  3. #3
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Passing a shared instance to Form1 sub new.

    Just make
    Private Shared mSomeInstance As SomeClass

    Public if you want to access it outside the class scope

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Passing a shared instance to Form1 sub new.

    It's a curious error message, and not what I would expect from any line that you showed. Is the error coming from one of those lines?

    The basic problem has to do with the fact that mSomeInstance is Shared. There is no clear reason why it should be Shared, especially since you are then setting it in the constructor of the class. A Shared member of a class exists without any specific instance of the class existing. Therefore, mSomeInstance exists whether there is 0 instances of Form1, one instance of Form1, or a dozen instances of Form1. The fact that it is a form doesn't make any difference at all.

    By assigning to that shared member in the constructor, you end up with a situation where every new instance of Form1 will overwrite whatever is in mSomeInstance with whatever new variable is passed to the constructor. While I'm surprised that you are getting that particular error, I'm not surprised that there is something wrong with this, because there is almost no scenario where this is a good idea. The only use for such a construct that comes readily to mind is a reference counter, which mSomeInstance does not appear to be.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    546

    Re: Passing a shared instance to Form1 sub new.

    The type makes no difference ..

    vb Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New(ex As String)
    4.  
    5.         ' This call is required by the designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.  
    10.     End Sub
    11.  
    12.  
    13. End Class

    will produce the same error code. The line highlighted is in the designer

    vb Code:
    1. <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
    2.         Protected Overrides Sub OnCreateMainForm()
    3.             Me.MainForm = Global.WindowsApplication1.Form1
    4.         End Sub

    Me.MainForm = Global.WindowsApplication1.Form1

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,957

    Re: Passing a shared instance to Form1 sub new.

    Is Form1 your startup form?

    If so that's your problem. You effectively removed the default parameterless constructor. So the compiler says, I can't create Form1 because I don't have a copy of SomeClass (shared or not) to pass to the constructor of the form. So it's essentially throwing its hands up in the air and telling you that it doesn't know what you expect it to do.

    Code:
    Public Class Form1
     
        Public Sub New()
     
            ' This call is required by the designer.
            InitializeComponent()
     
            ' Add any initialization after the InitializeComponent() call.
     
        End Sub
     
        Public Sub New(ex As String)
     
            ' This call is required by the designer.
            InitializeComponent()
     
            ' Add any initialization after the InitializeComponent() call.
     
        End Sub
      
    End Class
    Adding the default parameterless constructor should solve your problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    546

    Re: Passing a shared instance to Form1 sub new.

    Ok that makes sense. But now which is also obvious where can i pass the applicationevents instance to the startup form? Normally we would do this when a new form is created but in this case it is automatically created for us.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,957

    Re: Passing a shared instance to Form1 sub new.

    Why do you even need to pass it in in the first place? It's a shared class. It's part of the MyApplication namespace, which should be global to the applicaiton.... for you shouldn't even need it in the form other than to use it directly: MyApplication.mSomeClass.SomeProperty = "Foo"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    546

    Re: Passing a shared instance to Form1 sub new.

    Ok maybe i am going about this all wrong. Let me explain. My application takes a text file that is listed with arguments. I assign this to a class during application.events. I assumed using application events is the best place to accept the commandline arguments at start up? I made this shared as multiple classes use this class later on. I was lead to believe if a class is used in more then one place it should be shared rather then creating multiple instances passed between classes. I dind t think using a moduel was best practice?

    I am open to ideas?

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,957

    Re: Passing a shared instance to Form1 sub new.

    Right... so you create a Shared variable under MyApplication ... right... good....
    so presumably you want to use in form1, right? Ok, great... just use it: MyApplication.mSomeClass ... there's no need to pass it to the form... MyApplication IS your application, so it's got complete scope over everything else. Anything declared inside is available. I'm not talking about a module (although that would work too), but allowing it to be scoped at the application level. You shouldn't need to pass it. Kind of defeats the purpose of a shared class.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Passing a shared instance to Form1 sub new.

    There's no problem with using a module (or a 'shared' class).

    Your main form has access to the command line arguments, anyway: My.Application.CommandLineArgs as a read only collection of strings. You could use that, and not worry about the application events, if you want. Reading it application events, of course, gives you the option of closing the application before showing the form.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width