简体   繁体   English

C#Class public int无法引用非静态int

[英]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; 我遇到了一个小问题,(我环顾四周可以对这样的东西进行罚款,但没有帮助),如果我创建了int size = 1; and then have a public int backgroundWidth = size * Images.Background.Width; 然后具有一个公共int backgroundWidth = size * Images.Background.Width; . This worked when it was a static int but I want to change the int at will. 当它是static int时此方法有效,但我想随意更改int。 This is all in the same class and should be working but it does not like to multiply ints ? 这都在同一个类中,应该可以工作,但是它不喜欢乘ints吗?

EDIT: 10:21 12/3/2013 编辑:10: 21 12/3/2013

@dcastro I try to use that format that was given to me but a small problem is still there. @dcastro我尝试使用给我的格式,但是仍然存在一个小问题。

because I am using XNA and the format may have been a little off, here is the more indepf code: 因为我使用的是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
    }
}

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. 我已经定义了int,但是当我添加代码的下半部分时,它需要一个返回值才能起作用。 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 编辑: 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 编辑: 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 ). 现在,我已经可以调用图像了,但是使用一种不同的方法,我仍然使用Resize类,但只构造它们,然后在主类( 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. 我添加了一个布尔值,以便在有人想要更改大小时都会通过if语句并更改整数。

Game1.cs (Update Method): 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);
        }

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 : 根据MSDN文档

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#“非静态字段需要对象引用”,静态成员函数的类问题 - C# “An object reference is required for the non-static field,” Class issue with Static Member Function C#使用反射调用公共非静态方法而不实例化其类 - C# calling a public non-static method using reflection without instantiating its class 非静态字段,方法或属性DoStuff(int,int)'需要对象引用 - An object reference is required for the non-static field, method, or property DoStuff(int, int)' 非静态字段,方法或属性ConsoleApplication5.Program.myFirst(int,int)要求对象引用 - An object reference is required for the non-static field, method, or property ConsoleApplication5.Program.myFirst(int, int) 静态类 JINT 中的 C# 非静态类 - C# non-static class inside static class JINT C#-将静态类初始化为非静态类? - C# - initialize static class as non-static class? asp.net c# - 字段初始值设定项不能引用非静态字段,方法或属性 - asp.net c# - A field initializer cannot reference the non-static field, method or property c#数组初始化“无法引用非静态字段,方法或属性”错误 - c# array initialization “cannot reference the non-static field, method or property” error C#中的线程问题(字段初始化器无法引用非静态字段,等等) - Threading problems in C# (A field initializer cannot reference the non-static field, ect) 如何引用不同类C#的非静态成员 - How do I reference a non-static member of a different class c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM