0

i need to use a for loop i think so I can access each property in each row in the array to make up my string.

  public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
  {

    foreach (var part in HintTextString)
    {
      HintText = HintText + "  " + part.Name + "  " + part.IncentiveTrainingModuleOptionName + " = " + part.Points;
    }

    return HintText;
  }

  public class UserTrainingPointsDataModel
  {
    public virtual int InteractionType { get; set; }
    public virtual int Points { get; set; }
    public virtual string Name { get; set; }
    public virtual string IncentiveTrainingModuleOptionName { get; set; }
  }

each row constains values:

  China Incentive Program  Points For Completing = 50  
  India - Q2 Incentive   Points Per Correct Answer = 50  
  China - Q2 Incentive  Points For Completing = 50  
  India Incentive Program  Points Per Correct Answer = 100  
  India - Q2 Incentive   Points Per Correct Answer = 100

HintTextString contains 5 rows each with the properties above and i need to make up a string that looks like this:

  "Incentive Program: " + part.Name[0] + "  " + part.IncentiveTrainingModuleOptionName[0] + " + " + part.Points[0] + 
  "Incentive Program: " + part.Name[1] + "  " + part.IncentiveTrainingModuleOptionName[1] + " + " + part.Points[1]

etc. So i need a for loop rather? and how would I get the length of HintTextString? intellisense does not give me that property

The view:

 <% if (module.HasAssessment)
 { %>
    <div class="<%: moduleStateClass %>">&nbsp;</div>
    <div class="<%: moduleScoreClass %>"><%: module.ModuleScore %></div>
    <% var HintText =  TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %>

    <script type="text/javascript">
       <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: " + HintText , "training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %>
    </script> 
 <% }  %> 
 <% else
    {  %>
 <% var HintText =  TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %>
 <script type="text/javascript">
    <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: "  + HintText ,"training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %>   
 </script>
 <% } %>    

"Action" :

  <div class="module-column-content">
    <% Html.RenderPartial("~/Areas/Training/Views/Home/Details.ascx", Model); %>
  </div>

Thanks

4
  • so you want to return the whole list as one sting Commented Jan 9, 2013 at 7:19
  • post the action that generates the result Commented Jan 9, 2013 at 7:24
  • Also post the view if possible Commented Jan 9, 2013 at 7:25
  • i think you need nested for Loops Commented Jan 9, 2013 at 7:25

2 Answers 2

1

If you just want to return the whole list as one sting in the format you described this should work

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
    string returnValue = string.Empty;
    foreach (var dataModel in HintTextString)
    {
        returnValue += string.Format("Incentive Program: {0}  {1} + {2}  ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points);
    }
    return returnValue;
}

As Jared mentioned, if your collection is large you probably want to use StringBuilder as is much better on perfromance.

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
    var returnValue = new StringBuilder();
    foreach (var dataModel in HintTextString)
    {
        returnValue.AppendFormat("Incentive Program: {0}  {1} + {2}  ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points);
    }
    return returnValue.ToString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

When doing rapid string manipulation StringBuilder should be used... If there are 5000 items in the array/list this could be very "costly"
0

Use a StringBuilder to construct the text, while iterating on the collection. It's optimized to work with string manipulation functions. If you use simple text adding (+=) in looping the performance can get abysmal.

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
  //format for each item 
  //{Name} {ModuleName} = {Points} <NewLine>
  string lineFormat = "Incentive Program: {0} {1} = {2}\r\n";
  StringBuilder sb = new StringBuilder();
  foreach (var part in HintTextString)
  {
     sb.AppendFormat(lineFormat, part.Name, part.IncentiveTrainingModuleOptionName, part.Points);
  }

  return sb.ToString();
}

Also, if you need the length of the HintTextString collection, you can use the IEnumerable.Count() extension method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.