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(WebUtil s.Connecti onPrefix, ContractId);
Error: Cannot implicitly convert type 'ClaimsCollection' to 'System.Collections.Generi c.List<Cla imsCollect ion>
List<ClaimsCollection> list = new List<ClaimsCollection>();
list = Claim.GetClaimList(WebUtil
Error: Cannot implicitly convert type 'ClaimsCollection' to 'System.Collections.Generi
Are you building a list of claims or collections? If collections, then:
list.Add(Claim.GetClaimLis t(WebUtils .Connectio nPrefix, ContractId));
list.Add(Claim.GetClaimLis
ASKER
I'm binding to a list of Claims. So I don't need to do any Adds
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(WebUtil s.Connecti onPrefix, 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(WebUtil s.Connecti onPrefix, 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.
ClaimsCollection claims = Claim.GetClaimList(WebUtil
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(WebUtil
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
AType a = b AS AType;
will return a valid pointer or null, but will not throw an exception.
Jim
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(re ader);
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(WebUtil s.Connecti onPrefix, ContractId);
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(re
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(WebUtil
ASKER
whoops or in other words per my last example would be more like :
List<Claims> list = new List<Claims>();
list = Claim.GetClaimList(WebUtil s.Connecti onPrefix, ContractId);
List<Claims> list = new List<Claims>();
list = Claim.GetClaimList(WebUtil
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(WebUtil s.Connecti onPrefix, ContractId) AS List<Claim>;
claims = Claim.GetClaimList(WebUtil
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.
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!
ASKER
I wonder though if you cast like this, is it unboxing and boxing? meaning is the cast efficient?
ASKER
errrr
Cannot convert type 'ClaimsCollection' to 'System.Collections.Generi c.List<Cla im>' via a built-in conversion
Cannot convert type 'ClaimsCollection' to 'System.Collections.Generi
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.Conn ectionPref ix, ContractId) as List<Claim>;
// 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.Conn
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?
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.
ASKER
a generic list is a collection itself I would think also.
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.
ClaimsCollection claims = Claim.GetClaimList(WebUtil
for each (ClaimItem claimItem in claims)
{
list.Add(claim);
}