Set Property for List dynamically

How to set proprety for List dynamically.

I have a WebPage class having properties.

strNodeName is one of the property of this class which is the parameters of method.

I want to set the string strTitle according to the parameter strNodeName i.e., set

strTitle = _list[i].Title.ToString();

I want Title to be the strNodeName.

Like if strNodeName = Author, then

strTitle = _list[i].Author.ToString();

if strNodeName = CoAuthor, then

strTitle = _list[i].CoAuthor.ToString(); 

Below is my code.

private void GetGroupedNode(ref TreeNode Node, string strNodeName, int DocumentTypeID)
        {
            WebPage Page = new WebPage();
            List<WebPage> _list = CurrentPage.GetActivePages(_WebSiteID);
 
            TreeNode _AuthorNode;
 
            _AuthorNode = new TreeNode();
 
            // traverse the list to create the tree view
            if (_list.Count > 0)
            {
 
                // set the change in title
                strTitle = _list[i].Title.ToString();
               }
            }
        }

 Thanks 

private void GetGroupedNode(ref TreeNode Node, string strNodeName, int DocumentTypeID)
        {
            WebPage Page = new WebPage();
            List<WebPage> _list = CurrentPage.GetActivePages(_WebSiteID);
  
            TreeNode _AuthorNode = new TreeNode();
            string strTitle = string.Empty;
            // traverse the list to create the tree view
            if (_list.Count > 0)
            {
                for(int i=0;i<_list.Count; i++)
                {
                    // set the change in title
                    if(strNodeName.ToLower() == "title")
                    {
                        strTitle = _list[i].Title.ToString();
                    }
                    else if(strNodeName.ToLower() == "author")
                    {
                        strTitle = _list[i].Author.ToString();
                    }
                    else
                    {
                        strTitle = _list[i].CoAuthor.ToString();
                    }
                }
               }
            }
        }

switch(strNodeName) { case "Title":

would be more readable

TreeNode has name, value property that you can intiailze in the constructor.

thoughts on "Set Property for List dynamically"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>