请各位大侠有空帮小弟看看问题到底是出在哪里,故障有两个,第一个是下面那个被调用的函数在return了以后还要在从SetParentNode(familyStr, entity.ParentCategoryId.Value)处按顺序往下执行一次(虽然并没有影响返回的结果);第二个是上面的主函数当中在return result以后,就说result是未被定义的变量,真的不知道是哪里出了问题了,希望各位大侠看看。。。
主函数
string
SetFamilyNode(
int
parentCategoryId)
{
try
{
string result = parentCategoryId.ToString();
if ( ! parentCategoryId.Equals( 0 ))
{
result = SetParentNode( new StringBuilder(result), parentCategoryId);
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}
{
try
{
string result = parentCategoryId.ToString();
if ( ! parentCategoryId.Equals( 0 ))
{
result = SetParentNode( new StringBuilder(result), parentCategoryId);
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}
被调用函数
string
SetParentNode(StringBuilder familyStr,
int
infoCategoryId)
{
try
{
entity = service.GetByInfoCategoryId(infoCategoryId);
if (entity != null )
{
familyStr.Append( " , " + entity.ParentCategoryId.Value.ToString());
if ( ! entity.ParentCategoryId.Value.Equals( 0 ))
{
SetParentNode(familyStr, entity.ParentCategoryId.Value);
}
}
return familyStr.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}
{
try
{
entity = service.GetByInfoCategoryId(infoCategoryId);
if (entity != null )
{
familyStr.Append( " , " + entity.ParentCategoryId.Value.ToString());
if ( ! entity.ParentCategoryId.Value.Equals( 0 ))
{
SetParentNode(familyStr, entity.ParentCategoryId.Value);
}
}
return familyStr.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}
把上面的改成如下就可以了
string
SetParentNode(StringBuilder familyStr,
int
infoCategoryId)
{
try
{
InfoCategoryService service = new InfoCategoryService();
InfoCategory entity = new InfoCategory();
entity = service.GetByInfoCategoryId(infoCategoryId);
if (entity != null )
{
familyStr.Append( " , " + entity.ParentCategoryId.Value.ToString());
if ( ! entity.ParentCategoryId.Value.Equals( 0 ))
{
SetParentNode(familyStr, entity.ParentCategoryId.Value);
}
}
return familyStr.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}
{
try
{
InfoCategoryService service = new InfoCategoryService();
InfoCategory entity = new InfoCategory();
entity = service.GetByInfoCategoryId(infoCategoryId);
if (entity != null )
{
familyStr.Append( " , " + entity.ParentCategoryId.Value.ToString());
if ( ! entity.ParentCategoryId.Value.Equals( 0 ))
{
SetParentNode(familyStr, entity.ParentCategoryId.Value);
}
}
return familyStr.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message.Trim());
}
}