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>
LVL 1
dba123Asked:
Who is Participating?
 
JimBrandleyConnect With a Mentor Commented:
I stand corrected - List<t> does implement ICollection and IEnumerable. So,
List<Claim> claims = new List<Claim>();
claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId) AS List<Claim>;

should work.

0
 
JimBrandleyCommented:
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);
}
0
 
JimBrandleyCommented:
Are you building a list of claims or collections? If collections, then:
list.Add(Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId));

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dba123Author Commented:
I'm binding to a list of Claims.  So I don't need to do any Adds
0
 
dba123Author Commented:
GetClaimList returns a collection of Claim objects..or in other words records.
0
 
JimBrandleyCommented:
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.

0
 
JimBrandleyCommented:
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
0
 
dba123Author Commented:
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);
0
 
dba123Author Commented:
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);
0
 
dba123Author Commented:
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.
0
 
JimBrandleyCommented:
You are correct - the mistake was mine - it just needs the explicit cast, as in:
claims = Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId) AS List<Claim>;
0
 
dba123Author Commented:
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.
0
 
JimBrandleyCommented:
We've all said that a time or two.
0
 
dba123Author Commented:
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!
0
 
dba123Author Commented:
I wonder though if you cast like this, is it unboxing and boxing?  meaning is the cast efficient?
0
 
dba123Author Commented:
errrr

Cannot convert type 'ClaimsCollection' to 'System.Collections.Generic.List<Claim>' via a built-in conversion
0
 
dba123Author Commented:
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>;
0
 
JimBrandleyCommented:
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.
0
 
JimBrandleyCommented:
Is there a reason you want to go from a collection to a List?
0
 
dba123Author Commented:
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.
0
 
dba123Author Commented:
a generic list is a collection itself I would think also.
0
 
dba123Author Commented:
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.
0
 
JimBrandleyCommented:
That's correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.