Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to re-write this code 2

Status
Not open for further replies.

purplehaze1

Programmer
Jul 23, 2003
86
0
0
US
A customer can have multiple insurances. So in user interface,
I have 4 checkboxes representing multiple insurance types.
If any box is checked, first I see if the checked insurance type
already exists in the collection before I create a new one. The following code works but is rather long when I check for each insurancetype in the collection,
I was wondering if there's more efficient way to do it. Examples would be greate.
Thanks.


Private Sub SaveInsuranceTypes()
Dim BlueCross, MCC, Medicaid As InsuranceType
Dim Medicare, NoCoverage As InsuranceType

BlueCross = oRequest.InsuranceTypes.getInsuranceByType(oInsurance.InsuranceType.BlueCross)
If BlueCross Is Nothing Then
If chkBlueCross.Checked Then
BlueCross = New InsuranceType()
With BlueCross
.InsuranceTypeID = .InsuranceTypes.BlueCross
End With
ocustomer.InsuranceTypes.add(BlueCross)
End If
Else
If Not chkBlueCross.Checked Then
BlueCross.delete()
End If
End If

MCC = oRequest.InsuranceTypes.getInsuranceByType(oInsurance.InsuranceType.ManagedCareCommercial)
If MCC Is Nothing Then
If chkManagedCare.Checked Then
MCC = New InsuranceType()
With MCC
.InsuranceTypeID = .InsuranceTypes.MCC
End With
ocustomer.InsuranceTypes.add(MCC)
End If
Else
If Not chkManagedCare.Checked Then
MCC.Delete()
End If
End If

.....(same for other insuranceTypes)
.....

End Sub
 
Some remarks:
1. If you use one checkbox for each type, what will you do when you'll hav 20 types? I used CheckListBox. Not a briliant control, but fine.

2. If you are developing a windows application, react to each check done by the user and change the collection. When the user press save, save it to somewhere.

3. I used CollectionBase to create a strongly typed collection for InsuranceType. If you need to implement more collection functions see:

4. My code is not the best in object oriented and you have a lot work to do, but it's a start.

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private m_Customer As Customer

#Region " Windows Form Designer generated code "

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
    Friend WithEvents btnSave As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
        Me.btnSave = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'CheckedListBox1
        '
        Me.CheckedListBox1.CheckOnClick = True
        Me.CheckedListBox1.Location = New System.Drawing.Point(48, 32)
        Me.CheckedListBox1.Name = "CheckedListBox1"
        Me.CheckedListBox1.Size = New System.Drawing.Size(216, 169)
        Me.CheckedListBox1.TabIndex = 0
        '
        'btnSave
        '
        Me.btnSave.Location = New System.Drawing.Point(160, 224)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(80, 24)
        Me.btnSave.TabIndex = 1
        Me.btnSave.Text = "save"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.btnSave)
        Me.Controls.Add(Me.CheckedListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        m_Customer = New Customer

        'Add any initialization after the InitializeComponent() call

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim InsuranceTypes() As InsuranceType = [Enum].GetValues(GetType(InsuranceType))

        For Each tmpInsuranceType As InsuranceType In InsuranceTypes

            CheckedListBox1.Items.Add(tmpInsuranceType, m_Customer.Insurances.Contains(tmpInsuranceType))

        Next

    End Sub

    Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck

        Dim tmpInsuranceType As InsuranceType

        tmpInsuranceType = [Enum].Parse(GetType(InsuranceType), CStr(CheckedListBox1.Items(e.Index)))

        If e.NewValue = CheckState.Checked Then

            m_Customer.Insurances.Add(tmpInsuranceType)

        Else

            m_Customer.Insurances.Remove(tmpInsuranceType)

        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        ' Save the data to database, file etc...

    End Sub

End Class

Public Enum InsuranceType

    BlueCross = 1
    MCC = 2
    Medicaid = 3
    Medicare = 4
    NoCoverage = 5

End Enum

Public Class InsuranceCollection
    Inherits CollectionBase

    Public Function Add(ByVal value As InsuranceType) As Integer

        Return List.Add(value)

    End Function

    Public Function Contains(ByVal value As InsuranceType) As Boolean

        Return List.Contains(value)

    End Function

    Public Sub Remove(ByVal value As InsuranceType)

        List.Remove(value)

    End Sub

End Class

Public Class Customer

    Private m_Insurances As InsuranceCollection

    Public Sub New()

        m_Insurances = New InsuranceCollection

    End Sub

    Public ReadOnly Property Insurances() As InsuranceCollection
        Get
            Return m_Insurances
        End Get
    End Property

End Class
 
Take a step back and look at all the places where you have cut and pasted code. This is a 'bad code smell'. Anything that you do more than once is fair game for refactoring out to a helper method. And the bit where you
Code:
BlueCross = New InsuranceType()
                With BlueCross
                    .InsuranceTypeID = .InsuranceTypes.BlueCross
                End With
could be streamlined by adding a constructor for the InsuranceType class that takes one of the InsuranceTypes.* as a parameter.

Rather than having named variables for BlueCross etc., just use a collection - each object in the collection knows what kind of insurance it is.

korach's enumeration of insurance types is a good idea, or put them in a config file or even on a database. They need to be somewhere where they can be returned as a collection, not a set of named variables.

So you can get a collection of the names and values in the Enum, and then iterate over the collection processing each one.

Then when you want to add a new one, just amend the Enum, and it all still works.

One other thing - is 'NoCoverage' a brand name for a type of insurance, or does it mean that they don't have any? If it's the latter, then it's not really an insurance type.
 
Thanks korach and stevexff for your valuable comments.

listcheckbox is good idea, didn't realize. :)
Like steve said, I have insurance types in the database.
And insurance class has enum type insurancetypes with the same value as in the database. Following korach's eg in the form load event then, how then can I do the same as below if I have enum declared in insurance class?

i.e.
Dim InsuranceTypes() As InsuranceType = [Enum].GetValues(GetType(InsuranceType))

Steve's suggestion using parameter in contructor is good idea. I don't know how to do the following though I understand what you mean, can you give an example? That would be great. Thanks much.

--------
So you can get a collection of the names and values in the Enum, and then iterate over the collection processing each one.

Then when you want to add a new one, just amend the Enum, and it all still works.



 
Enum has a GetNames method that returns an array of the names in the Enum, and GetValues that does the same for the values. Looking at the class documentation, you can actually do quite a lot of cool[sup]1[/sup] stuff with Enums besides just using them as handy named constants.

[sup]1[/sup] or at least mildly interesting...
 
You guys are great.
Like Steve said, I populated collection from the db and iterated through it to populate the checklistbox like korach suggested. However checklistbox displays all items as ProgramName.InsuranceType rather than insurance type names. What am I missing here?
Thanks again.

'User interface

Private Sub populateInsuranceType()

Dim InsuranceTypes As InsuranceTypes
InsuranceTypes = oInsuranceType.loadInsuranceType
InsuranceTypeList.Items.Clear() 'checklist
For Each InsuranceType as insuranceType In InsuranceTypes
InsuranceTypeList.Items.Add(InsuranceType, m_Customer.Insurances.Contains(InsuranceType)))
Next
End Sub


