Solved

Pass collection base class between tiers

Posted on 2008-11-02
8
292 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:JRockFL
  • 4
  • 2
  • 2
8 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22863511
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.
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 300 total points
ID: 22863545
Think of having a Base Class Animal -  Dog inherits from Animal, and Cat Inherits from Animal, but you cannot convert a Dog into a Cat.

This would possibly work:

   Public Shared Function GetAll() As CollectionBase
        Dim uc As New ORM.DataAccess.UserCollection
        uc.Load()
        Return uc
    End Function

You would then need to cast the return value when GetAll is called, to convert the result back into a  ORM.DataAccess.UserCollection (since a Collection base, or something that inherits from CollectionBase) is being returned.

AW


0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22863557
Exactly, AW's description hopefully makes the point alot clearer for you, JRockFL.
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 8

Author Comment

by:JRockFL
ID: 22863614
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

0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 total points
ID: 22863681
You can pass the datareader OR to keep the layers separated more, you can use the DataReader to fill a DataTable and pass back the generic System.Data.DataTable versus the data reader object which to my knowledge is usually implementation specific like SqlDataReader or OleDbDataReader.   Since you can build a DataTable manually, this is more flexible and OO like. :)
0
 
LVL 8

Author Closing Comment

by:JRockFL
ID: 31512543
Thanks for the help!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22864282
Glad to be of assistance.

AW
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22864309
Diddo. :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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