Solved

How to use string.join in method

Posted on 2014-11-12
3
96 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

830 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