• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

C# Accessors

Status
Not open for further replies.
I would like it to work something like the get / set accessors.

Here is my above example
Code:
public float _Test = 0;
private float Test
{
    get { return _Test; }
    set { _Test = value; }
    custom1 { // do actions }
    custom2(int value) { // do actions }
}

Along with get / set I would like to be able to make custom1 and custom2. Change name accordingly by custom1 / 2 will do for example.
I would like for them to be able to take / return values.

Something so I can do this / something like this.

Code:
private Vector2 _Test;
public Vector2 Test
{
    get { return _Test; }
    set { _Test = value; }
    X { return _Test.X; } // Just examples.
    Y { return _Test.Y; }
}

// or like this.

public Vector2 Test
{
    get { return _Test; }
    set { _Test = value; }
    getX { return _Test.X; } // Just examples.
    getY { return _Test.Y; }
}

// This would be the most preferred way.

public Vector2 Test
{
    get 
    {
        vector2 { return _Test; }
        x { return _Test.X; } 
        y { return _Test.Y; }
    }
    set 
    { 
        vector2 { _Test= value; }
        x { _Test.X = value; } 
        y { _Test.Y = value; } 
    }
}
 
strictly speaking, for those that I used, accessors = get/set only (on a per instance variable basis)

though I guess adding your own methods that act as "Accessors" is doable... though I'd simply call them methods rather than accessors... since when you access something, it's really only either you want to use it (get) or you want to change it (set)

PS: anyway, can't you test it out yourself?
 
strictly speaking, for those that I used, accessors = get/set only (on a per instance variable basis)

though I guess adding your own methods that act as "Accessors" is doable... though I'd simply call them methods rather than accessors... since when you access something, it's really only either you want to use it (get) or you want to change it (set)

PS: anyway, can't you test it out yourself?

I'm still quite new to coding. I don't know how to create my own if it is possible. Can you give me an example ?
 
well, have you tried writing and testing the example you posted?

I'm not sure about C#, and I can't test right now but on other languages that I used you can do things like
Code:
class class_name
  method/accessor_name
    do thing here
  end
end

basically you make the "accessors" on the classes/types so that any variables for objects/instances of those can now use the "accessor"

so now if you make a variable of that class/type, you can do variable_name.method/accessor_name

then again, it's more fitting to call them methods since accessors are basically methods that are used for get/set...
 

Ralle

Owner
Level 79
Joined
Oct 6, 2004
Messages
10,183
You are still not showing any use, just the way you want to code them. Check this out:
C#:
public class MyClass
{
    private Vector vec = new Vector(2,3);
    public Vector Vec
    {
        get
        {
            return vec;
        }
        set
        {
            vec = value;
        }
    }

    public double X
    {
        get
        {
            return vec.x;
        }
        set
        {
            vec.x = value;
        }
    }

    public double Y
    {
        get
        {
            return vec.y;
        }
        set
        {
            vec.y = value;
        }
    }
}

This would allow you to do something like
C#:
var MyInstance = new MyClass();
MyInstance.Vec; // Would return the vector
MyInstance.X; // Would return the vectors X
MyInstance.Y; // Would return the vectors Y
MyInstance.X = 2; // Would set the X of the vector
 
I think I get it now, it errors since what you're trying to do is add a custom accessor for a variable... which is not allowed since it really doesn't make any sense to do it anyway...

though if for example a vector is accessible via vec, then it's X component should already be directly accessible via something like

Object.vec.x

without needing for an additional accessor or methods...

else, just do what Ralle posted
 
You realize that the term "accessor" is used to describe a get or set operation....right?

What you keep asking is whether you can make normal class methods, and yes, you can make those.

Accessor is not only used to describe get / set

Example the below uses add / remove accessors.

Code:
    public event EventHandler MyEvent
    {
        [MethodImpl(MethodImplOptions.Synchronized)]
        add 
        { 
            _myEvent = (EventHandler)Delegate.Combine(_myEvent, value);
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        remove 
        { 
            _myEvent = (EventHandler)Delegate.Remove(_myEvent, value); 
        }
    }
If you know what I am asking then can you give me an example ?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
It looks like there are also add and remove accessors that Microsoft in their brilliance didn't write about in the accessors page, however they seem to be used for something specific.

What you wrote is adding functions dynamically to objects. I do not know if this is possible or not in C# (never used it, you can google it if you want), but this is certainly not a good practice nor something very useful.
You should create a class with methods.
 
What problem are you trying to solve with custom accessors?

Never lose focus of the big picture.

Remember, if a feature X doesn't solve any problems, then it might as well not exist.
I'll accept aesthetic purposes, but you need to demonstrate what you gain from doing this.

Also, I don't like the word "possible".
It's possible to write a C or a C++ program with Pascal syntax by abusing macros. Should you? No.
 
Status
Not open for further replies.
Top