Solved

How to use string.join in method

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

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 32

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now