Shared (static) classes with events in C# - c#

Here is an example of what I would do in Visual Basic:
Public Class Class1
Public Shared WithEvents Something As New EventClass
Public Shared Sub DoStuff() Handles Something.Test
End Sub
End Class
Public Class EventClass
Public Event Test()
End Class
How do I do this in C#?
I know there is not a Handles clause in C# so I need some function that is called and assign the event handlers there. However, since it's a shared class, there is no constructor; I must put it somewhere outside of a function.
How can it be achieved?

You can use the static constructor...
static readonly EventClass _something;
static Class1()
{
_something = new EventClass();
_something.Test += DoStuff;
}
static void DoStuff()
{
}

Try the following
public static class Class1 {
private static EventClass something;
public static EventClass Something {
get { return something; }
}
static Class1 {
something = new Class1();
something.Test += DoStuff;
}
public static void DoStuff() {
...
}
}

Related

Subscribe to event handler inside Singleton class

I am trying to subscribe to an event inside Singleton class.
public class Singleton : IDisposable
{
private static readonly Singleton _instance = new Singleton();
public static Singleton Instance => _instance;
static Singleton()
{
AppSettings.Instance.OnUpdated += OnAppSettingsUpdated;
OnAppSettingsUpdated(null, null);
}
public void Dispose()
{
AppSettings.Instance.OnUpdated -= OnAppSettingsUpdated;
}
private static void OnAppSettingsUpdated(object sender, EventArgs e)
{
// do something
}
}
AppSettings is another singleton class.
public partial class AppSettings
{
public EventHandler OnUpdated;
}
When OnUpdated invoked, nothing happens. It looks like OnAppSettingsUpdated is not subscribed.
In a code I use Singleton like this.
Singleton instance = Singleton.Instance;
Maybe I missed something?
It is important to subscribe inside Singleton class.
I suspect that the issue is that you have never accessed the Instance property of your Singleton class. Until you actually use the class, none of the code within it will be executed, including static members. In order for your static field to be assigned and your static constructor to be run, you need to actually use the class somehow. Try assigning its Instance property to a variable somewhere and I think you'll find that your event handler will be executed when you expect it to be.
EDIT:
When I run this code, I see nothing in the console:
class Program
{
static void Main(string[] args)
{
Singleton2.Instance.RaiseSomethingHappened();
}
}
public class Singleton1
{
private static Singleton1 _instance = new Singleton1();
public static Singleton1 Instance => _instance;
private static void Singleton2_SomethingHappened(object sender, EventArgs e)
{
Console.WriteLine("Singleton2_SomethingHappened");
}
public Singleton1()
{
Singleton2.Instance.SomethingHappened += Singleton2_SomethingHappened;
}
}
public class Singleton2
{
private static Singleton2 _instance = new Singleton2();
public static Singleton2 Instance => _instance;
public event EventHandler SomethingHappened;
protected virtual void OnSomethingHappened()
{
SomethingHappened?.Invoke(this, EventArgs.Empty);
}
public void RaiseSomethingHappened()
{
OnSomethingHappened();
}
}
When I change this:
static void Main(string[] args)
{
Singleton2.Instance.RaiseSomethingHappened();
}
to this:
static void Main(string[] args)
{
var s1 = Singleton1.Instance;
Singleton2.Instance.RaiseSomethingHappened();
}
and run the code again, I see the expected output in the console window. Either you're not actually using the single instance of the class handling the event or you're not registering the event handler correctly. This code demonstrates that the former makes a difference and how to do the latter.

How to create an Observable "channel" in Rx?

I started to transform my push -> pull bridge to a much simpler construct with Reactive Extensions.
So now I have a class with a (private) event, and an Observable created from it.
class WithEvents {
public class MyEvent {}
private delegate void MyEventHandler(MyEvent e);
private event MyEventHandler EventRaised;
Public IObservable<MyEvent> TheEvents;
public void Foo() {
EventRaised(new MyEvent());
}
}
Thing is, this event seems like unneeded scaffolding here. So I was wondering: is there a way to construct a 'bare' Observable, that I can just 'push' events to?
class WithChannel {
public class MyEvent {}
public IObservable<MyEvent> EventRaised {get} = new Channel<MyEvent>();
public void Foo() {
((Channel)EventRaised).DoNext(new MyEvent());
}
}
Yes, there is a thing called Subject (in System.Reactive.Subjects namespace) which does exactly that:
class WithChannel {
public class MyEvent {
}
private readonly Subject<MyEvent> _event;
public WithChannel() {
_event = new Subject<MyEvent>();
}
public IObservable<MyEvent> EventRaised => _event;
public void Foo() {
_event.OnNext(new MyEvent());
}
}
Usage of subjects is generally not recommended, but for this specific task I think it's fine.

Access static object from other instance

