Solved

Pass collection base class between tiers

Posted on 2008-11-02
8
294 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Suggested Solutions

Title # Comments Views Activity
Help with converting xml file to excel using VB.NET 3 31
Write to a printer using vb.net 9 59
VB.NET 2008 - Windows Protected Your PC 7 54
Passing data between Forms 3 28
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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