在单独的方法中铸造的问题 [英] Problem with casting in a seprate method

查看:64
本文介绍了在单独的方法中铸造的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个演员:

I have a casting inside a mehod:

//Description of the background classes. Not relevalnt to the problenm itself.
public class TypeY {....}
public class TypeX : TypeY {....}
public class BaseClass
{
    ...
    TypeY Y;
    ...
}
//End of background.
Public class SomeClass4 : BaseClass
{
    TypeX x;

    public void something()
    {
        ...
        x = y as Typex /* THIS WORKS FINE*/
        ...
    }
}





为了通过子类启用修改,我将其更改为:



To enable modifing by subclasses I changed it to:

<pre lang="c#">
Public class SomeClass4 : BaseClase

{
    TypeX x;

    public void something()
    {
        ...
        setX()
        ...
    }
    public virtual void setX()
    {
        x = y as Typex /* THIS DOES NOT WORK AS EXPECTED IN RUN */
    }

}



执行setX()x后为空。

有人能弄清楚为什么会这样吗?



我尝试过:



我检查了调试器在setX中执行的行。

虽然y as TypeX具有预期值,但执行该行后的x为空。



提前感谢您的任何解释。



yepi


following the execution of setX() x is null.
Can anyone figure out why this happens?

What I have tried:

I Checked the execution of the line in setX by the debugger.
while "y as TypeX" has the expected value, x following the execution of the line is null.

Thanks in advance for any explanation.

yepi

推荐答案

从调试器开始,在SetX调用上设置断点。

看看x和y - 在调用方法之前调试器显示它们是什么?

我们看不到声明,所以我们不知道它是什么类型, Ø你把它放进去的时候 - 我们可以;运行你的代码让我们可以告诉你。



猜测,y是null,或者是一个类这不是从Typex派生的,或者是/ overX被覆盖的东西。最可能它是null因为你还没有为它分配一个值...调试器会告诉你真相。
Start with the debugger, and put a breakpoint on the SetX call.
Look at x and at y - what does the debugger show they are before you call the method?
We can't see the declaration if y at all, so we have no idea what type it is, or what you put into it when - and we can;t run your code so we can;t tell.

At a guess, y is null, or a class that isn't derived from Typex, or something / setX is overridden. Most most likely it's null because you haven't assigned a value to it yet ... the debugger will show you the truth.


这里的问题是你缺乏对良好实践的理解实现面向对象设计(OOD)。你需要做一些关于类和继承的基础研究;并且,您需要学习如何思考何时以及在何处使用全局变量。



有一套连贯的OOD首字母缩略词SOLID表达的想法,我强烈建议你开始研究这些想法[ ^ ]。



我只能猜测你在这里做什么因为缺乏具体细节:尝试从代码中退一步,明确表达您想要做的事情。



尝试使用'TypeX作为'BaseType毫无意义...除非?



我有几个学生,他们的思维和代码与你的一样困惑;我发现唯一有用的是帮助他们开始倾斜,而不是旋转他们的轮子,这是一个硬重置:停止编码,再次研究基础知识。
The problem here is your lack of understanding of good practice in implementing object-oriented design (OOD). You need to do some basic study of classes and inheritance; and, you need to learn how to think about when, and where, to use global variables.

There is a coherent set of OOD ideas expressed in the acronym SOLID, and I strongly suggest you start studying those ideas [^].

I can only guess at what you are doing here because of the lack of specifics: try and step back from your code, and articulate what you are trying to do.

Trying to use a 'TypeX as a 'BaseType makes no sense ... unless ?

I have had a few students whose thinking and code was as confused as yours; the only thing I found useful to help them start leaning, rather than spinning their wheels, was a "hard reset:" stopping coding, and studying the basics, again.


这篇关于在单独的方法中铸造的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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