?
Solved

How to use string.join in method

Posted on 2014-11-12
3
Medium Priority
?
100 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 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