Having a function across multiple classes

B

BobRoyAce

Let's say that I have multiple classes, representing various entities.
For example, I have Merchant, Customer, etc. Well, I have an
application where users can attach documents to many different
entities. The folder in which these documents would be stored depends
on the type of entity and, perhaps, one of the fields/properties of
the entity. So, Merchants may store their doc's under C:\Docs\Merchants
\MERCHANT_NAME, where MERCHANT_NAME is a property of a Merchant.
Customers, on the other hand, may store their doc's under C:\Docs
\Customers\CUSTOMER_NAME, where CUSTOMER_NAME is a property of a
Customer.

Well, I am creating a "generic" form that can be used to attach
documents to all of the entities that allow for this. What I need to
know how to do is create the classes such that they all have a
function, called GetAttachedDocumentsFolder, which will pass me back
the path. Then, on the form, I want to be able to call that function
regardless of the type of entity. In other words, I don't want to do
something like this:

Dim sFolder As String

Select Case Entity
Case AMerchant
Dim oMerchant As Merchant
sFolder = oMerchant.GetAttachedDocumentsFolder
Case ACustomer
Dim oCustomer As Customer
sFolder = oCustomer.GetAttachedDocumentsFolder
...
End Select

I want to just be able to do something like pass an object to the
form, let's say called oEntity, and then just be able to call
oEntity.GetAttachedDocumentsFolder. The passed in object could be any
of many different ojbect types.

How would I accomplish something like this? I am thinking that I need
to use interfaces or something...will have to look into that.
 
C

Cor Ligthert[MVP]

Bob

For that is mostly used a collection class that is a property of all your
other classes.

If you see by instance control.controls, then is controls just a collection
(wherefor in this case probably collectionbase is used), to which you can
add all kind of information. I still like collectionbase to use probably
because of the fact that you can copy the sample on this page and then use
it direct. However you can use in fact any type of collection including
generics.

http://msdn2.microsoft.com/en-us/library/system.collections.collectionbase.aspx

Don't forget to change the int16 to integer or if you want to be futured on
more newer computers int64, because int16 is on computers with registers
larger than 16 bit sligthly inefficient.

Cor
 
J

Jack Jackson

Let's say that I have multiple classes, representing various entities.
For example, I have Merchant, Customer, etc. Well, I have an
application where users can attach documents to many different
entities. The folder in which these documents would be stored depends
on the type of entity and, perhaps, one of the fields/properties of
the entity. So, Merchants may store their doc's under C:\Docs\Merchants
\MERCHANT_NAME, where MERCHANT_NAME is a property of a Merchant.
Customers, on the other hand, may store their doc's under C:\Docs
\Customers\CUSTOMER_NAME, where CUSTOMER_NAME is a property of a
Customer.

Well, I am creating a "generic" form that can be used to attach
documents to all of the entities that allow for this. What I need to
know how to do is create the classes such that they all have a
function, called GetAttachedDocumentsFolder, which will pass me back
the path. Then, on the form, I want to be able to call that function
regardless of the type of entity. In other words, I don't want to do
something like this:

Dim sFolder As String

Select Case Entity
Case AMerchant
Dim oMerchant As Merchant
sFolder = oMerchant.GetAttachedDocumentsFolder
Case ACustomer
Dim oCustomer As Customer
sFolder = oCustomer.GetAttachedDocumentsFolder
...
End Select

I want to just be able to do something like pass an object to the
form, let's say called oEntity, and then just be able to call
oEntity.GetAttachedDocumentsFolder. The passed in object could be any
of many different ojbect types.

How would I accomplish something like this? I am thinking that I need
to use interfaces or something...will have to look into that.

You can either inherit each of the entity classes from a base class
that has a Public Overridable Function GetAttachedDocumentFolder(), or
you could define an interface and have all of the entity classes
implement that interface.
 
P

Phill W.

BobRoyAce said:
Let's say that I have multiple classes, representing various entities.
For example, I have Merchant, Customer, etc. Well, I have an
application where users can attach documents to many different
entities. The folder in which these documents would be stored depends
on the type of entity and, perhaps, one of the fields/properties of
the entity. So, Merchants may store their doc's under C:\Docs\Merchants
\MERCHANT_NAME, where MERCHANT_NAME is a property of a Merchant.
Customers, on the other hand, may store their doc's under C:\Docs
\Customers\CUSTOMER_NAME, where CUSTOMER_NAME is a property of a
Customer.

So each class needs to "know" where it can store it's "own" documents?
That smacks of an Overridable Property:

Public ReadOnly Property GetAttachedDocumentsFolder() _
as String

Get
Return Path.Combine( m_sClassName, m_sName )
End Get

End Property

Private m_sClassName = "Merchants"
Private m_sName as String = Nothing
Then, on the form, I want to be able to call that function regardless
of the type of entity. In other words, I don't want to do

Total "Type-independence" in code is usually difficult to achieve and
inevitably comes back to bite you when it goes wrong later on.
It's just not what .Net's about; it wants to know what Type everything
is so that it can [try and] stop you doing things that are downright wrong.
In other words, I don't want to do something like this:
Dim sFolder As String

Select Case Entity
Case AMerchant
Dim oMerchant As Merchant
sFolder = oMerchant.GetAttachedDocumentsFolder
Case ACustomer
Dim oCustomer As Customer
sFolder = oCustomer.GetAttachedDocumentsFolder
...
End Select

If your Merchant and Customer classes both inherit from some "base"
class, then that class should define the overridable Property. This
makes your Form code:

Dim sFolder as String

If Typeof Entity Is BaseClass Then
sFolder = DirectCast(Entity, BaseClass).GetAttachedDocumentsFolder
End If
I want to just be able to do something like pass an object to the
form, let's say called oEntity, and then just be able to call
oEntity.GetAttachedDocumentsFolder. The passed in object could be any
of many different ojbect types.

If there's /no/ common base class, then consider an Interface:

Public Interface IDocFolder
ReadOnly Property GetAttachedDocumentsFolder() as String
End Interface

Class Customer
Implements IDocFolder

Public ReadOnly Property GetAttachedDocumentsFolder() _
as String _
Implements IDocFolder.GetAttachedDocumentsFolder

Get
Return Path.Combine( m_sClassName, m_sName )
End Get
End Property

Private m_sClassName = "Merchants"
Private m_sName as String = Nothing

End Class

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top