[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-08-11
23
Medium Priority
?
2,572 Views
Last Modified: 2010-08-05
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>
0
Comment
Question by:dba123
  • 13
  • 10
23 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677787
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677791
Are you building a list of claims or collections? If collections, then:
list.Add(Claim.GetClaimList(WebUtils.ConnectionPrefix, ContractId));

0
 
LVL 1

Author Comment

by:dba123
ID: 19677830
I'm binding to a list of Claims.  So I don't need to do any Adds
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dba123
ID: 19677832
GetClaimList returns a collection of Claim objects..or in other words records.
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677844
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
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 19677850
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677856
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
 
LVL 1

Author Comment

by:dba123
ID: 19677862
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
 
LVL 1

Author Comment

by:dba123
ID: 19677864
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
 
LVL 1

Author Comment

by:dba123
ID: 19677867
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677875
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
 
LVL 1

Author Comment

by:dba123
ID: 19677881
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19677889
We've all said that a time or two.
0
 
LVL 1

Author Comment

by:dba123
ID: 19677926
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
 
LVL 1

Author Comment

by:dba123
ID: 19677932
I wonder though if you cast like this, is it unboxing and boxing?  meaning is the cast efficient?
0
 
LVL 1

Author Comment

by:dba123
ID: 19677935
errrr

Cannot convert type 'ClaimsCollection' to 'System.Collections.Generic.List<Claim>' via a built-in conversion
0
 
LVL 1

Author Comment

by:dba123
ID: 19677936
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19678043
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19678051
Is there a reason you want to go from a collection to a List?
0
 
LVL 1

Author Comment

by:dba123
ID: 19680213
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
 
LVL 1

Author Comment

by:dba123
ID: 19680215
a generic list is a collection itself I would think also.
0
 
LVL 1

Author Comment

by:dba123
ID: 19680218
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19680220
That's correct.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question