CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

NameValueListBase using Guid and String instead of Int and String

Last post 05-09-2008, 11:27 AM by ktran. 4 replies.
Sort Posts: Previous Next
  •  05-07-2008, 3:16 PM 23407

    NameValueListBase using Guid and String instead of Int and String

    I'm using the CSLA.NET 3.5.1 version.  I tried changing the RoleList class from Project Tracker to point to my database roles table which uses a Guid for the RoleId instead of integer.  When I use the following code in my class  I get a InvalidCastException

    Unable to cast object of type 'System.Collections.Generic.List`1[Csla.NameValueListBase`2+NameValuePair[System.Int32,System.String]]' to type 'System.Collections.Generic.IList`1[Csla.NameValueListBase`2+NameValuePair[System.Guid,System.String]]'.

     

    using Csla;
    using Csla.Data;
    using System;
    using System.Linq;

    namespace ProjectTracker.Library
    {
        [Serializable()]
        public class RoleList : NameValueListBase<Guid, string>
        {
            #region  Business Methods

            public static Guid DefaultRole()
            {
                RoleList list = GetList();
                if (list.Count > 0)
                    return list.Items[0].Key;
                else
                    throw new NullReferenceException("No roles available; default role can not be returned");
            }

            #endregion

            #region  Factory Methods

            private static RoleList _list;

            public static RoleList GetList()
            {
                if (_list == null)
                    _list = DataPortal.Fetch<RoleList>();
                return _list;
            }

            /// <summary>
            /// Clears the in-memory RoleList cache
            /// so the list of roles is reloaded on
            /// next request.
            /// </summary>
            public static void InvalidateCache()
            {
                _list = null;
            }

            private RoleList()
            { /* require use of factory methods */ }

            #endregion

            #region  Data Access

            private void DataPortal_Fetch()
            {
                this.RaiseListChangedEvents = false;
                using (var ctx = ContextManager<ProjectTracker.DalLinq.AspNetDbDataContext>.GetManager(ProjectTracker.DalLinq.Database.AspNetDb))
                {
                    var data = from role in ctx.DataContext.aspnet_Roles
                               select new NameValuePair(role.RoleId, role.RoleName);
                    IsReadOnly = false;
                    this.AddRange(data);
                    IsReadOnly = true;
                }
                this.RaiseListChangedEvents = true;
            }

            #endregion

        }
    }

     

    Any help on this matter would be greatly appreciated.

    Thanks,

    Kent

  •  05-07-2008, 9:24 PM 23428 in reply to 23407

    Re: NameValueListBase using Guid and String instead of Int and String

    ktran:

    Unable to cast object of type 'System.Collections.Generic.List`1[Csla.NameValueListBase`2+NameValuePair[System.Int32,System.String]]' to type 'System.Collections.Generic.IList`1[Csla.NameValueListBase`2+NameValuePair[System.Guid,System.String]]'.

    Somewhere in your code you still have a field or class declared as NameValueListBase<int,string> - the exeption is quite clear on that Smile [:)]

    In fact, it appears that your actual type must be declared as an <int, string> and your field is of <guid, string> - and of course you can't cast the former to the latter.


    Rocky
  •  05-08-2008, 2:55 PM 23449 in reply to 23428

    Re: NameValueListBase using Guid and String instead of Int and String

    Thanks for you reply Rocky.  To clarify are you saying that the NameValueListBase has to be a

    <int, string> pair or can you use it for <guid, string> or whatever combonation of data types?  Because there is nowhere in my new project that declares NameValueListBase<int,string>.  I've checked the Dbml File for my Linq to Sql classes and the fields I'm using are the expected data types <guid, string>

     

    kent

  •  05-08-2008, 5:46 PM 23453 in reply to 23449

    RE: NameValueListBase using Guid and String instead of Int and String

    I’m saying that a NameValueListBase object can be of type <guid, string>, no problem.

     

    I am also saying that your exception specifically says you have an object of type <int. string> that you are trying to assign to a field of type <guid, string>. I don’t know how that is happening in your code, but that is what the exception is telling you. I suggest using the debugger to examine the object type and field type to figure out how the object is ending up as a type you don’t expect.

     

    Rocky

     

     

    From: ktran [mailto:cslanet@lhotka.net]
    Sent: Thursday, May 08, 2008 2:56 PM
    To: rocky@lhotka.net
    Subject: Re: [CSLA .NET] NameValueListBase using Guid and String instead of Int and String

     

    Thanks for you reply Rocky.  To clarify are you saying that the NameValueListBase has to be a

    <int, string> pair or can you use it for <guid, string> or whatever combonation of data types?  Because there is nowhere in my new project that declares NameValueListBase<int,string>.  I've checked the Dbml File for my Linq to Sql classes and the fields I'm using are the expected data types <guid, string>

     

    kent




    Rocky
  •  05-09-2008, 11:27 AM 23467 in reply to 23453

    Re: RE: NameValueListBase using Guid and String instead of Int and String

    Thanks Rocky, I got it worked out, stupid mistake I overlooked.

    I really appreciate your prompt replies :)

    Kent

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems