Solved

How to use string.join in method

Posted on 2014-11-12
3
94 Views
Last Modified: 2014-11-13
I'm trying to create a method that returns a comma separated list of all Calendar controls on a page, the solutions I've read have suggested using string.join; however, I an confused on how to implement this.  Could this also be an alternative solution?

protected String GetAllCalControls()
    {
        StringBuilder countCal = new StringBuilder();            
        foreach (Control ctrl in Page.Controls)
        {
            foreach (Control childc in ctrl.Controls)
            {
                if (childc is Calendar)
                {
                    countCal.Append(childc.ID+",");
                }
            }
        }
        countCal.Remove(countCal.Length, -1);
        return countCal.ToString();
    }

Open in new window

0
Comment
Question by:Scarlett72
  • 2
3 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40438600
You might be looking for something like:
private string GetAllControls()
{
	return string.Join(",", (from Control control in Controls where control is Calendar select (control as Calendar).ID).ToArray());
}

Open in new window


Make sure you add using System.Linq if it is not already there.

-saige-
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40438786
Actually after looking over your initial request, I determined that the above solution won't work because it does not take into account that controls can have child controls.  So I changed the function like such:
private string GetAllControls(Control parent)
{
	// Create a result string to return
	string result = string.Empty;
	// If the parent has a collection of controls and it is greater than 0
	if (parent.Controls.Count > 0)
	{
		// Use string join and enumerate the child controls in the parent collection, find the calendar control and retrieve it's name.
		result = string.Join(",", (from Control child in parent.Controls where child is Calendar select (child as Calendar).ID).ToArray());
		// Enumerate through each child control in the parent and see if the child has a control collection, if so select the child control.
		foreach (Control child in (from Control child in parent.Controls where child.Controls.Count > 0 select child))
		{
			// If our result string is not null or empty, we will join the result of this function recursively with the result string, otherwise, call this function and set result to it's return.
			if (!string.IsNullOrEmpty(result))
				result = string.Join(",", new string[] { result, GetAllControls(child) });
			else
				result += GetAllControls(child);
		}
	}
	// If result is not null or empty and the result ends with a comma, strip it off.
	if (!string.IsNullOrEmpty(result) && result.LastIndexOf(",") == result.Length - 1)
		result = result.Substring(0, result.Length - 1);
	// return the result
	return result;
}

Open in new window


I added some commenting so that you could see what was happening.

-saige-
0
 

Author Closing Comment

by:Scarlett72
ID: 40441189
Thank you it_saige, this is fantastic.  I really need to play with it some more and read up on the concepts you've used to fully understand your solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question