C#无法从另一个班级获得价值(分数) [英] C# Unable to obtain value (a score) from another class

查看:57
本文介绍了C#无法从另一个班级获得价值(分数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好读者,

我正在尝试制作一些益智游戏连接点".
我有以下问题:
我想做的是在表单上获取一个得分值(属性ScoreCounter(在Grid.cs类中))(很可能用标签显示).
我试图将scoreCounter公开,只是为了对其进行测试,并且还尝试使一个性能不佳的财产.
我尝试调试,发现当玩家假设要越过点时获得一个点时,scoreCounter的值实际上会在条件内增加.
但是,当我在声明分数的顶部放置一个断点时,它告诉我它没有任何价值.
有人可以帮我获得分数,以便将其显示在标签中吗?
提前非常感谢!
调试示例


代码如下所示:

在类Form的顶部

Hello reader,

I''m trying to make a little puzzle game "Connect the dots".
And I have the following problem:
What I''m trying to do is to get a score value (property ScoreCounter(in class Grid.cs)) on my form (most likely showable with a label).
I tried to make scoreCounter public just to test it out, and I also tried to make a property which didn''t work aswell.
I tried debugging and I saw that when the player is suppose to obtain a point when crossing a dot the value of scoreCounter actually increments within the condition.
But when I place a breakpoint at the top where I declared the score, it tells me it has no value.
Could someone please help me to obtain the score so I can display it in a label?
Many thanks in advance!
Debug example


This is how the code looks like:

At the top of the class Form

private Grid drawing;



类Form中的某处



Somewhere in the class Form

lblScore.Text = "Score: " + Convert.ToString(drawing.ScoreCounter);



Grid.cs类



Class Grid.cs

private int scoreCounter;
public int ScoreCounter
{
    get { return scoreCounter; }
    set { scoreCounter = value; }
}


protected override void OnMouseUp(MouseEventArgs e)
{
    base.OnMouseUp(e);  //On Panel
    var dotLocation = this.GetDotFromPoint(e.Location); //checks if graphic location is a dot

    // If the first holded dot is color green
    if (lastDot.DotColor == colorGreen)
    {
        firstGreen = true;
    }

    if (firstGreen)
    {
        // Checks if MouseUp is on a dot that isn''t colored red
        // Checks if the user doesn''t skip a dot
        // Checks if the user doesn''t cross a dot diagonally (only allows horizontally)
        if (dotLocation != null && lastDot != null && dotLocation.DotColor != colorRed &&
       ((lastDot.Center.X + 40 == dotLocation.Center.X) || (lastDot.Center.X - 40 == dotLocation.Center.X)) &&
       (lastDot.Center.Y == dotLocation.Center.Y))
        {

            scoreCounter += 1;
            Line line = new Line();
            line.X1 = lastDot.Center.X;
            line.Y1 = lastDot.Center.Y;

            line.X2 = dotLocation.Center.X;
            line.Y2 = dotLocation.Center.Y;
            this.markers.Add(line);
            Invalidate();
        }

        // Checks if MouseUp is on a dot that isn''t colored red
        // Checks if the user doesn''t skip a dot
        // Checks if the user doesn''t cross a dot diagonally (only allows vertically)
        else if (dotLocation != null && lastDot != null && dotLocation.DotColor != colorRed &&
            ((lastDot.Center.Y + 40 == dotLocation.Center.Y) || (lastDot.Center.Y - 40 == dotLocation.Center.Y)) &&
            (lastDot.Center.X == dotLocation.Center.X))
        {
            scoreCounter += 1;
            Line line = new Line();
            line.X1 = lastDot.Center.X;
            line.Y1 = lastDot.Center.Y;

            line.X2 = dotLocation.Center.X;
            line.Y2 = dotLocation.Center.Y;
            this.markers.Add(line);
            Invalidate();
        }
        else
        {
            MessageBox.Show("Invalid turn, try again.");
        }
    }
}



我也有想法,无论我想在Form中尝试什么,例如在Grid类中调用方法,都行不通.
我尝试通过执行以下操作来清除列表标记来进行测试:

在Form中调用以下方法



I also have the idea that whatever I''m trying to in the Form like for example calling a method in the class Grid it doesn''t work.
I tested it by trying to clear the list markers by doing the following thing:

In Form calling the following method

drawing.ClearLines();



并在Grid.cs类中



And in class Grid.cs

public void ClearLines()
{
    markers.Clear();
    scoreCounter = 0;
    Invalidate();
}



我在方法OnMouseUp
中也调用了相同的方法



I also called the same method in the method OnMouseUp

else
{
    MessageBox.Show("Invalid turn, try again.");
    ClearLines() // or markers.Clear();
}



