Azurewrath

Hi all,

I want to implement a Vector3 class just for practice. But I am in a dilemma:

I keep wondering if I should implement some methods as static and/or as instance methods, like:

1. Vector3.Normalize(v1);

or

2. v1.Normalize();


Here you might not want to change the original vector, so #1 makes sense. And maybe you want to use it on unitialized variables so, you must use #1. But maybe I want to change the original one, so #2 is more elegant than:

v1 = Vector3.Normalize(v1);


But what about this example:

3. Vector3.GetBoundingDiagonalLength(points);

or

4. points.GetBoundingDiagonalLength();

Since this returns a float, the purpose changes slightly so, #4 doesn't change the original vector like #3 doesn't.





Even in Microsoft's Vector3 implementation for XNA they make provide an extra method for out support. So I am also wondering if that's common to use IE, would you use:

v1...
v2...
Vector3.Distance(v1, v2, out d)



Lastly what about Vector3.Add, Subtract that XNA has in there Isn't it more readable, desirable and elegant using operators directly on them




Thanks alot again,
Aw



Re: Visual C# General Conventions for Class Methods

CalvinR

This is really a case of personal preference.
For myself I use an instance method (non-static) whenever a method has to access the instances fields, and use a class method (static methods) when it has to modify class fields (static fields). So if it interacts with the instance fields but I don't want to actually modify them I will generally just output a new object and then I can do with that what I want.

Also Static methods cannot be extended so if you think you are ever going to have a need to extend that method don't make it static.






Re: Visual C# General Conventions for Class Methods

Peter Ritchie

It depends on what you want these methods to do. If you want to change the value of an object, then make it an instance method. If you want to create a new object from another (like a transform) then make it a class member. Or, if your class is immutable, anything that would "change" an object must be a transform and should be an class member. But it's more style than anything. For example, String.ToUpper() is an instance method but it doesn't modify the instance. I feel it would be more clear if it were String.ToUpper(String source).






Re: Visual C# General Conventions for Class Methods

Azurewrath

Thanks guys. You are right!




Aw