Raise a StaticPropertyChanged event in VB

p-plater used Ask the Experts™

I need to raise a StaticPropertyChanged event in VB.
I can only find C# examples, Could someone help me to convert it to VB?

C# Code
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static void RaiseStaticPropertyChanged(string propName)
    EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
    if (handler != null)
        handler(null, new PropertyChangedEventArgs(propName));

Open in new window

My VB Attempt.
(I can't work out what to put in the Shared Sub.
Public Shared Event StaticPropertyChanged As PropertyChangedEventHandler

    Public Shared Sub OnStaticPropertyChanged(propertyName As String)

    End Sub

Open in new window

Many Thanks
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

RaiseEvent StaticPropertyChanged(propertyName)


Thanks James

That gives me an error "Argument not specified for parameter 'e' of 'Public Shared Event StaticPropertyChanged As PropertyChangedEventHandler"


I Tried

RaiseEvent StaticPropertyChanged(Nothing, New PropertyChangedEventArgs(propertyName))

Open in new window

That doesn't give any error but doesn't cause the UI to update either.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Top Expert 2015
Sorry, I was "complètement dans les patates" as we say here. I don't know where my head was. My answer was anything. Your last definition is OK. For a standard event, the first parameter is usually Me (the equivalent of this in C#), but because a static event can be triggered by any existing instance, Nothing is the logic thing to pass there

What do you mean by "doesn't cause the UI to update". The role of an event is not to "do" something, it's simply to inform the application that uses the object that something happened. It's up to the application to perform any required operation. Triggering the event does nothing else than calling the handler method(s) that have been associated with it.

Do you mean that the event is not triggered in the application?

Then it might be that you do not call OnStaticPropertyChanged when it is required, or it might be that the  way you are trapping the event that is not good.

Here is code that I just tested, and it works. See if yours follows the same pattern.

The class that declares and triggers the event:
Public Class Class1

	Public Shared Event MyEvent As EventHandler

	Public Shared Sub Test()
	End Sub

	Public Shared Sub OnMyEvent(propertyName As String)
		RaiseEvent MyEvent(Nothing, New EventArgs)
	End Sub

End Class

Open in new window

The code that tests it. I tried 2 ways to set the handler and called the method that is supposed to trigger the event 2 different ways:
	Public Sub MyEventsHandler(sender As Object, e As EventArgs)
		Console.WriteLine("Got you")
	End Sub

	Public Sub TestEvent()

		Dim x As New Class1

		AddHandler x.MyEvent, AddressOf MyEventsHandler
		AddHandler Class1.MyEvent, AddressOf MyEventsHandler

		Console.WriteLine("Triggered by instance")
		x.Test()      'Triggers the event
		Console.WriteLine("Triggered by class")
		Class1.Test() 'Also triggers the event, which is to be expected from a static event

	End Sub

Open in new window

And the result:
Triggered by instance
Got you
Got you
Triggered by class
Got you
Got you

Open in new window

As you can see, it works no matter if you set the handler on an instance or the class, and no matter if you call the method that triggers it through the instance or the class. This is the way a Shared event should behave. All instances see it, but you can use it directly on the class.


Hello James

What do you mean by "doesn't cause the UI to update".

I have a UI Textbox data bound to the Shared Property, But when I change the Property in Code the Textbox doesn't update.
From what I understand the StaticPropertyChanged should solve this problem.
(I'm using WPF)
Top Expert 2015

You did not say that you were in WPF, and this is not my bag. I just showed you how to raise a standard event in a class library, because the kind of code that you presented in your question (declaring an event and triggering it in a OnEventName method) is usually used as ways to trigger events in your own classes. And there is a PropertyChanged event that can be triggered with similar code, although different, mine was simplified with a method instead of a property, to show that the concept works.

We do not have the StaticPropertyChanged in my environment, and I do not know about properties binding in WPF, so I cannot do more.

I am just puzzled that you would try to use it instead of PropertyChanged. Static (Shared) elements are not object specific, they are shared with all the objects on a given class. Change the property on the class or on any of the objects, and it changes on all the objects. And the event that would be triggered on a Shared property can be pretty useless on a TextBox because you would know which property has changed, but not on which TextBox.

And looking at the documentation for the WPF TextBox, I do not see any Shared property.

I suggest that you post another question, not asking how to correct your code (in my view it is useless for what you seem to want to do), but describing what you want to do and asking how to achieve it.


You answered my Question - I'll ask another one.

What I am doing is I have a Static Class with Static Properties for the Current user.
I am now trying to bind a Texbox on each window to this Static Property so when the User Changes all the Windows Update automatically.
I don't use Property Changed as it doesn't work with Static Properties (From what I read thats why they introduced StaticPropertyChanged.)
Top Expert 2015

As I told you, I do not work with WPF.

In Windows Forms, when implemented through the IPropertyChanged interface, the PropertyChanged event works with both static and instance properties. Form what I understand from the documentation, and from what you say in your last post, this is not the case in WPF. Because of my lack of experience with the latter, which seems to need to be used in a special way, there is nothing more I can tell you, except maybe the following.

Because StaticPropertyChanged seems to be something special, maybe it does not work with data binding. Have you tried to catch the event yourself through an event method? This is not much code, and unless you have a big lot of windows, this is not too much work to copy and paste from one to another.

And since its basically only one property that changes, why go through a generic PropertyChanged event instead of creating a brand new specific event such as CurrentUserChanged?

Also, I am curious. What can bring the current user to change while an application is running? If there is a need to identify a user, this is usually done when starting the application, not in the middle of its use.

And what do you display in that TextBox? The username? Doesn't the user knows it own name?

Can you be more specific about what you want to do?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial