This question already has answers here:
Binding to static property
(12 answers)
INotifyPropertyChanged for static variable [duplicate]
(2 answers)
Notify binding for static properties in static classes
(1 answer)
Closed 8 days ago.
Is the StaticPropertyChanged event simply a convention (regarding names) or what mechanism does WPF use to recognize this event?
The eventhandler raising static property change notification must have one of two possible signatures.
Note that it must be static itself, so it cannot just be a regular propertychanged eventhandler
This is explained here:
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/getting-started/whats-new?redirectedfrom=MSDN&view=netframeworkdesktop-4.8#static_properties
Binding to static properties
You can use static properties as the source of a data binding. The data binding engine recognizes when the
property's value changes if a static event is raised. For example, if
the class SomeClass defines a static property called MyProperty,
SomeClass can define a static event that is raised when the value of
MyProperty changes. The static event can use either of the following
signatures.
public static event EventHandler MyPropertyChanged;
public static event EventHandler
StaticPropertyChanged;
Note that in the first case, the class exposes a static event named
PropertyNameChanged that passes EventArgs to the event handler. In the
second case, the class exposes a static event named
StaticPropertyChanged that passes PropertyChangedEventArgs to the
event handler. A class that implements the static property can choose
to raise property-change notifications using either method.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am new to C# and I found this documentation and example about events in C#:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines
For me especially these lines are of interest:
public void DoSomething()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Event triggered"));
}
// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> raiseEvent = RaiseCustomEvent;
// Event will be null if there are no subscribers
if (raiseEvent != null)
{
// Format the string to send inside the CustomEventArgs parameter
e.Message += $" at {DateTime.Now}";
// Call to raise the event.
raiseEvent(this, e);
}
}
For me this naming does not make sense at all or I did not understand how the events work in C#. If I am not wrong then in DoSomething the CustomEvent is triggered. But normally onAnything functions are listening on events. Do you also think that OnRaiseCustomEvent should be named RaiseCustomEvent?
That is the c# naming convention.
You don't have to agree with it, but you should be familiar with it and I would also recommend using it in your code.
An experienced c# programmer that will see a method called On<blabla> that take in an argument named EventArgs or anything that ends with the suffix EventArgs (like <blabla>EventArgs) will immediately expect this method to raise an event called <blabla>.
(Naturally, <blabla> is just a placeholder for an actual name)
This pattern exists because while events are being inherited, the only way to raise them is within the declaring type - as documented in How to raise base class events in derived classes (C# Programming Guide):
... events are a special type of delegate that can only be invoked from within the class that declared them. Derived classes cannot directly invoke events that are declared within the base class.
This is why you need the On<blabla>(BlaBlaEventArgs e) method to be protected virtual (unless your class is declared as sealed) - so that it's derived classes will be able to raise the <blabla> event as well.
Official documentation also admits that (at least some of) the names chosen in this pattern are somewhat misleading - as mentioned here:
The name EventHandler can lead to a bit of confusion as it doesn't actually handle the event.
Personally, I think they might have chosen better names for this as well as for the On<blabla> methods, but it's way, way too late to change it now.
This question already has answers here:
Is it possible to implement Property Changed as Attribute?
(2 answers)
Override Getter/Setter with Custom Attribute
(2 answers)
Override property setter and getter
(2 answers)
C# WPF How to set Property setter method dynamically?
(3 answers)
Closed 5 years ago.
I'm trying to create an OleTableAttribute that I will apply to a class MyTable alongside a System.Data.Linq.Mapping.TableAttribute so that it would look something like this:
[Table(Name="MyTable")]
[OleTable]
public class MyTable
{
[Column(IsPrimaryKey = true)]
public int pk_id { get; set; }
/*...*/
}
What I'd like the OleTableAttribute to do is look for any [Column] attributed properties in the class that it is marked on, and reflectively add some type of listener to the setter method if it has one. The focus of this question is really how to do the reflective monitoring-setup of setter calls; just a simple "hey-its-been-called" signal is all I need. Is this possible?
PostSharp works. Using the [NotifyPropertyChanged] aspect will decorate all your properties behind the scenes with just the type of signalling I'm looking for here. Then the [OleTable] can treat it's attributed class as INotifyPropertyChanged and add its own PropertyChanged event.
This question already has answers here:
What is the difference between a field and a property?
(33 answers)
Closed 8 years ago.
Suppose I have a property in a class as in the following:
class testclass
{
public string Name {get; set;}
public void dosomething(){//...}
}
There is no functional difference between this format and the following:
class testclass
{
public string name;
public void dosomething(){//...}
}
Both name fields can be set to anything including an empty string and both can retrieve just without any restrictions. So what is the use of the property semantics detailed above where there is no validation or other process in the get and set methods? One use I see is that you can remove either the get or set method to make it write only or read only, respectively. I don't know what other use this would serve.
The main reason is so that you can change the implementation later without breaking client code. You might not do any validation or raise an event now but what if you decide to in the future? Also, properties can be bound while fields can't.
This question already has answers here:
Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class?
(3 answers)
Closed 4 years ago.
If I have ObjectA, and it has a private method GetPrice() and also has a "parent" field of the same type, why am I able to call GetPrice() on the parent instance from within the child instance?
Example:
private decimal GetPrice()
{
ObjectA parent = Parent;
if(parent != null)
{
return parent.GetPrice(); // Why is this OK?
}
return 0;
}
Because private means "not accessible to other types", not "not accessible to other instances".
Because private scope is limited to the class, not the instance as defined in the C# spec:
1.6.2 Accessibility
Each member of a class has an associated accessibility, which controls
the regions of program text that are able to access the member. There
are five possible forms of accessibility. These are summarized in the
following table.
Accessibility Meaning
public Access not limited
protected Access limited to this class or classes derived from this class
internal Access limited to this program
protected internal Access limited to this program or classes derived from this class
private Access limited to this class
An access modifier is related to it's implementing class/type not to instances of that class
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the difference between encapsulating a private member as a property and defining a property without a private member?
In C#, usually when I define a property I declare and implement a single line or more for get and set. e.g.
public bool IsThere
{
get { return _isThere; }
set { _isThere = value;}
}
now what does this mean?
public bool IsThere
{
get;
set;
}
Those are auto-properties. They work the same way as your first example, but allow you to omit the unnecessary source code.
They're best used when there is no longer to your getter/setter methods.
They also allow you to add logic to your getter/setter methods later without breaking any calling code (even though you'll also have to implement the private backing property yourself).
It's an Auto-Implemented Property (automatic property).
The C# compiler will automatically create a private field member for the get/set methods to read/write from.
Note that there are limitations to automatic properties (for now). For example, you cannot use modifiers such as readonly, though you can still mark it as private set it isn't quite the same.