Jump to content

C# class question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
Hi everybody, I've recently been programming a game and I wanted to use vectors for movement, I found a vector class in Python that has __add__ methods, which allows you to do:

vec1 += vec2

(something I needed for my class) -- the only way I've been able to replicate that is with a static method:

		public static Vector2 Add (Vector2 a, Vector2 b)

		{

			return new Vector2 (a.X + b.X, a.Y + b.Y);

		}


is there a way to do this in C#?

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
You mean using an operator to add the two vectors, and not a method. Be explicit when asking.
public Vector2 operator+(Vector2 a, Vector2 b)
{
      return new Vector2 (a.X + b.X, a.Y + b.Y);
}
Now you can add them without any problem.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
That's just what I was looking for, thanks (just had to change them to public static instead of just public)