Jump to content
  • Advertisement
Sign in to follow this  
MrKao

C# -- ref as Member Variable?

This topic is 2718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, all.

I'm trying to pass a reference to a 2D Vector (e.g. a position variable) to another class and have that class actually hold on to that reference. The intended effect being that as this 2D Vector changes, any code in my second class that relies on its X and Y values will have the updated values to work with automatically.

I thought I might try giving the class a reference to a 2D Vector as a member variable (i.e. "private ref Vector2 mRef"), but this seems to be illegal syntax. I haven't done much with references in C# before, so I suppose I'm simply misunderstanding something about how they work. Can anyone help me with my syntax, or ideas on how to achieve my intended behavior? I can't think of an easy way for my class to refer to an accessor method for the 2D Vector, so that's why I thought a reference might be a good approach.

Thanks for the help.

Share this post


Link to post
Share on other sites
Advertisement
The ref keyword only works with method arguments in C#.
Alternatives include having something else push the updates to the class or having the class (ab)use delegates.
Example of the latter:

using System;

class Foo {
public Func<Vector2> GetPosition;
public Vector2 Position { get { return GetPosition(); }}

public void Print() {
Console.WriteLine( "{0},{1}", Position.X, Position.Y );
}
}

class Program {
static void Main() {
Vector2 v = new Vector2(1,2);
Foo foo = new Foo()
{ GetPosition = () => v
};
foo.Print();
v.X = 3;
foo.Print();
}
}

Share this post


Link to post
Share on other sites
Or you could wrap the Vector in a reference type, like

[color="#660066"][font="CourierNew, monospace"]class Wrapper { [/font][color="#660066"][font="CourierNew, monospace"]Vector2 Wrapped {get; set;} [/font][color="#660066"][font="CourierNew, monospace"]} [/font]
[color="#660066"][font="CourierNew, monospace"] [/font]
[color="#660066"][font="CourierNew, monospace"][color="#000000"][font="arial, verdana, tahoma, sans-serif"]and use that if you had to?[/font][/font]

Share this post


Link to post
Share on other sites

Or you could wrap the Vector in a reference type, like

[color="#660066"][font="CourierNew, monospace"]class Wrapper { [/font][color="#660066"][font="CourierNew, monospace"]Vector2 Wrapped {get; set;} [/font][color="#660066"][font="CourierNew, monospace"]} [/font]
[color="#660066"][font="CourierNew, monospace"] [/font]
[color="#660066"][font="CourierNew, monospace"][color="#000000"][font="arial, verdana, tahoma, sans-serif"]and use that if you had to?[/font][/font]

A bit less flexible (because whatever we're trying to track needs to use the wrapper too) but yes, that would be another option.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!