I have a Program class which has:
private static ClientBase objClientBase = new ClientBase(new List<RecordType> { RecordType.none }, ModuleType.Monitor);
static void Main(string[] args)
{
objClientBase.Connect(); //IRRELEVANT
objQueueMon = new Main(); //<-INSIDE THIS IS WHERE I WANT TO ACCESS objClientBase
objClientBase.MainModuleThreadManualResetEvent.WaitOne(); //IRRELEVANT
}
This Progam creates a Main class instance as you see:
objQueueMon = new Main();
Notice that they are separated in different files, but the Main class instance is created inside the Program class.
Inside my Program class I want to access that objClientBase.
Do I have to create a constructor method and pass it or make a public access to it?
So what I want to achieve is, inside the Main class, do a objClientBase.FUNCTION
You can do exactly what you just said:
public class Main {
private ClientBase _caller;
public Main (ClientBase caller) {
_caller = caller;
}
}
Or, you can set it later
public class Main {
private ClientBase _caller;
public Main () {
}
// only your assembly sets it
internal SetClientBase(ClientBase cb) {
_caller = cb;
}
// but anyone gets it
// Now you can let some client execute "Function"
public ClientBase Caller {
{return _caller;}
}
}
Just an example
Change the constructor of your Main class to accept a ClientBase object, like this:
public class Main
{
private ClientBase _clientBase;
public Main(ClientBase clientBase)
{
_clientBase = clientBase;
}
public void SomeMethod()
{
// Use ClientBase.FUNCTION here
_clientBase.FUNCTION();
}
}

How to initialize a static property before initializing a static class?

Below Class2 has a property that needs to be set before GetSomething is called, however because I access Class2 at the top of Class1 the property is always null when it gets to Something class. I can't seem to figure out how to change my code to set the property before it's used. Anyone?
EDIT
I want to pass the dependency from form1's constructor, not hardcode it further up the chain.
public partial class form1
{
private static readonly ISomeConstructedClass someConstructedClass = Class1.SomeConstructedClass;
public form1()
{
someConstructedClass.SomeDependency = new SomeDependency();
someConstructedClass.Whatever();
}
}
public static class Class1
{
public static readonly ISomething something = (ISomething)Class2.GetSomething("something");
public static ISomeConstructedClass SomeConstructedClass
{
get
{
return something.SomeConstructedClass;
}
}
}
....
}
public class Class2
{
public static ISomeDependency SomeDependency
{
get;
set;
}
public static GetSomething(string something)
{
switch(something)
{
case "something":
return new Something( SomeDependency );
}
}
}
public class Something : ISomething
{
public ISomeDependency SomeDependency
{
get;
set;
}
public Something(ISomeDependency someDependency)
{
SomeDependency = someDependency;
}
}
[Re]Edit:
I was confused about what you were trying to do before, you just need to create the dependency first.
public partial class form1
{
private static /*readonly*/ ISomeConstructedClass someConstructedClass;
public form1()
{
Class2.SomeDependency = new SomeDependency();
someConstructedClass = Class1.SomeConstructedClass;
someConstructedClass.Whatever();
}
}
I would also move the creation of something into the property just to make sure it is not initialized too soon (before the form1 constructor is called).
public static class Class1
{
public static ISomething something;
public static ISomeConstructedClass SomeConstructedClass
{
get
{
if (something == null) {
something = (ISomething)Class2.GetSomething("something");
}
return something.SomeConstructedClass;
}
}
}
You can use a static constructor. This is called before any static (or instance for that matter) fields or methods are called/accessed.
Something like:
static Class2() {
SomeDependency = SomeDependencyYouNeed;
}
Why are you using static methods? It looks like you're attempting a sort of Dependency Injection. Either create an instance of Class2 and pass the dependency in the constructor (and don't use static methods), or pass the dependency as a parameter of the GetSomething() method.
public static GetSomething(string something, ISomeDependency dependency).

How can I make one class solely responsible for creating and providing access to another class

This is how I understand I can implement the singleton pattern in C#:
public class ChesneyHawkes{
private static ChesneyHawkes _instance = new ChesneyHawkes();
public ChesneyHawkes Instance {get{return _instance;}}
private ChesneyHawkes()
{
}
}
What if I want to provide a single instance of an object, so that there can only ever be one, make the access to it public, but only allow it to be created or replaced by another singleton.
// The PuppetMaster should be the only class that
// can create the only existing Puppet instance.
public class PuppetMaster{
private static PuppetMaster_instance = new PuppetMaster();
public static PuppetMaster Instance {get{return _instance;}}
// Like a singleton but can be replaced at the whim of PuppetMaster.Instance
public static Puppet PuppetInstance {get {return Puppet;}}
private PuppetMaster()
{
}
public class Puppet{
// Please excuse the pseudo-access-modifier
puppetmasteronly Puppet(){
}
}
}
// To be accessed like so.
PuppetMaster.Puppet puppet = PuppetMaster.Instance.PuppetInstance;
You don't really need more than one singleton for that. Look at this example:
using System;
// interface for the "inner singleton"
interface IPuppet {
void DoSomething();
}
class MasterOfPuppets {
// private class: only MasterOfPuppets can create
private class PuppetImpl : IPuppet {
public void DoSomething() {
}
}
static MasterOfPuppets _instance = new MasterOfPuppets();
public static MasterOfPuppets Instance {
get { return _instance; }
}
// private set accessor: only MasterOfPuppets can replace instance
public IPuppet Puppet {
get;
private set;
}
}
class Program {
public static void Main(params string[] args) {
// access singleton and then inner instance
MasterOfPuppets.Instance.Puppet.DoSomething();
}
}

Categories

Resources