Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio says "Method must have a return type"

It says

"Method must have a return type"

whenever I try to debug it.

I don't know how to fix this class

This is a player class for a c# coded 2d Game

public class player
{
    public float moveSpeed;
    public Vector2 position;
    public Texture2D texture;

    //default constructer
    public Player(Texture2D tex, Vector2 startPos)
    {
        position  = startPos;
        texture   = tex;
        moveSpeed = 5.0f;
    }
    public void Update(GameTime gameTime)
    {
        //------------------------------------------
        //check for keyboard input(keyboard IF statements)

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}
like image 724
user3477229 Avatar asked Mar 30 '14 03:03

user3477229


People also ask

Does a method must have a return type?

Methods use the return operator to return a value. Any method that is not declared void must contain a return statement. The data type of the value returned by the return statement must match the data type that the method claims to return; you can't return an Object from a method declared to return an integer.

Can a method have no return type?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

Does a method need a return type C#?

We use “void” keyword if we want a method not to return anything but perform operations only / Execute group of statements. NOTE: if return type is anything except void, then method must have “return “statement.

What is return type of method in C#?

Defining Methods in C# The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void. Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.


1 Answers

Your class is player but the constructor is Player, because they are different it is expecting Player to be a method rather than a constructor

Change the class name to Player and you will be good

like image 91
FabianCook Avatar answered Sep 20 '22 14:09

FabianCook