Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4266964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:49:58+00:00 2026-05-21T06:49:58+00:00

I have the following code – var db = new DBEntities(); var entity =

  • 0

I have the following code –

    var db = new DBEntities();
    var entity = //get entity;

    lblName.Text = string.Empty;
    var names = entity.Names.OrderBy(x => x.Value).ToList();
    for (var i = 0; i < names .Count; i++)
    {
        if (i == names .Count - 1) lblName.Text += names [i].Value + ".";
        else lblName.Text += names [i].Value + ",&nbsp;";
    }

I’ll have several For loops like above which will format the value to be displayed in a label. I’m trying to make a method out of it which will do the formatting when I pass in the collection and the label, something like –

void FormatValue(List<??> items, Label label)
    {
        //For loop
        //Format value
    }

What do I pass in for the List. How do I make this generic enough so I’ll be able to use it for all entity.Names, entity.Xxx, entity.Yyy etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:49:59+00:00Added an answer on May 21, 2026 at 6:49 am

    Make the method itself generic and allow the caller to specify a formatter:

    void FormatValue<T>(List<T> items, Label label, Func<string, T> formatter)
    {
        foreach(var item in items)
        {
            label.Text += formatter(item);
        }
    }
    

    You can then call the method like:

    FormatValue<Name>(entity.Names.OrderBy(x => x.Value).ToList(),
                      lblName,
                      i => i.Value + ", &nbsp;");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);
I have following code in VS2008 If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then RoleCheckBox.Checked = True
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code making a GET request on a URL: $('#searchButton').click(function() {
I have following code: Tools::Logger.Log(string(GetLastError()), Error); GetLastError() returns a DWORD a numeric value, but
I have following code: $telnums = array(10, 20, 30); $obj = new StdClass(); $obj->telnums
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
I have following code private static ObservableCollection<myColor> myItems = new ObservableCollection<myColor>(); myItems.Add(new myColor(red)); When
I have following code in the form_Load function System.Windows.Forms.Timer newtimer = new Timer(); newtimer.Tick
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.