Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:41:04+00:00 2026-06-10T17:41:04+00:00

I’m trying to find how to add functionality to C# structures, and in a

  • 0

I’m trying to find how to add functionality to C# structures, and in a more elegant way than just wrapping them in entirely new classes. An extension method just isn’t as powerful as an override, and most definitely cannot give a struct event firing/handling capabilities either.

The specific problem I’m currently facing however is detecting and reacting to changes in a Vector2 structure’s members. This is only an internal requirement though, so users of my library should be able to use/perceive only Vector2 structures when interacting with it (while still setting off internal events).


Generally, how can I extend C# structures other than by extension methods or encapsulation?

Specifically, how can I extend the Vector2 structure to support event firing/handling?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:41:06+00:00Added an answer on June 10, 2026 at 5:41 pm

    Generally, how can I extend C# structures other than by extension
    methods or encapsulation?

    The only way is to create extension methods or use encapsulation.

    Specifically, how can I extend the Vector2 structure to support event
    firing/handling?

    As I said previously you can use encapsulation and create a class. So use this code:

    public class ExtendedVector2 {
        public Vector2 Vector{
            get;
            private set;
        }
    
        public ExtendedVector2(float value){
            Vector = new Vector2(value);
        }   
    
        public ExtendedVector2(float x, float y){
            Vector = new Vector2(x, y);
        }
    }
    

    Then you can add interfaces, methods and events.

    EDIT :

    But how would I detect if X or Y changes? Say the user “gets” the
    Vector, and then sets one of its members (X or Y). I could add event
    firing for when the Vector as a whole is set, but not when one of it’s
    members are set after a ‘get.’ Keep in mind that I don’t want to force
    the user to use a new vector class in order to interact with my
    library. I don’t even want him/her to have to know that a new vector
    class is being used internally.

    Firstly if you want to mask completely that internally is used a structure of type Vector2 you should rewrite all methods in this way:

    public class ExtendedVector2 {
        //...
        private Vector2 _vector2;  
    
        //mask X and Y values of Vector2 structure 
        public float X{
            set{ _vector2.X = value; }
            get{ return _vector2.X; }
        }
    
        public float Y{
            set{ _vector2.Y = value; }
            get{ return _vector2.Y; }
        } 
    
        //example to mask a method of Vector2 structure 
        public static float Dot(ExtendedVector2 value1, ExtendedVector2 value2){
            return Vector.Dot(value1, value2);
        }
    
        //override the cast to Vector2
        public static implicit operator Vector2(ExtendedVector2 value) //I'd make it implicit because I'd think to it like an upcast
        {
            return new Vector2(value.X, value.Y);
        }
    }
    

    For more info look here.

    Now it’s simple if you want to create an event that is fired when one members changes.
    I’d create a costumized EventArgs. Let’s write some code:

    //use the convention of eventName+EventArgs
    class MemberChangedEventArgs : EventArgs
    {
        public readonly float LastValue{
            get;
            set;
        }
        public readonly float NewValue{
            get;
            set;
        }
    
        public MemberChangedEventArgs(float LastValue, float NewValue)
        {
            this.LastValue = LastValue;
            this.NewValue = NewValue;
        }
    }
    

    Then you can write your own event:

    public class ExtendedVector2 {
        private Vector2 _vector2;  
    
        public float X{
             set{                  
                 if(_vector2.X != value)           
                   OnMemberXChanged(new MemberChangedEventArgs(_vector2.X, value));
    
                 _vector2.X = value;   
             }
             get{ return _vector2.X; }
        }
    
        public float Y{
             set{ 
                 if(_vector2.Y != value)
                   OnMemberYChanged(new MemberChangedEventArgs(_vector2.Y, value));
    
                 _vector2.Y = value;                    
             }
             get{ return _vector2.Y; }
        }        
    
        public event EventHandler<MemberChangedEventArgs> MemberXChanged;
        public event EventHandler<MemberChangedEventArgs> MemberYChanged;
    
        public ExtendedVector2(float value){
            Vector = new Vector2(value);
        }   
    
        public ExtendedVector2(float x, float y){
            Vector = new Vector2(x, y);
        }
    
        private virtual void OnMemberXChanged(MemberChangedEventArgs e){
            if(MemberXChanged != null)
               MemberXChanged(this, e);
        }
    
        private virtual void OnMemberYChanged(MemberChangedEventArgs e){
            if(MemberYChanged != null)
               MemberYChanged(this, e);
        }
        //... 
    
        //here mask the Vector2 structure using the previous solution
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.