Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pass collection base class between tiers

Posted on 2008-11-02
8
293 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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.
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…

839 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