Reputation: 2504
My brain is fried at the moment, so here's the scenario.
I have a form that adds a member to my VB.NET application.
When I press submit I add their details: name and number as well as what products they want (internet and phone). Internet and Phone are their own class as is member.
I want to add that they HAVE whichever they have chosen and with the member store it.
EDIT:
So it looks like this :
MEMBER : Name- Bob Number- 118 Phone=True Internet = False
That would be one record.
What can I store all 3 in so there is a reference to all 3 of them? It'driving me insane.
I originally had an array which just had true or false for whether they were there, but that was before I implemented classes.
I was also using a collection to store the members, which would contain everything pretty much.
Any ideas?
As what happens is once the member is created (stored) I will go to input how much they have used, and then store that. But to do so I will need access to the internet and phone classes, which are associated with said member.
Upvotes: 0
Views: 2132
Reputation: 44931
There are two parts to this problem:
The typical scenario for storage in the database is that Member is the parent record and Internet and Phone are child records, either stored in the same table or in different tables.
My usual preference is to store contact information in a single table with a Type field that delineates the type of data stored in the record and a single field that holds the content associated with the record (i.e. web address, email address, etc.).
In this design, you would have a foreign key reference to the Member primary key in the Contacts table and the Member primary key should not be anything related to the content of the Member record.
For example:
CREATE TABLE Members
(
/* This table contains current information on a given Member */
/* The unique id for this record */
RecordId INTEGER NOT NULL IDENTITY(1,1) ,
/* The Member's first name */
FirstName NVARCHAR(50) NOT NULL ,
/* The Member's middle name */
MiddleName NVARCHAR(50) NULL ,
/* the Member's last name */
LastName NVARCHAR(50) NOT NULL ,
CONSTRAINT PK_Members PRIMARY KEY
(
RecordId
)
)
CREATE TABLE Contacts
(
/* The unique id for this record */
RecordId INTEGER NOT NULL IDENTITY(1,1) ,
/* The member that this contact is associated with */
MemberId INTEGER NOT NULL ,
/* The type of contact. Current values are:
1 - Internent
2 - Phone
*/
Type INTEGER NOT NULL ,
/* The contact details */
Reference NVARCHAR(MAX) NOT NULL ,
CONSTRAINT PK_Contacts PRIMARY KEY
(
RecordId
) ,
/* Referential integrity notes:
If the member is deleted, all associated records in this table will be automatically deleted
*/
CONSTRAINT FK_Members_C FOREIGN KEY (
MemberId
) REFERENCES Members (
RecordId
) ON DELETE CASCADE
)
For storing the information in the application I personally prefer mirroring the tables in the database with classes in the application since that is easier to maintain and more extensible:
Public Class Member
Public Property RecordId As Integer
Public Property FirstName As String
Public Property MiddleName As String
Public Property LastName As String
Public Property Contacts As New System.Collections.Generic.Dictionary(Of Integer, Contact)
End Class
Public Class Contact
Public Property RecordId As Integer
Public Property MemberId As Integer
Public Enum Types
None
Internet
Phone
End Enum
Public Property Type As Types
Public Property Reference As String
End Class
But you could also manufacture instances of existing classes based on the type as the data is being read from the database and store them in the member.
Upvotes: 0
Reputation: 4598
One solution is to define an interface that defines common properties of your classes. For example, define a Selectable
with a getType()
method. This interface is implemented by both Phone
and Internet
. You could then maintain a collection of Selectable
objects per user and build your rows by iterating over each user's collection and determining which types of Selectables are present.
Upvotes: 1