Results 1 to 5 of 5

Thread: [RESOLVED] Events for Auto-Implemented Properties

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Resolved [RESOLVED] Events for Auto-Implemented Properties

    I'm wanting to be lazy... Can I raise events for Auto-Implemented properties? For example typically I'll do this:
    Code:
    Private mFoo As String
    Public Property Foo() As String
        Get
            Return mFoo
        End Get
        Set(ByVal value As String)
            If mFoo <> value Then
                mFoo = value
    
                RaiseEvent FooChanged(Me, EventArgs.Empty)
            End If
        End Set
    End Property
    
    Public Event FooChanged(ByVal sender As Object, ByVal e As EventArgs)
    However... I'm wanting to have the auto-implemented property:
    Code:
    Public Property Foo() As String
    And still raise FooChanged when it changes. Am I able to have my cake and eat it too?

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

    Re: Events for Auto-Implemented Properties

    How would it know what event to raise and when? The Auto implemented property is for when you have a 'minimalist' property: repeating the get/set is unnecessary.

    Further, you wouldn't normally raise the event directly, you would call an overridable method which raises the event (following the standard, event raising, pattern).

    So, yeah, I think you are wanting cake and eat it, too.
    "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."

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Events for Auto-Implemented Properties

    If you use that particular code pattern a lot, then write an IntelliSense Code Snippet. You could easily modify the Property snippet to insert your pattern.

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: Events for Auto-Implemented Properties

    Quote Originally Posted by TnTinMN View Post
    If you use that particular code pattern a lot, then write an IntelliSense Code Snippet. You could easily modify the Property snippet to insert your pattern.
    Oh thanks. I didn't know about IntelliSense code snippets, although that was kind of what I actually had in mind. I'll learn how to create the XML and probably post in the in codebank.

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Events for Auto-Implemented Properties

    Quote Originally Posted by dday9 View Post
    Oh thanks. I didn't know about IntelliSense code snippets, although that was kind of what I actually had in mind. I'll learn how to create the XML and probably post in the in codebank.
    You can do that. However, you said you want to be lazy, so I would suggest you download Snippet Editor. It really makes it easy to write a snippet.

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