Solved

How to use string.join in method

Posted on 2014-11-12
3
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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