locked
Event Variable scope across whole project. RRS feed

  • Question

  • User-850127045 posted

    I'm moving from VB6 to VB.net and there are a few things confusing.  In vb.net, your program just automatically has a form, now the form in in a class and so it may or may not be there.  In vb6, the main subroutine controls program flow and I don't see that in vb.net.  However, my biggest problem is that I have a com dll that has a connect class with events.  I need to be able to access these variable outside of that class.  I don't understand how to do that.  I can't seem to declare

    public withevents

    outside of the class and in the namespace so it can be shared with other code.  Its say "not valid" in Namespace.

    So what is the best way to do that.  Do I create a set/get property for the class that holds this value?  And, if I do that, is the connect class automatically instantiated as a part of the COM dll?  Right now I don't instantiate the connect class, I assume the application does.  But I'm not sure exactly how the property of the class would be set...perhaps I would need to call the property set from the onconnection method.

    Anybody know what the answer to these two questions are and this 3rd one?

    How can I have a variable that has "events" have scope across the entire project?

    Tuesday, June 7, 2011 4:37 PM

Answers

  • User-1696077569 posted

    Remove shared from method name

    Public Sub InsertProduct() 
     
            '''''''''''''''''''''''''''''''''''' 
            'Handles Insert Product Link Click Event from Menu in form 
            '''''''''''''''''''''''''''''''''''' 
            '
    reset edit flag as this is an insert 
            EditFlag = False 
            'Make sure the user is not trying to Insert a Product Link within an existing Product Link 
            Dim chkElement As IHTMLElement 
            chkElement = fpapp.ActiveDocument.activeElement 
            Dim chkValue As Intege

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 11:01 PM

All replies

  • User-1696077569 posted

    Create a class and declare events as shared

    Public Class GlobalEvents
        Public Shared Event SpecialEventRaised(ByVal sender As Object, _
               ByVal type As String, ByVal msg As String)
    
        Public Shared Sub SpecialEvent(ByVal sender As Object, _
               ByVal type As String, ByVal msg As String)
            RaiseEvent SpecialEventRaised(sender, type, msg)
        End Sub
    End Class
    Wednesday, June 8, 2011 12:29 AM
  • User-850127045 posted

    Ok, my question is not that complex.  I don't believe I have to "preserve" events in the 2nd class.

    I tried to simply overload the New() constructor for the windows.form and pass the object into the form constructor.  That actually worked fine in my test form.  However, the form I'm migrating is much more complex...and it doesn't seem to work.  The difference is the code in my test was all put into the designer.  However in my real world case, all of the code is in the .vb file.  I really don't understand why all this stuff is segmented and not shared.... 

    I have this overloaded constructor code in the designer.vb file to grab this variable once the form is instantiated.

    Partial Class frmProdLinkExp
        Public fpapp As Microsoft.Expression.Web.Interop.Application
    
        Public Sub New(ByVal WebApp As Microsoft.Expression.Web.Interop.Application)
            MyBase.New()
            fpapp = WebApp
    ....
     
    In my regular vb code for the form, I have this:
    Partial Friend Class frmProdLinkExp
        Inherits System.Windows.Forms.Form
        Public Shared Sub InsertProduct()
    
            ''''''''''''''''''''''''''''''''''''
            'Handles Insert Product Link Click Event from Menu in form
            ''''''''''''''''''''''''''''''''''''
            'reset edit flag as this is an insert
            EditFlag = False
            'Make sure the user is not trying to Insert a Product Link within an existing Product Link
            Dim chkElement As IHTMLElement
            chkElement = fpapp.ActiveDocument.activeElement
            Dim chkValue As Intege
    at fpapp.ActiveDocument I get the error:
    Error 2 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
     
    Wednesday, June 8, 2011 7:12 PM
  • User-1696077569 posted

    Remove shared from method name

    Public Sub InsertProduct() 
     
            '''''''''''''''''''''''''''''''''''' 
            'Handles Insert Product Link Click Event from Menu in form 
            '''''''''''''''''''''''''''''''''''' 
            '
    reset edit flag as this is an insert 
            EditFlag = False 
            'Make sure the user is not trying to Insert a Product Link within an existing Product Link 
            Dim chkElement As IHTMLElement 
            chkElement = fpapp.ActiveDocument.activeElement 
            Dim chkValue As Intege

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2011 11:01 PM