[C#] [XNA] Get and set with extensions?

Started by
10 comments, last by Spa8nky 14 years, 2 months ago
for( int componentIndex = 0; componentIndex  < 3; ++componentIndex  ) {  vec.SetByIndex( componentIndex, valueGoesHere );}

Obviously, contrived, but you can scale the operation. I personally do not consider it worthwhile (or indeed, good practice) to build an extension method for this sort of thing though. For complex examples like Spa8nky's, I'd either refactor the complexity to a function (if it's actually that complicated) or pull it outside the loop. The latter can always be done if the computation has no dependence on the component index.
Advertisement
Would this be a sensible way of doing it then:

        public static void SetIndex(ref Vector2 v, int index, float value)        {            switch (index)            {                case 0:                     {                         v.X = value;                        break;                    }                case 1:                     {                         v.Y = value;                        break;                    }                default:                    {                        // This is unsafe code BUT it is useful for debugging                        throw new ArgumentOutOfRangeException(                            "Vector component index out of range"                        );                    }            }        }

This topic is closed to new replies.

Advertisement