View Full Version : Cho em hỏi đoạn code này sai chỗ nào?
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!
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;
}
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;
và
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;
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;
}
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.