webForumDet fria alternativet

Fall through i Switch sats

.NET

3 svar · 205 visningar · startad av Viktor

Medlem sedan aug. 20021 752 inlägg
Frågan#1

Jag vill ha en switch sats som ser ut såhär

switch(xmlIterator.Current.Name)
{
	case "name":
		Console.WriteLine("Name: "+xmlIterator.Current.Value);
		break;
	case "info":
		Console.WriteLine("Info: "+xmlIterator.Current.Value);
		break;
	case "pointinfo":
		xmlIterator.Current.MoveToFirstChild();
	case "pointpos":
		xmlIterator.Current.MoveToFirstAttribute();
		Console.Write("Pos: {0}",xmlIterator.Current.Value);
		xmlIterator.Current.MoveToNextAttribute();
		Console.WriteLine(", Points: {0}",xmlIterator.Current.Value);
		break;
};

dvs när den kommer ner till "pointinfo" så ska den göra det som står där och sedan gå vidare och göra det som står under "pointpos", men när jag försöker göra det så får jag

Controll cant fall through one case lable to another.

Finns det något sätt att göra så det går?

/Viktor

Medlem sedan sep. 20011 914 inlägg
#2

Re: Fall through i Switch sats

Kanske så här?

bool grr;
switch(xmlIterator.Current.Name)
{
	case "name":
		Console.WriteLine("Name: "+xmlIterator.Current.Value);
		break;
	case "info":
		Console.WriteLine("Info: "+xmlIterator.Current.Value);
		break;
	case "pointinfo":
		xmlIterator.Current.MoveToFirstChild();
                                grr = true;
                                break;
	case "pointpos":
		xmlIterator.Current.MoveToFirstAttribute();
		Console.Write("Pos: {0}",xmlIterator.Current.Value);
		xmlIterator.Current.MoveToNextAttribute();
		Console.WriteLine(", Points: {0}",xmlIterator.Current.Value);
		break;
};
if(grr == true){
   //do stuff
   grr = false
}
Medlem sedan maj 2001329 inlägg
Medlem sedan aug. 20021 752 inlägg
#4

Tack, det var något sånt som jag ville ha.

/Viktor

272 ms totalt · 4 externa anrop · v20260731065814-full.6fe65c25
123 ms — deklarationer (db)
0 ms — hämta statistik (cache)
130 ms — hämta tråd, inlägg och bilagor (db)
127 ms — ändringar (db)