C#声明变量进入if语句 [英] C# declare variable into if statement
本文介绍了C#声明变量进入if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做这样的事情使用C#:
i want to do something like this using C# :
if (i == 0)
{
button a = new button();
}
else
{
TextBlock a = new TextBlock();
}
mainpage.children.add(a);
但我得到一个错误,
But i get an error that
错误1名'一个'不在当前情况下存在
Error 1 The name 'a' does not exist in the current context
任何想法?
感谢你在前进!
推荐答案
您需要一个共同的基类,这两个按钮
和文本块
从派生,它需要外部的如果拟申报code>语句,如果它是在
如果
来访问已完成。 控制
也许
You need a common base class that both button
and Textblock
derive from, and it needs to be declared outside of the if
statement if it's to be accessed after the if
is complete. Control
maybe?
Control a;
if (i == 0)
{
a = new button();
}
else
{
a = new TextBlock();
}
mainpage.children.add(a);
不知道你用什么具体的控制工具包(WPF也许?)我不能再提醒。但我会看签名添加
来得到一个线索 - 有什么声明
Not knowing what specific control toolkit you're using (WPF maybe?) I can't advise further. But I'd look at the signature for Add
to get a clue - what's the parameter declared as?
这篇关于C#声明变量进入if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文