PDA

View Full Version : Cho em hỏi đoạn code này sai chỗ nào?



Heo còi
28-01-2009, 00:36
public static Boolean Formdamo(string tenform)
{
Form frm = QLCP.ActiveForm;
foreach (Form f in frm.MdiChildren)
{
if (f.Name == tenform)
{
f.Activate();
break;
return true;
}
else
{
break;
return false;
}
}
}

Khi chạy đoạn code c# này thì báo lỗi: not all code paths return a value
Xin mọi người chỉ giúp xem nó sai ở chỗ nào, em xin cảm ơn!

huonghx
28-01-2009, 16:13
public static Boolean Formdamo(string tenform)
{
boolean temp;

Form frm = QLCP.ActiveForm;
foreach (Form f in frm.MdiChildren)
{
if (f.Name == tenform)
{
f.Activate();
break;
temp= true;
}
else
{
break;
temp=false;

}
}
return temp;
}

Heo còi
29-01-2009, 17:26
public static Boolean Formdamo(string tenform)
{
boolean temp;

Form frm = QLCP.ActiveForm;
foreach (Form f in frm.MdiChildren)
{
if (f.Name == tenform)
{
f.Activate();
break;
temp= true;
}
else
{
break;
temp=false;

}
}
return temp;
}


Cách này em đã làm và đây là lỗi phát sinh Error 3: Use of unassigned local variable 'temp'

dangviethai
30-01-2009, 19:40
ơ hơ bác phải để
temp= true;

temp=false;
ở trước
break;
chứ nếu không thì khi code chạy nó vào break; là nó thóat khỏi vòng lặp luôn nên biến temp coi như là vô dụng.

p/s: khi khởi tạo biến bác nên gán cho nó 1 giá trị

boolean temp = true;

namrom
02-02-2009, 15:20
public static Boolean Formdamo(string tenform)
{
Form frm = QLCP.ActiveForm;
foreach (Form f in frm.MdiChildren)
{
if (f.Name == tenform)
{
f.Activate();
break;
return true;
}
else
{
break;
return false;
}
}
}


Break mất rồi, làm sao mà nó return được nữa

tinhdoinghesi
03-02-2009, 14:22
public static Boolean Formdamo(string tenform)
{
Form frm = QLCP.ActiveForm;
foreach (Form f in frm.MdiChildren)
{
if (f.Name == tenform)
{
f.Activate();
break;
return true;
}
else
{
break;
return false;
}
}
return false;
}