Re: [问题] 变量宣告在if结构里

楼主: GoalBased (Artificail Intelligence)   2014-05-18 12:28:23
※ 引述《LetsGoToEat (一起去吃东西吧)》之铭言:
: 请教一下,原本写读取纪录变量的txt档,类似这样
: System.IO.StreamReader file = new System.IO.StreamReader("file.txt");
: ClassFather xxx = new ClassFather();
: string str = file.ReadLine();
: xxx.strA = str;
: str = file.ReadLine();
: xxx.strB = str;
: 后来改写,想由文字档的第一行判断要用父或子类别,之后的处理都一样的
: System.IO.StreamReader file = new System.IO.StreamReader("file.txt");
: string str = file.ReadLine();
: if (str == "Father")
: {
: ClassFather xxx = new ClassFather();
: str = file.ReadLine();
: xxx.strA = str;
: str = file.ReadLine();
: xxx.strB = str;
: }
: else if (str == "Son")
: {
: ClassSon xxx = new ClassSon();
: str = file.ReadLine();
: xxx.strA = str;
: str = file.ReadLine();
: xxx.strB = str;
: }
: 因为后面的xxx.strA和xxx.strB程式码都长一样,
: 有没有只要写一次的方法呢,新手不知道该搜寻什么关键字,
: 还请多见谅
if (str == "Father")
{
ClassFather xxx = new ClassFather();
IamFunction();
}
else if (str == "Son")
{
ClassSon xxx = new ClassSon();
IamFunction();
}
protected void IamFunction()
{
str = file.ReadLine();
xxx.strA = str;
str = file.ReadLine();
xxx.strB = str;
}
大概是长这样,细节有错的话自己改,
是说..现在的新手会开档读档,会用类别,不会用function是怎么回事= =
作者: LetsGoToEat (一起去吃东西吧)   2014-05-18 13:21:00
我读档是写在button的click方法里 不知变量在哪宣告

Links booklink

Contact Us: admin [ a t ] ucptt.com