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

  • Home
  • SEARCH
  • 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 169467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:38:10+00:00 2026-05-11T12:38:10+00:00

I have two methods that basically converts underlying checkboxes’ text or tag as CSV

  • 0

I have two methods that basically converts underlying checkboxes’ text or tag as CSV strings.

These two methods

  • GetSelectedTextAsCsv()
  • GetTagAsCsv()

differ only by which property to extract value from SelectedCheckBoxes, which is of type IList<CheckBox>

    public string GetSelectedTextAsCsv()     {         var buffer = new StringBuilder();         foreach (var cb in SelectedCheckBoxes)         {             buffer.Append(cb.Text).Append(',');         }         return DropLastComma(buffer.ToString());     }      public string GetTagAsCsv()     {         var buffer = new StringBuilder();         foreach (var cb in SelectedCheckBoxes)         {             buffer.Append(cb.Tag).Append(',');         }         return DropLastComma(buffer.ToString());     } 

I was trying to extract a method that returns a Func<T, TResult> but not sure how I can pull that off. My poor attempt was like the following but I cannot figure out how to extract the property portion as shown in the comment within ConvertToCsv()

    public Func<T, string> ConvertToCsv<T>()     {         return propertyName =>         {             var buffer = new StringBuilder();             foreach (var checkBox in SelectedCheckBoxes)             {                 buffer.Append(                     /* How can you abstract this portion? like following? */                      checkBox.propertyName                 ).Append(',');             }             return DropLastComma(buffer.ToString());         };     } 

If I am on a wrong track, would you please advise me on how I can refactor above code to use a common method?

[UPDATE 1] Here is the combination of both Brian and Jon’s answers

    public string ConvertToCsv<T>(Func<CheckBox, T> getValue)     {         var stringValues = SelectedCheckBoxes.Select(             cb => getValue(cb).ToString()).ToArray();         return string.Join(',', stringValues);     }      public string GetSelectedTextAsCsv()     {         return ConvertToCsv(cb => cb.Text);     }      public string GetTagAsCsv()     {         return ConvertToCsv(cb => cb.Tag);     } 

[UPDATE 2] version 2

    public string GetAsCsv<T>(Func<CheckBox, T> getValue)     {         return string.Join(',', SelectedCheckBoxes.Select(             cb => getValue(cb).ToString()).ToArray());     }      public string GetSelectedTextAsCsv()     {         return GetAsCsv(cb => cb.Text);     }      public string GetTagAsCsv()     {         return GetAsCsv(cb =>              cb.Tag == null ? string.Empty : cb.Tag.ToString());     } 

[UPDATE 3] Made the parameter of GetAsCsv() as a closed generic of CheckBox and string

Func<CheckBox, T> to Func<CheckBox, string>.

That allowed me to make GetAsCsv() even simpler and more readable.

private string GetAsCsv(Func<CheckBox, string> getValue) {     return string.Join(',', SelectedCheckBoxes.Select(getValue).ToArray()); } 
  • 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. 2026-05-11T12:38:10+00:00Added an answer on May 11, 2026 at 12:38 pm
    public string GetAsCsv(Func<CheckBox, string> getValue) {     var buffer = new StringBuilder();     foreach (var cb in SelectedCheckBoxes)     {         buffer.Append(getValue(cb)).Append(',');     }     return DropLastComma(buffer.ToString()); } 

    Then:

    GetAsCsv(cb => cb.Tag != null ? cb.Tag.ToString() : string.Empty); GetAsCsv(cb => cb.Text); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Retrieving it via getAttribute(): alert(document.getElementById( "my_div" ).getAttribute("my_attr")); Works fine for… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer For each point, create a text object with your label… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer With filetype indent on inside my .vimrc, Vim indents HTML… May 11, 2026 at 5:58 pm

Related Questions

I have two methods that basically converts underlying checkboxes' text or tag as CSV
This is a followup to this question . I seem to be stuck on
I was working on some code recently and came across a method that had
I am currently doing a CRUD project for school and basically they want us
Sorry for the bad question title. Let's say that I have DateRange class that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.