kubaj
kubaj

Reputation: 63

Nonsense errors in Visual C# Express

I started with programming in C# with XNA framework. I have created new solution, made programm, saved it. Programm was working and no errors where there. At 2nd day when I opened solution I have had a lot of errors in one file. Code seems OK and here it is with errors in comments:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using The_Destiny_of_Azureus.Komponenty;

namespace The_Destiny_of_Azureus
{

public class Hra : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    public SpriteBatch spriteBatch;

    public int wwidth = 1366;
    public int wheight = 768;

    Texture2D cursor;

    MouseState mouseState;

    public Hra()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth = wwidth;
        graphics.PreferredBackBufferHeight = wheight;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();

        MMenu mmenu = new MMenu(this);
        Components.Add(mmenu);           //1 error: } expected


        public Button ngbutton = new Button(this, new Vector2(64, 258), "NEW GAME");     // Keyword 'this' is not available in the current context
        Components.Add(ngbutton);       //3 errors:
                                                     //Invalid token '(' in class, struct, or interface member declaration, 
                                                     //Invalid token ')' in class, struct, or interface member declaration
                                                     //Microsoft.XNA.Framework.Game.Components is a property but is used like a type

        base.Initialize();                       //1 error: Method must have a return type
    }


    protected override void LoadContent()     //1 error: Expected class, delegate, enum, interface, or struct
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);      //1 error: Expected class, delegate, enum, interface, or struct

        cursor = Content.Load<Texture2D>(@"Textury\cursor");
    }


    protected override void UnloadContent()            //1 error: Expected class, delegate, enum, interface, or struct
    {

    }


    protected override void Update(GameTime gameTime)        //1 error: Expected class, delegate, enum, interface, or struct
    {

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        mouseState = Mouse.GetState();

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)           //1 error: Expected class, delegate, enum, interface, or struct
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(cursor, new Vector2(mouseState.X, mouseState.Y), Color.White);      //1 chyba: Expected class, delegate, enum, interface, or struct
        spriteBatch.End(); 

        base.Draw(gameTime);
    }              //1 error: Type or namespace definition, or end-of-file expected
}
}

Errors:

Error   } expected  44  35
Error   Invalid token '(' in class, struct, or interface member declaration 47  27
Error   Invalid token ')' in class, struct, or interface member declaration 47  36
Error   Method must have a return type  49  18
Error   Expected class, delegate, enum, interface, or struct    53  28
Error   Expected class, delegate, enum, interface, or struct    56  31
Error   Expected class, delegate, enum, interface, or struct    62  28
Error   Expected class, delegate, enum, interface, or struct    68  28
Error   Expected class, delegate, enum, interface, or struct    81  28
Error   Expected class, delegate, enum, interface, or struct    86  42
Error   Type or namespace definition, or end-of-file expected   90  9
Error   Keyword 'this' is not available in the current context  46  49
Error   'Microsoft.Xna.Framework.Game.Components' is a 'property' but is used like a 'type' 47  13
Error   'The_Destiny_of_Azureus.Hra.ngbutton' is a 'field' but is used like a 'type'    47  28

I try to send whole solution to my friend, he opened solution and he had no errors in this file.

Upvotes: 0

Views: 702

Answers (2)

D Stanley
D Stanley

Reputation: 152634

You problem is here:

Components.Add(mmenu);           //1 error: } expected


public Button ngbutton = new Button(this, new Vector2(64, 258), "NEW GAME");     // Keyword 'this' is not available in the current context
Components.Add(ngbutton);       //3 errors:

The public keyword is not valid for a local variable. Either you copied and pasted that from a member definition on the class or just added the public by mistake.

The chain of errors stems from the fact that the compiler thinks that you missed a } to end Initialize and the rest of the bracing was thrown off.

There's no way that your colleague has that exact code and got it to build.

Upvotes: 8

SLaks
SLaks

Reputation: 888177

The errors are completely correct.

It doesn't make sense to have a public local variable.

Therefore, the compiler assumes that you were trying to create a field on the class, but forgot the }.
This causes more errors as it reads the rest of your code under the incorrect assumption that you wanted an extra }.

Upvotes: 3

Related Questions