New Posts New Posts RSS Feed: Passing an EntityList?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Passing an EntityList?

 Post Reply Post Reply
Author
jeffdoolittle View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Jun-2007
Location: United States
Posts: 146
Post Options Post Options   Quote jeffdoolittle Quote  Post ReplyReply Direct Link To This Post Topic: Passing an EntityList?
    Posted: 09-Sep-2008 at 9:17pm
In order to use IEnumerable without a type parameter, you need a using statement that references System.Collections:

using System.Collections;

To get a reference to the extension method .Cast<T>(), you need a using statement like this:

using System.Linq;



Back to Top
HFloyd View Drop Down
Groupie
Groupie


Joined: 27-Jul-2007
Location: United States
Posts: 44
Post Options Post Options   Quote HFloyd Quote  Post ReplyReply Direct Link To This Post Posted: 09-Sep-2008 at 8:43pm
Thanks for your reply, Jeff.

Well, I tried leaving the T out:

    private IEnumerable mListMainEntity;

        protected IEnumerable MainEntityList
        {
            get { return mListMainEntity; }
            set { mListMainEntity = value; }
        }



but get a Build error:
Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires '1' type arguments  

I was intrigued by your second suggestion, since all I need is the Count, which is a property of Entity,  but also cannot get it formatted correctly.
This is what I tried:

base.MainEntityList = mListMainEntity.Cast<Entity>();

(The mListMainEntity already exists, I just want to pass it to the base)

private EntityList<HouseholdBusiness> mListMainEntity= new EntityList<HouseholdBusiness>();


The build error is:
'IdeaBlade.Persistence.EntityList<HS.Admin.Model.HouseholdBusiness>' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast<TResult>(System.Data.EnumerableRowCollection)' has some invalid arguments

I tried other combinations, but none were acceptable to the compiler:

base.MainEntityList = mListMainEntity().Cast<Entity>(); <--Doesn't compile


I was able to compile with this:

base.MainEntityList = (IEnumerable<Entity>)mListMainEntity;


but then got a runtime exception:
System.InvalidCastException was unhandled by user code
  Message="Unable to cast object of type 'IdeaBlade.Persistence.EntityList`1[HS.Admin.Model.HouseholdBusiness]' to type 'System.Collections.Generic.IEnumerable`1[IdeaBlade.Persistence.Entity]'."


I'm not sure how to proceed.

Heather



Edited by HFloyd - 09-Sep-2008 at 8:44pm
Back to Top
jeffdoolittle View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Jun-2007
Location: United States
Posts: 146
Post Options Post Options   Quote jeffdoolittle Quote  Post ReplyReply Direct Link To This Post Posted: 09-Sep-2008 at 8:51am
You can't use IEnumerable<Entity>, you have to just use IEnumerable.  Generics collections don't know how to auto-cast their members to a base type, so you can't do this:

IEnumerable<Entity> collection = new EntityList<HouseholdBusiness>();

because "T" is not the same for both collections.

You can either just just IEnumerable without a generic type constraint, or you could try this:

IEnumerable<Entity> collection = new EntityList<HouseholdBusiness>().Cast<Entity>();

which uses a Linq extension method to do the list cast for you.
Back to Top
HFloyd View Drop Down
Groupie
Groupie


Joined: 27-Jul-2007
Location: United States
Posts: 44
Post Options Post Options   Quote HFloyd Quote  Post ReplyReply Direct Link To This Post Posted: 09-Sep-2008 at 8:36am
Hi Jeff,

Thanks for your idea, but I'm afraid I'm not sure how to implement it.

I put this into BASE:

private IEnumerable<Entity> mListMainEntity;

        protected IEnumerable<Entity> MainEntityList
        {
            get { return mListMainEntity; }
            set { mListMainEntity = value; }
        }


But am having trouble assigning it from the Derived form:

private EntityList<HouseholdBusiness> mListMainEntity = new EntityList<HouseholdBusiness>();

base.MainEntityList = mListMainEntity;


The message is:
Cannot implicitly convert type 'IdeaBlade.Persistence.EntityList<HS.Admin.Model.HouseholdBusiness>' to 'System.Collections.Generic.IEnumerable<IdeaBlade.Persistence.Entity>'. An explicit conversion exists (are you missing a cast?)
 
Do you happen to know the cast syntax?

Thanks,

Heather
Back to Top
jeffdoolittle View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Jun-2007
Location: United States
Posts: 146
Post Options Post Options   Quote jeffdoolittle Quote  Post ReplyReply Direct Link To This Post Posted: 07-Sep-2008 at 10:00pm
How about using IEnumerable instead of object?  Then make sure you have a reference to System.Linq and you can use the .Count() extension method.

--Jeff
Back to Top
HFloyd View Drop Down
Groupie
Groupie


Joined: 27-Jul-2007
Location: United States
Posts: 44
Post Options Post Options   Quote HFloyd Quote  Post ReplyReply Direct Link To This Post Posted: 07-Sep-2008 at 1:08pm
Hi,
I am interested in using a "base" user control for building my various usercontrols for editing each entity. I would like to have some basic functionality code in the "base" to manage operations related to Data Navigation and Adding/Saving, etc. Because I need to be able to fully access the visual controls since each derived uc will look unique, I am not including those visual (form) items in my base uc, but need to reference them (for instance, to enable/disable buttons or respond to "onclicks", etc. I figured that to get around this, I would just pass my form controls from the derived uc to the base through exposed properties. This works fine for form controls, but I am also interested in dealing directly with the EntityList of the main business object being edited on the derived uc, and can't seem to figure out how to format the property in base to accept an EntityList<>, which could be of whatever type.

What I have thought of is to make the base property just an "object", since the dervied uc will define what the actual entity list is, but I am having a hard time accessing properties for the EntityList/object in the base uc code. When I write "mListMainEntity.Count", the compiler won't allow it, since .Count is not defined for generic objects. This is also not allowed: "mListMainEntity["Count"]".

Here is some example code:

derived uc :


private void HouseholdsBusinesses_Load(object sender, EventArgs e)
        {
            base.InitalizePM();
            base.OtherIntitalizing();
            ConfigureBindingSources();
            LoadData();

            //Initialize Base Property Values
            base.mCBM = this.dataCBM;
            base.mListMainEntity= mListMainEntity;
            base.mMainBindingSource = this.bsMainEntity;
            base.MainEntityType  = typeof(HouseholdBusiness);
        }   


base uc:


protected object mListMainEntity;

protected object MainEntityList
        {
            get { return mListMainEntity; }
            set { mListMainEntity = value; }
        }

private void MainEntityListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
        {
            if (mListMainEntity == null || mListMainEntity.Count == 0) <---This is the problem area
            {
                SetDNRemoveFalse();
            }
            else
            {
                SetDNRemoveTrue();

                if (!mPersMgr.HasChanges())
                { //Disable Save/Reset Buttons
                    SetDNEndEditFalse();
                    SetDNCancelEditFalse();
                }
                else
                { //Enable Save/Reset Buttons
                    SetDNEndEditTrue();
                    SetDNCancelEditTrue();
                }
            }
        }


Is there some syntax I am missing? Am I going about this the wrong way?

Thanks,

Heather
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down