Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Cannot implicitly convert type 'ClaimsCollection' to 'System.Collections.Generic.List<ClaimsCollection>

I can't figure out why it's complaining.  I'm setting the list as the type of object and the function is returing that type:

                List<ClaimsCollection> list = new List<ClaimsCollection>();
                list = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId);

Error: Cannot implicitly convert type 'ClaimsCollection' to 'System.Collections.Generic.List<ClaimsCollection>
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

A collection and a list are not the same thing. You will need to iterate over the items in the collection, adding each to the list. AS
ClaimsCollection claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId
for each (ClaimItem claimItem in claims)
{
   list.Add(claim);
}
Are you building a list of claims or collections? If collections, then:
list.Add(Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId));

Avatar of dba123
dba123

ASKER

I'm binding to a list of Claims.  So I don't need to do any Adds
Avatar of dba123

ASKER

GetClaimList returns a collection of Claim objects..or in other words records.
If you want them in a generic list of claims, then you will have to add them individually. As:
ClaimsCollection claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId);
List<Claim> list = new List<Claim>();
The use for each to iterate the collection, and add to the list.

However, if you just want a local reference to that collection, just:
ClaimsCollection claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId);
 and you now have a local reference to the collection.

Once again, A collection of anything is not the same thing as a generic List of those objects.

ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW: "Cannot implicity convert" means an explicit cast is required. Using
AType a = b AS AType;

will return a valid pointer or null, but will not throw an exception.

Jim
Avatar of dba123

ASKER

I've done this many times.  You don't need to do any adds like you're stating.  Point in case check this out:

Lets say that GetClaimList returns a list of claim objects like this:

using (IDataReader reader = db.ExecuteReader(cmd))
{
   if (reader.Read())
   {
         Coaim newClaim = new Claim();
         newClaim.LoadFromReader(reader);
         return newClaim;
   }
}

All I have to do is this to bind that list of objects to a generic list I create of type Claim, that's it!  i've done it a thousand times but this time cannot figure out why it's complaniing on me:

                List<ClaimsCollection> list = new List<ClaimsCollection>();
                list = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId);
Avatar of dba123

ASKER

whoops or in other words per my last example would be more like :

                List<Claims> list = new List<Claims>();
                list = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId);
Avatar of dba123

ASKER

So in other words, the adding is done at the Data Layer using a reader into your object.  The generic list just binds to that object which is a list of objects already..no need to be doing this add stuff.
You are correct - the mistake was mine - it just needs the explicit cast, as in:
claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId) AS List<Claim>;
Avatar of dba123

ASKER

I see what may be going on.  I need to change the function or create a new function to return a generic list of Claim objects.  Right now that Get fucntion is just returning Claim objects....ah, whoops.
We've all said that a time or two.
Avatar of dba123

ASKER

Jim good one.  Because what I ended up doing was creating another method inside our Data layer that returned a generic list back to me.  Instead I can just keep the same method and cast it....thanks!
Avatar of dba123

ASKER

I wonder though if you cast like this, is it unboxing and boxing?  meaning is the cast efficient?
Avatar of dba123

ASKER

errrr

Cannot convert type 'ClaimsCollection' to 'System.Collections.Generic.List<Claim>' via a built-in conversion
Avatar of dba123

ASKER

That last error was pertaining to this attempt

           // Create a generic list of claim records
            List<Claim> list = new List<Claim>();

            // Cast the incoming list to change it to a Generic list then bind the list
            list = GetClaimList(WebUtils.ConnectionPrefix, ContractId) as List<Claim>;
Sorry - Had to break for some dinner. Not boxing and unboxing. That occurs when you start treating a value type like a reference type. These are both reference types.
Is there a reason you want to go from a collection to a List?
Avatar of dba123

ASKER

There's actually no reason for a collection.  I can just create another method in our DAL to return a generic list.  So I forgot about that.  I've always done it that way but they were using a collection which we don't need to return.  The method called just needs to return a generic list of whatever type you're working with.
Avatar of dba123

ASKER

a generic list is a collection itself I would think also.
Avatar of dba123

ASKER

I accepted your solution as a possibility but I was getting errors when trying to cast when it tried to convert.  However the real solution for me was to just create another DAL method to return a Generic list of Claims and then i was fine again instead of it returning a collection of claims.
That's correct.