-2

In C# is there a way to make something similar to a struct that is only created when new instances of an object are created? Here is an example. I want people to be able to assign events, the problem is that there are a lot of events to assign, and often they have similar functionality, just for a different object. For example, a left button down event, right button down event, etc. etc. I thought I could organize all these with structs but, I ran into a snag when I found that structs where considered "static" and not able to access non-static members. Is there any sort of structure that would let me do this in C#.

(The end result should let me make a new object, and assign to this objects event through these structures)

MouseObject mouse  = new MouseObject();

mouse.Left.PressedEvent += somemethod();

In this example Left cannot be a struct since it is used in a non-static instance.

redcodefinal
  • 909
  • 3
  • 11
  • 24
  • 4
    Your question is very confused (or confusing). Structs aren't considered static - indeed, the concept doesn't even make sense. Your code certainly *could* be made to compile, although it wouldn't be a good idea. I see no reason to use a struct here - you should really read up about the differences between structs and classes. – Jon Skeet Jul 07 '12 at 14:32
  • 1
    When you sasy "structs are static", did you define the `Left` struct inside your `MouseObject` class? If you actually create an instance of your struct, it won't be "static" (unless you define the instance using the `static` keyword). – Jon Senchyna Jul 07 '12 at 14:35

3 Answers3

4

Why not use another class?

class MouseButton
{
    public SomeEvent PressedEvent;
}

class MouseObject
{
    public MouseButton Left { get; }
    public MouseButton Right { get; }
}
japreiss
  • 11,111
  • 2
  • 40
  • 77
  • I don't know how I missed this, I was trying to put my struct inside the MouseObject class. Derp. Thanks for the help. – redcodefinal Jul 07 '12 at 14:39
0

Mutable structs are evil. If you make it with structs, as below, it runs, but the PressedEvent never gets my handler, presumably because the struct I'm modifying isn't the one that's really in MouseObject. Go with a class, as in @japreiss's solution.

struct MouseButton
{
    internal void OnPressed()
    {
        if (PressedEvent != null)
            PressedEvent(this, EventArgs.Empty);
    }
    public event EventHandler PressedEvent;
    public event EventHandler ReleasedEvent;
}
class MouseObject
{
    public MouseButton Left { get; private set; }
    public MouseButton Right { get; private set; }
    public void OnLeftPressed()
    {
        Left.OnPressed();
    }
}
static void Main(string[] args)
{
    var m = new MouseObject();
    m.Left.PressedEvent += (s, e) => Console.WriteLine("pressed");
    m.OnLeftPressed();
}

This prints it was null!.

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

I'm guessing this is the code you used.

public class MouseObject
{
    public struct Left
    {
        public event EventHandler PressedEvent;
    }
}

In this case, Left is not a member of MouseObject. It is just another type. This kind of encapsulation behaves much like namespaces.

What you probably intended was something like this:

public class MouseObject
{
    public MouseButton Left { get; set; }

    // Left still needs to be intialized, preferably in the constructor
}

public class MouseButton
{
    public event EventHandler PressedEvent;
}
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148