Public Group

C# -- ref as Member Variable?

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

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 on other sites
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 on other sites
Great. Thank you for your help!

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 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 on other sites
Or abusing arrays:

[font="Courier New"]Vector2D[] x = new Vector2D[1];[/font]

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632630
• Total Posts
3007521
• Who's Online (See full list)

There are no registered users currently online

×