?
Solved

Pass collection base class between tiers

Posted on 2008-11-02
8
Medium Priority
?
297 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
[X]
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
  • 4
  • 2
  • 2
8 Comments
 
LVL 60

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 1200 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 60

Expert Comment

by:Kevin Cross
ID: 22863557
Exactly, AW's description hopefully makes the point alot clearer for you, JRockFL.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 800 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 60

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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