Public Class InsuranceType

Public Function loadInsuranceType() As InsuranceTypes
' Populates data from database into the collection
Try
Dim transplant As InsuranceType
Dim strSQL As String
Dim dr As DataRow
Dim oInsuranceType As InsuranceType
Dim InsuranceTypes As New InsuranceTypes()

strSQL = "SELECT * from t_insurance_type"
Dim da As New OleDbDataAdapter(strSQL, myConnection)
Dim ds As New DataSet()

da.Fill(ds, "InsuranceType")

For Each dr In ds.Tables(0).Rows
oInsuranceType = Nothing
oInsuranceType = New InsuranceType()
With oInsuranceType
.InsuranceTypeID = IIf(dr.IsNull("insurance_type_id"), Nothing, dr("insurance_type_id"))
.InsuranceTypeCode = IIf(dr.IsNull("insurance_type_cde"), Nothing, dr("insurance_type_cde"))
.IsDirty = False
.IsNew = False
End With

InsuranceTypes.AddExisting(oInsuranceType)
Next

Return InsuranceTypes
Catch e As Exception

End Try
End Function

End Class


Public Class InsuranceTypes
inhertis system.collections.collectionBase

Public Function AddExisting(ByVal value As InsuranceType) As Boolean
Try
list.Add(value)
Return True
Catch E As Exception
Return False
End Try
End Function

Public Function Contains(ByVal value As InsuranceType) As Boolean

Return List.Contains(value)

End Function
 
Got the items showing on the checklistbox working this way:

Private Sub populateInsuranceType()

Dim InsuranceTypes As InsuranceTypes
InsuranceTypes = oInsuranceType.loadInsuranceType
InsuranceTypeList.Items.Clear() 'checklist
For Each InsuranceType as insuranceType In InsuranceTypes
InsuranceTypeList.Items.Add(InsuranceType).ToString()
Next
End Sub


Public Class InsuranceType

Public Function loadInsuranceType() As InsuranceTypes
' Populates data from database into the collection
Try
Dim transplant As InsuranceType
Dim strSQL As String
Dim dr As DataRow
Dim oInsuranceType As InsuranceType
Dim InsuranceTypes As New InsuranceTypes()

strSQL = "SELECT * from t_insurance_type"
Dim da As New OleDbDataAdapter(strSQL, myConnection)
Dim ds As New DataSet()

da.Fill(ds, "InsuranceType")

For Each dr In ds.Tables(0).Rows
oInsuranceType = Nothing
oInsuranceType = New InsuranceType()
With oInsuranceType
.InsuranceTypeID = IIf(dr.IsNull("insurance_type_id"), Nothing, dr("insurance_type_id"))
.InsuranceTypeCode = IIf(dr.IsNull("insurance_type_cde"), Nothing, dr("insurance_type_cde"))
.IsDirty = False
.IsNew = False
End With

InsuranceTypes.AddExisting(oInsuranceType)
Next

Return InsuranceTypes
Catch e As Exception

End Try
End Function

Public Overrides Function ToString() As String
Return m_transplantTypeCode
End Function

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top