这一切都不起作用.



It all doesn''t work.

推荐答案

您是不是说您在声明score属性的行上设置了一个断点?如果是这样,无论何时更改该变量,您的断点都会命中吗?我不希望它不在变量声明中.这意味着它将在首次初始化时被命中一次-因此该值将被视为null,直到您跨过该行-即直到您对其进行初始化为止.在这种情况下,您的代码应该类似于以下内容:

Did you say you set a break point on the line where you declared the score property? If so, does your breakpoint get hit whenever that variable is changed? I would expect not if it is on the variable declaration. This would mean that it is hit once when it is first initialised - so that value would be seen as null until you have stepped over that line - i.e. until you have initialised it. Your code should probably be something like the following for this situation:

private int scoreCounter;
public int ScoreCounter
{
    get
    {
        //Do anything extra then:
        return scoreCounter;
    }
    set
    {
        //Do anything extra then:
        scoreCounter = value;
        //Then do anything else
    }
} 



注意:做任何额外的事情"应该是与scoreCounter 直接相关的任何事情-不要在方法中放入长代码.这也避免了长代码通过在另一个ScoreCounter:get调用中调用ScoreCounter:get引起无限循环的风险.

另外,所有您应该使用ScoreCounter属性,而不是证明的小写scoreCounter属性.

希望这会有所帮助,

Ed:)



Note: The ''do anything extras'' should be anything directly related to the scoreCounter - do not put long code that should be in a method. This also avoids the risk of long code causing an infinite loop by say calling the ScoreCounter:get within another ScoreCounter:get call.

Also, ALL your should use the ScoreCounter property, not the provate, lower case scoreCounter property.

Hope this helps,

Ed :)


也许您只是放错了这一行:
Maybe you just misplaced this line:
lblScore.Text = "Score: " + Convert.ToString(drawing.ScoreCounter);


你把这行放在哪里?

每当ScoreCounter值更改时,您也应该更新标签的Text属性.确保在更新分数后调用文本更新语句.我看不出它为什么不起作用的任何原因.
---------------------------------

因此,这意味着您有几个实现ScoreCounter的类的实例:
我认为您正在更新其中一个实例,但是您正在显示另一个实例.

仔细检查您实例化这些对象的位置,并确保更新和显示的对象相同.


Where did you put this line?

Whenever ScoreCounter value changes, you should update the Text property of your label as well. Make sure the text update statement is called after you update the score. I don''t see any reason why it shouldn''t work.

---------------------------------

So it means you have several instances of the class implementing the ScoreCounter:
I think you are updating one of the instances, but you are displaying another one.

Check carefully where you instanciate these objects and make sure the objects you update and display are the same.


关于未命中断点:

其他人则谈到了为什么没有击中您的断点.我对此问题加了自己的看法:我认为正在发生的事情是您正在分配给scoreCounter(支持变量)-这不会导致执行ScoreCounter(属性)集合访问器方法的代码. br/>
关于未使用分数更新UI :

我将采取以下步骤来诊断为什么不显示分数:
Regarding your breakpoint not being hit:

The others have touched on why your breakpoint wasn''t being hit; adding my own take on the problem: I think what is happening is that you are assigning to scoreCounter (the backing variable) - which will not cause the code of the ScoreCounter (property) set accessor method to be executed.

Regarding the UI not being updated with the score:

I would take the following steps to diagnose why the score is not being displayed:

  1. 删除ScoreCounter的后备存储并将ScoreCounter设置为自动属性"(即,编译器在其中为属性创建后备变量的自动属性).这将防止在分配给后备存储区和属性之间造成任何混淆.
    这一步骤不是至关重要的(您只能这样做,因为您没有在两个访问器中都进行任何工作),但是它使代码清理器,因此有帮助.
  2. 搜索对ScoreCounter属性的所有引用.

    • 查看分配了属性的所有位置:是否有任何地方将属性分配为零?
    • 在所有分配上都放置断点,看看发生了什么.

  1. Delete the backing store for ScoreCounter and make ScoreCounter an ''auto-property'' (i.e. one where the compiler creates the backing variable for the property). This will prevent any confusion beween assigning to the backing store vs. to the property.
    This step is not crucial (and you can only do it because you''re not doing work in either of the accessors), but it makes the code cleaner so it helps.
  2. Search for all references to the ScoreCounter property.

    • Look at all places where the property is assigned: are there any places where it is getting assigned zero?
    • Put breakpoints on all assignments and see what is happening.


希望有帮助.
克里斯


Hope that helps.
Chris


这篇关于C#无法从另一个班级获得价值(分数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