简体   繁体   中英

C# Class public int cannot reference the non-static int

I am having a small problem, (I looked around to fine something like this but it did not help) were I create int size = 1; and then have a public int backgroundWidth = size * Images.Background.Width; . This worked when it was a static int but I want to change the int at will. This is all in the same class and should be working but it does not like to multiply ints ?

EDIT: 10:21 12/3/2013

@dcastro I try to use that format that was given to me but a small problem is still there.

because I am using XNA and the format may have been a little off, here is the more indepf code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace **.StartUp
{
    public class Resize
    {
        #region Define
        private int size = 1;
        //
        public int backgroundWidth;
        public int backgroundHeight;
        #endregion

        #region Update
        public static void Update(GameTime gameTime)
        {
        }
        #endregion

        #region public Methods
        #endregion
    }
}

I have defined the int's but when I add the lower half of the code it needs a return value for it to work. Or I may be over complicating it.

I add this in to public methods:

public MyClass()
    {
        backgroundWidth = size * Images.Background.Width;
    }

EDIT: 10:35 12/3/2013

The error now is that the method needs to have a return type and am stick not knowing what to do. I am learning as much as possible and am right now resurrecting what I have to do. I would like to have some help if possible, thank you.

EDIT: 10:26 12/4/2013

I have now been able to call on the image, but using a diffrent method, I still use the Resize class but only construct them and then use them in the main class( Game1.cs ). I add a bool so that when ever some one wants to change the size it will make an if statment go through and change the integers.

Game1.cs (Update Method):

protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            #region GameState Switch
            switch (gameState)
            {
                case GameStates.StartUp:
                    break;
                case GameStates.TitleScreen:
                    StartUp.TitleScreen.Update(gameTime);
                    break;
                case GameStates.Options:
                    break;
                case GameStates.Credits:
                    break;
            }
            #endregion

            #region Image Resize
            if (resize.change == true)
            {
                resize.change = false;
                resize.continueHeight = resize.size * StartUp.Images.Continue.Height;
                resize.continueWidth = resize.size * StartUp.Images.Continue.Width;
                StartUp.TitleScreen.con = new Rectangle(330, 246, resize.continueWidth, resize.continueHeight);
            }
            #endregion

            base.Update(gameTime);
        }

You'll have to initialize the field in the constructor.

public class MyClass
{
    private int _size = 1;
    private int _backgroundWidth;

    public MyClass()
    {
        //TODO: initialize 'Images'
        _backgroundWidth = _size * Images.Background.Width;
    }
}

As per the MSDN documentation :

A variable initializer for an instance field cannot reference the instance being created.

which means you cant do something like this:

private int _backgroundWidth = this._size * this._something;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM