开发者

Duplicate code in IF else blocks...how do I make it shorter?

if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) == null) 
                {
                    Int32 LangID = -1;
                    string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
                    string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
                    string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
                    string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
                    Response.Redirect(ROSDAAB.Const开发者_JAVA技巧ants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
                }
                else   
                {

                    Int32 LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
                    string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
                    string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
                    string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
                    string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
                    Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);
                }

[EDIT] Thats duplicate code in IF else blocks...how do I make it shorter ?


Int32 LangID = -1;
if (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] != null) 
{
    LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
}
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);

Here's what I did here:

  • extract LangID out of the if, set it to the default value of -1, and only update it if LanguageID is not null.
  • place the common part that was duplicated in your if/else clauses after the if check, since the else is no longer needed at all.


Why use ToString()?

if (e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] == null)

Just be aware of the underlying type

Int32 LangID = (Int32)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"];

So as for shorter:

int LangID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] != null ? (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] : -1;
string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);

If the null check does not work, try !Convert.IsDBNull(), if your values are from a data source.

int LangID = !Convert.IsDBNull(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"]) ? (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"] : -1;


You can also use the ternary operator to reduce repeats:

var tempLangId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"];
Int32 LangID = tempLangId == null ? -1 : Convert.ToInt32(tempLangId);


You below code this:-

   bool isOk = String.IsNullOrEmpty(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());    
   Int32 LangID = isOk ? -1 : Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()); ;
   string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode);
   string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode);
   string QuesID = com.Encrypt(ID.ToString(), com.KeyCode);
   string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());
   Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);


string QuesTypeID = com.Encrypt(TypeID.ToString(), com.KeyCode); string LanguageID = com.Encrypt(LangID.ToString(), com.KeyCode); string QuesID = com.Encrypt(ID.ToString(), com.KeyCode); string PID = com.Encrypt(radGridQues.CurrentPageIndex.ToString(), com.KeyCode.ToString());

    Int32 LangId = -1;

    if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) != null) 
    {
                Int32 LangID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString());
    }


    Response.Redirect(ROSDAAB.Constants.SiteURL + "editQues/" + "QuesID/" + QuesID + "/" + PID + "/" + QuesTypeID + "/" + LanguageID);


ok I tried this ..it worked

 if ((e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["LanguageID"].ToString()) == String.Empty)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