【问题标题】:C# Class public int cannot reference the non-static intC# Class public int 不能引用非静态 int
【发布时间】:2013-12-03 14:57:19
【问题描述】:

我有一个小问题,(我环顾四周寻找类似这样的东西,但没有帮助)我创建 int size = 1; 然后有一个公共 int backgroundWidth = size * Images.Background.Width;。这在static int 时有效,但我想随意更改 int。这都在同一个类中,应该可以工作,但它不喜欢乘以ints

编辑: 2013 年 12 月 3 日 10:21

@dcastro 我尝试使用提供给我的格式,但仍然存在一个小问题。

因为我用的是XNA,格式可能有点不对,这里是比较独立的代码:

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
    }
}

我已经定义了 int,但是当我添加代码的下半部分时,它需要一个返回值才能工作。或者我可能过于复杂了。

我将它添加到公共方法中:

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

编辑: 2013 年 12 月 3 日 10:35

现在的错误是该方法需要有一个返回类型并且我坚持不知道该做什么。我正在尽可能多地学习,现在我正在恢复我必须做的事情。如果可能的话,我想得到一些帮助,谢谢。

编辑: 2013 年 12 月 4 日 10:26

我现在已经能够调用图像,但是使用不同的方法,我仍然使用Resize 类,但只构造它们,然后在主类中使用它们(Game1.cs)。我添加了一个布尔值,这样当有人想要更改大小时,它会执行一个 if 语句并更改整数。

Game1.cs(更新方式):

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);
        }

【问题讨论】:

  • 在您编写代码时包含代码(至少对于整数成员),帮助我们帮助您。我看不出它为什么不起作用。
  • 有什么问题?发生什么了?你有错误吗?
  • 如果您尝试在 Images 课程中使用它,我建议您注意,因为所有内容都是静态的。为了使用非静态属性,您首先必须创建一个新对象。 (我指的是this question
  • 关于更新:到底是什么问题?尝试更具描述性,否则我们无法为您提供帮助。
  • 如果Images.Background.Width 的类型是int,那么应该可以。什么是编译器错误?

标签: c# class static xna int


【解决方案1】:

您必须在构造函数中初始化该字段。

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

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

根据MSDN documentation

实例字段的变量初始化器不能引用正在创建的实例。

这意味着你不能这样做:

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

【讨论】:

  • @RyanFoy 我已经用示例和解释更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2020-11-26
  • 1970-01-01
  • 2019-09-01
相关资源
最近更新 更多