Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 'get' accessor not recognised

Tags:

c#

get

accessor

xna

From http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/creating_the_player, it is instructed that this code be used:

public int Width()
    {
        get { return PlayerTexture.Width; }
    }

    public int Height()
    {
        get { return PlayerTexture.Height; }
    }

However, the 'get' accessor doesn't appear to be recognized at all. I get the following errors:

  • The name 'get' does not exist in the current context.

  • Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

Am I missing a 'using System.(Something)' line? I've seen this used successfully countless times while investigating my problem but I can't find anyone who has run into the same thing.

I am using XNA Game Studio 4.0 with Microsoft Visual C# 2010 Express. This is my full code for the Player.cs class:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Shooter
{
class Player
{
    private Texture2D PlayerTexture;
    public Vector2 Position;
    public bool Active;
    public int Health;

    public int Width()
    {
        get { return PlayerTexture.Width; }
    }

    public int Height()
    {
        get { return PlayerTexture.Height; }
    }

    public void Initialise(Texture2D texture, Vector2 position)
    {
        PlayerTexture = texture;
        Position = position;
        Active = true;
        Health = 100;
    }

    public void Update()
    {
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
    }
}
}
like image 760
user2134261 Avatar asked Mar 05 '13 04:03

user2134261


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

This isn't a valid property declaration:

public int Width()
{
    get { return PlayerTexture.Width; }
}

The () part is incorrect - that looks like you're trying to declare a method rather than a property. You should have:

public int Width
{
    get { return PlayerTexture.Width; }
}

(I haven't checked the rest, but that may well be all that's wrong.)

like image 112
Jon Skeet Avatar answered Sep 17 '22 15:09

Jon Skeet