Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:01:15+00:00 2026-06-15T02:01:15+00:00

I’m not really sure how to ask this question, but basically I want to

  • 0

I’m not really sure how to ask this question, but basically I want to know how to specify a class type generically such that I can type and paste code around.

For example I have the following code:

Public Class CustInfo
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub NotifyPropertyChanged(ByVal Name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name))
    End Sub

    Protected Sub NotifyPropertyChanged()
        Dim myStackFrame As New StackFrame(1)
        Dim myChangingPropertyName As String = myStackFrame.GetMethod.Name.Split("_")(1)
        NotifyPropertyChanged(myChangingPropertyName)
    End Sub

    Protected Sub NotifyPropertyChanged(Of TResult)(propertyExpr As Expression(Of Func(Of CustInfo, TResult)))
        NotifyPropertyChanged(Me.GetPropertySymbol(propertyExpr))
    End Sub

The idea is to have the last Sub be generic, that is to say, not specifically have to specify CustInfo in it’s definition. This way I can literally copy this code to any other class and it will work unmodified.

I’ve tried creating a property that returns the type of the class and using that, which predictably failed. I tried using TypeOf, Me, even ‘this’ on a long shot and nothing worked. It seems there has to be a way for the compiler to know this based on this error message which I get when I change the last sub to:

Protected Sub NotifyPropertyChanged(Of TResult)(propertyExpr As Expression(Of Func(Of TResult)))
    NotifyPropertyChanged(Me.GetPropertySymbol(propertyExpr))
End Sub

Data type(s) of the type parameter(s) in extension method 'Public Function GetPropertySymbol(Of TResult)(expr As System.Linq.Expressions.Expression(Of System.Func(Of CustInfo, TResult))) As String' defined in 'CustClasses.Extensions' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

The extension method being referred to is:

<Extension()>
Public Function GetPropertySymbol(Of T, TResult)(obj As T, expr As Expression(Of Func(Of T, TResult))) As String
    Return DirectCast(expr.Body, MemberExpression).Member.Name
End Function

So, as you can see the compiler correctly inferred what is going on, but still couldn’t work with it. The only type that seems unknown is TResult, but that type should be easily inferred based on the other information in the extension method that is known.

It may be impossible to generically specify a class type in the procedure definition, in which case I’ll modify the code every time I use it, but I’d rather not and learn something new in the process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:01:16+00:00Added an answer on June 15, 2026 at 2:01 am

    Based on what @Mark Hurd answered, I’ve come up with something that makes working with properties much easier. I decided to create a class, decorated with MustInherit that Implements INotifyPropertyChanged and includes all the NotifyPropertyChanged code that is needed all set up with generics for maximum reuse and portability. I would love to hear some feedback on this solution. I don’t see any issues with it, but I could easily be missing something.

    Public MustInherit Class AutomaticNotfiyPropertyChanged
        Implements INotifyPropertyChanged
    
        Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    
        Protected Sub NotifyPropertyChanged(ByVal Name As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name))
        End Sub
    
        Protected Sub NotifyPropertyChanged()
            Dim myStackFrame As New StackFrame(1)
            Dim myChangingPropertyName As String = myStackFrame.GetMethod.Name.Split("_")(1)
            NotifyPropertyChanged(myChangingPropertyName)
        End Sub
    
        Protected Shared Sub NotifyPropertyChanged(This As AutomaticNotfiyPropertyChanged, Name As String)
            This.NotifyPropertyChanged(Name)
        End Sub
    
        Protected Overridable Sub NotifyPropertyChanged(Of TResult)(propertyExpr As Expression(Of Func(Of AutomaticNotfiyPropertyChanged, TResult)))
            NotifyPropertyChanged(GetPropertySymbol(propertyExpr))
        End Sub
    
        Protected Shared Sub NotifyPropertyChanged(Of T, TResult)(This As T, propertyExpr As Expression(Of Func(Of T, TResult)))
            Dim myParent As AutomaticNotfiyPropertyChanged = TryCast(This, AutomaticNotfiyPropertyChanged)
    
            If myParent IsNot Nothing Then
                AutomaticNotfiyPropertyChanged.NotifyPropertyChanged(DirectCast(myParent, AutomaticNotfiyPropertyChanged), This.GetPropertySymbol(propertyExpr))
            End If
        End Sub
    
    End Class
    

    Then calls to notify of a change in other properties would be NotifyPropertyChanged(Me, Function(x) x.myChangingProperty) or if you want to notify that the current property is changing all that is needed is NotifyPropertyChanged(). Additionally NotifyPropertyChanged(Me, "myChaningProperty") and NotifyPropertyChanged("myChangingProperty") will also work. Finally if NotifyPropertyChanged(Of TResult) is overridden to include the derived class, then a call like NotifyPropertyChanged(Function(x) x.myChaningProperty) will work too.

    Also, I’ve found that NotifyPropertyChanged(Me, Function(x) x.myChaningProperty) will work just fine, and will be checked by the compiler. However, IntelliSense doesn’t work for x. I find that strange since the compiler clearly knows what x is because it will even correct capitalization of variables. It isn’t a deal-breaker, but overriding NotifyPropertyChanged(Of TResult) to reflect the current class will make IntelliSense work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.