Class Association in VB.Net

241 views
Skip to first unread message

Dani

unread,
Jan 4, 2012, 4:24:43 PM1/4/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi all,
I'm programming an application with OOP. I've got a one to many
relationship between 2 tables, and I read this can be implemented thru
class association.

I've got 2 tables: Customers and Budgets. One Cutomer may have
multiple Budgets.

In the VS Class Diagram, I tried to create the association but I need
to define the property Budget in class Customer, as a list, otherwise
it wouldn't let me do it.

Any ideas about this?

Thanks a lot in advance.

Daniela

Cerebrus

unread,
Jan 10, 2012, 1:33:57 AM1/10/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
That's perfectly fine. Each Customer has a List of Budgets exposed
through a property of the Customer class.

Stephen Russell

unread,
Jan 10, 2012, 12:51:25 PM1/10/12
to dotnetde...@googlegroups.com
-----------------------------------------


Think of a container, Customer, that holds other containers like
List<Budget> and List<Orders>.


--
Stephen Russell

901.246-0159 cell

Daniela Zurita

unread,
Jan 11, 2012, 7:57:25 AM1/11/12
to dotnetde...@googlegroups.com
Thank you for your answers, Stephen and Cerebrus!
But now, could you please give me an example of definition and how to implement the classes? 
I've got an example and there are points i don'get. here my questions.

Side 1
Class Customer 
Friend Sub SetBudget(ByVal budget As Budget)
_budget = budget
End Sub
 
Public Property Budget() As Budget
Get
  Return _budget
End Get
Set(ByVal Value As Budget)
If Not _budget Is Nothing Then
_budget.Customers_with_budget.Delete(Me)
End If
_distribuidora = Value
If Not _budget Is Nothing Then
_budget. .Customers_with_budget.Append(Me)
End If
End Set
End Property
'what about add and delete methods? when I delete a customer shoud I delete a budget? Don't know how to do this

Side * 
'here I create a collection of customers with budget
Public Class Customers_with_budget
Implements IEnumerable
Private _budget As Budget
Private _htCustomers_with_budget As Hashtable
Friend Sub New(ByVal  budget As Budget )
_budget = budget
_htCustomers_with_budget = New Hashtable
End Sub
End Class

Public Sub Add(ByVal customer As Customer)
If Not _ht Customers_with_budget.Contains(customer) Then
_htCustomers_with_budget.Add(customer.Name, ... ,Customer) 'here I add other properties of a customer like address, contact number, etc
customer.setBudget(_budget)
Else
Throw New System.Exception("Already existing Customer")
End If
End Sub
Friend Sub Append(ByVal customer As Customer)
_htCustomers_with_budget.Add(customer.Name, ..., customer)
End Sub

Public Sub Remove(ByVal customer As Customer)
customer.SetBudget(Nothing) 'remove opposite reference
Me.Delete(customer)  'delete customer from collection
End Sub

Public Sub Remove(ByVal name As String)
'Delete opposite reference
Item(name).SetBudget(Nothing)
Me.Delete(Item(name)) ‘remove from collection
End Sub
Friend Sub Delete(ByVal customer As Customer)
_htCustomers_with_budget.Remove(customer.Name) 'what about other elements in customer class like address and contact number?
End Sub

'Clear method is called from Dispose method in Budget class and deletes elements of collection class 
Public Sub Clear()
Dim en As IDictionaryEnumerator
Dim name As String
en = _htCustomers_with_budget.GetEnumerator
While en.MoveNext()
name = CType(en.Key, String) 'i don't get this Ctype function
Me.Remove(name)
End While
_htCustomers_with_budget.Clear()
End Sub

and next question is how do I implement the collection class with table Budget? 

thanks a million!
Dani 



--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Cerebrus

unread,
Jan 12, 2012, 8:53:51 AM1/12/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Daniela,

I have posted an example that illustrates how to create the classes
and some sample usage at:

http://pastebin.com/cDmwxZL9

I have tried to explain via comments where possible, but feel free to
ask if you have any doubts. I hope you are familiar with VB 9.0
syntax. A couple of questions in your post remain unanswered and I try
to address them below:

1. What about Add/Delete? when I delete a customer shoud I delete a
budget? Don't know how to do this.

Add, Delete are implemented in my example. When you delete a Customer
instance, associated budget instances are automatically deleted.

2. What about other elements in customer class like address and
contact number?
Those elements get added/deleted/modified when the Customer instance
is added/deleted/modified.

You need to implement custom collection classes only if you are using
a version of .NET prior to 2.0 (pre-VS 2005) or the existing
collection classes do not suffice for your requirement. In this case,
I believe that the existing generic collections do suffice.

Another point is that I have loaded data into the instances manually.
In a real-world scenario, you would load the data from database
tables. Ideally, the customer's budgets would be loaded when a
customer is created (for instance, by initiating a database call
within the Customer class constructor).

On Jan 11, 5:57 pm, Daniela Zurita <danielaazur...@gmail.com> wrote:
> Thank you for your answers, Stephen and Cerebrus!
> But now, could you please give me an example of definition and how to
> implement the classes?
> I've got an example and there are points i don'get. here my questions.
>
>
> thanks a million!
> Dani
>

Cerebrus

unread,
Jan 13, 2012, 1:58:34 AM1/13/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is another example that was intended to showcase Generic methods,
but also illustrates the class association concept:

http://pastebin.com/w7e1Jn6J

Daniela Zurita

unread,
Jan 13, 2012, 8:29:10 AM1/13/12
to dotnetde...@googlegroups.com
Cerebrus, thank you very much for this information, really apreciate it. I'll implement my class association according to your examples and see how I go.
kind regards,
Daniela

Cerebrus

unread,
Jan 16, 2012, 3:05:14 AM1/16/12
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Good luck !

Let us know if you face any hurdles. :-)
Reply all
Reply to author
Forward
0 new messages