Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Pass collection base class between tiers

I have two classes that inherit from CollectionBase
One is in the dataaccess class library and the other is in the business library.
I get this complier error when I try to return the object through the tiers.
value of type orm.dataaccess.usercollection can not be converted to orm.business.usercollection
 
   Public Shared Function GetAll() As ORM.Business.UserCollection
        Dim uc As New ORM.DataAccess.UserCollection
        uc.Load()
        Return uc
    End Function

Why can't I do this? What should I do?
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Even if the code in the two classes are the same, the system will think of these as two different classes.
You are dimensioning uc as ORM.DataAccess.UserCollection then passing back ORM.BusinessCollection.UserCollection.  That would work if the former is a subclass of the latter.
You can either pass back the former OR CollectionBase (whatever class you extended).
When you add reference one layer from the other, you can use class from within the other if that is what is being returned to NOT have two classes OR only pass back and forth the public interface like CollectionBase and only use the other classes for implementation specific properties/methods within each layer.
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
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
Exactly, AW's description hopefully makes the point alot clearer for you, JRockFL.
Avatar of JRockFL

ASKER

Thanks for the replies. Yes that does make sense AW.

After thinking about, I don't think I would need to pass the collectionbase class between the tiers. Would it make more sense to pass a datareader?

What I am trying to do is move my current architecture to a 3 tier.
Here is a sample of code that executes a reader and then adds the user objects  to the class that inherits from collectionbase.


			Try
				reader = _dp.ExecuteReader("Users_Collection", hsh)
				If Not reader Is Nothing And Not reader.IsClosed Then
					While reader.Read()
							Dim objUser As New User
							If Not reader.IsDBNull(0) Then
								objUser.UserID = reader.GetInt32(0)
							End If
							If Not reader.IsDBNull(1) Then
								objUser.UserName = reader.GetString(1)
							End If
							If Not reader.IsDBNull(2) Then
								objUser.Password = reader.GetString(2)
							End If
						List.Add(objUser)
						objUser = Nothing
					End While
					reader.Close()
				End If
			Catch ex As Exception
				Throw ex
			End Try

Open in new window

SOLUTION
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
Avatar of JRockFL

ASKER

Thanks for the help!
Glad to be of assistance.

AW
Diddo. :)