Creating Breadcrumb - getting error as Cannot implicitly convert type 'object' to 'System.Collections.SortedList

Last post 07-03-2009 10:10 PM by venkatzeus. 2 replies.

Sort Posts:

  • Creating Breadcrumb - getting error as Cannot implicitly convert type 'object' to 'System.Collections.SortedList

    07-03-2009, 4:52 AM
    • Participant
      1,333 point Participant
    • venkatzeus
    • Member since 10-27-2006, 10:27 AM
    • Posts 535

    Hi,

    I am trying to create breadcrumb. I found a solution from the following vb.net code:

    http://www.codeguru.com/vb/vb_internet/aspnet/article.php/c6851#more

    I am trying to convert the same to c#. I am getting various errors, like Cannot implicitly convert type 'object' to 'System.Collections.SortedList, and

    Cannot implicitly convert type 'object' to 'BreadWeb.BreadCrumbUserControl.PageCrumb'. An explicit conversion exists (are you missing a cast?)

    This is the code i am using in the usercontrol,which i created:

    namespace BreadWeb
    {
        public partial class BreadCrumbUserControl : System.Web.UI.UserControl
        {
            public struct PageCrumb
            {
    
                private short _level;
                private string _url;
                private string _linkName;
    
    
                public PageCrumb(short level, string url, string linkName)
                {
    
                    _level = level;
                    _url = url;
                    _linkName = linkName;
                }
    
    
                public short Level
                {
                    get { return _level; }
                }
    
                public string Url
                {
                    get { return _url; }
                }
    
                public string LinkName
                {
                    get { return _linkName; }
                }
            }
    
            private PageCrumb _pageCrumb;
            private SortedList _crumbList;
            private string _tailName;
            private short _level;
    
            public short Level
            {
                get
                {
                    return _level;
                }
                set
                {
                    _level = value;
                }
            }
    
    
            public string TailName
            {
                get
                {
                    return _tailName;
                }
                set
                {
                    _tailName = value;
                }
            }
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!(Page.IsPostBack))
                {
    
                    if ((_level <= 0))
                    {
                        _level = 1;
                    }
    
                    if ((_tailName == ""))
                    {
                        _tailName = "Untitled";
                    }
    
                    _pageCrumb = new PageCrumb(_level, Request.RawUrl, _tailName);
    
    
                    if (Session["HASH_OF_CRUMPS"] == null)
                    {
                        _crumbList = new SortedList();
                        Session.Add("HASH_OF_CRUMPS", _crumbList);
                    }
                    else
                    {
                        _crumbList = Convert.ToString(Session["HASH_OF_CRUMPS"]);
                    }
    
    
                    ModifyList();
    
                    PutBreadCrumbs();
                }
    
            }
    
            private void ModifyList()
            {
                RemoveLowerLevelCrumbs();
                if (_pageCrumb.Level == 1)
                {
                    _crumbList.Clear();
                    _crumbList.Add((short)1, new PageCrumb(1, "/Home.aspx", "Home"));
                }
    
                else
                {
                    if (_crumbList.Count == 0)
                    {
                        _crumbList.Add((short)1, new PageCrumb(1, "/Home.aspx", "Home"));
                    }
                    _crumbList.Add(_level, _pageCrumb);
                }
            }
    
            private void RemoveLowerLevelCrumbs()
            {
    	      //  short level;
    	        ArrayList removalList = new ArrayList(_crumbList.Count);
    	        foreach (short level in _crumbList.Keys) {
    		        if ((level >= _level)) 
                    {
    			        removalList.Add(level);
    		        }
    	        }
    	        foreach (short level in removalList) 
                {
    		        _crumbList.Remove(level);
    	        }
            }
    
            private void PutBreadCrumbs()
            {
                System.Text.StringBuilder linkString = new System.Text.StringBuilder();
    
                PageCrumb pageCrumb = new PageCrumb();
                int index;
    
                for (index = 0; index <= _crumbList.Count - 2; index++)
                {
                    pageCrumb = _crumbList.GetByIndex(index);
                    linkString.Append(string.Format("<a href = {0} >{1} </a>", pageCrumb.Url, pageCrumb.LinkName));
                    linkString.Append(" > ");
                }
                pageCrumb = _crumbList.GetByIndex(index);
                linkString.Append(pageCrumb.LinkName);
    
                lblTrail.Text = linkString.ToString();
    
            }
    
    
        }
    }

    Where is the conversion I am missing..  Please help.

    Thank you

  • Re: Creating Breadcrumb - getting error as Cannot implicitly convert type 'object' to 'System.Collections.SortedList

    07-03-2009, 12:45 PM
    Answer
    • Contributor
      2,367 point Contributor
    • akhhttar
    • Member since 02-14-2007, 8:17 AM
    • Pakistan - Lahore
    • Posts 352

    Hi,

    Please see http://www.codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx to understand implicit and explicit casting in .NET.

    Now comes to your problem,

    You are doing following casting mistakes ( There could be more but during high level walkthrough of you code , i foud following two only)


    1. _crumbList = Convert.ToString(Session["HASH_OF_CRUMPS"]);

    While you need to use,

    _crumbList = (SortedList)Session["HASH_OF_CRUMPS"];


    2. pageCrumb = _crumbList.GetByIndex(index);

    while you need to use

    pageCrumb = (PageCrumb) _crumbList.GetByIndex(index);


    Thanks

    Muhammad Akhtar Shiekh

    Lets resolve the problem together.

    Please remember to mark the appropriate replies as answer after your question is solved, thanks

    My Blog
  • Re: Creating Breadcrumb - getting error as Cannot implicitly convert type 'object' to 'System.Collections.SortedList

    07-03-2009, 10:10 PM
    • Participant
      1,333 point Participant
    • venkatzeus
    • Member since 10-27-2006, 10:27 AM
    • Posts 535

    Hi,

    Thank you very much. That solved it.

Page 1 of 1 (3 items)