Avatar of Ron Kidd
Ron KiddFlag for Australia

asked on 

Raise a StaticPropertyChanged event in VB

Hello

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
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Jacques Bourgeois (James Burger)
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

RaiseEvent StaticPropertyChanged(propertyName)
Avatar of Ron Kidd
Ron Kidd
Flag of Australia image

ASKER

Thanks James

That gives me an error "Argument not specified for parameter 'e' of 'Public Shared Event StaticPropertyChanged As PropertyChangedEventHandler"
Avatar of Ron Kidd
Ron Kidd
Flag of Australia image

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ron Kidd
Ron Kidd
Flag of Australia image

ASKER

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)
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.
Avatar of Ron Kidd
Ron Kidd
Flag of Australia image

ASKER

Thanks
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.)
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?
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo