Solved

Pass collection base class between tiers

Posted on 2008-11-02
8
291 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
Comment Utility
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
Comment Utility
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
Comment Utility
Exactly, AW's description hopefully makes the point alot clearer for you, JRockFL.
0
 
LVL 8

Author Comment

by:JRockFL
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 total points
Comment Utility
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
Comment Utility
Thanks for the help!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
Glad to be of assistance.

AW
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Diddo. :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now