Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project is almost done!
For now I have this question:

My code is reading line by line and the result is showing in another form within my app. That's ok, but only one thing that I don't know how to change.

VB
Case "TEL;WORK;VOICE"                               'Telefoon op het werk
                            strTelWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)



The Code above: The line "TEL;WORK..etc is 2 times in the file..
Now I want to delete the line if the line is in that file already.. so if the line exist than delete and go to next line.

This is the code:
VB
Private Sub Importeren() _
        Handles itemImporteerVCard.Click, mnuImportVCard.Click, btnImportVCard.Click 'Alle knoppen voor het importeren.

        '------------------------------------------------------------------------
        ' Onderstaande code doet het wel, maar ik moet nog wat dingen uitzoeken.
        'Mijn originele code:
        '------------------------------------------------------------------------
        Dim strBestand As String = String.Empty                 'Locatie + naam van het bestand
        Dim strRegel As String = String.Empty                   'Regel/lijn
        Dim strWaarde() As String = Nothing                     'de inhoud van de regels/lijnen

        Dim objReader As System.IO.StreamReader = Nothing, sbBuilder As System.Text.StringBuilder = Nothing


        'Variabelen:
        '--------------------------------
        Dim strNaam As String = ""
        Dim strVoornaam As String = ""
        Dim strTelWerk As String = ""
        Dim strTelThuis As String = ""
        Dim strMobiel As String = ""
        Dim strOverig As String = ""
        Dim strFaxWerk As String = ""
        Dim strFaxThuis As String = ""
        Dim strAdres As String = ""
        Dim strUrlThuis As String = ""
        Dim strUrlWerk As String = ""
        Dim strMail As String = ""

        Dim boolFlag As Boolean = false ' *****


        'Bevestiging voor het importeren:
        If MsgBox("Wilt u een v-Card Importeren?", 36, "Bevestiging voor het importeren") = MsgBoxResult.Yes Then
            Try
                strBestand = "C:\Test.vcf"                                  'Verwijst naar de locatie van het bestand
                sbBuilder = New System.Text.StringBuilder                   'Er is een nieuwe stringbuilder gemaakt

                frmVCard.txtVCard.Text = strBestand                         'frmVcard.txtVcard.Text moet gevuld worden met de inhoud van het bestand
                objReader = New System.IO.StreamReader(strBestand)

                'Eigenschappen voor het openen van de V-Card:
                ofdImport.Filter = "V-Cards (*.vcf)|*.vcf"                  'Filter betreft de extensie van het bestand *.vcf
                ofdImport.FileName = "*.vcf"                                'Bestandsnaam: alles.vcf
                ofdImport.Title = "V-Card importeren"                       'Titel van het dialoogvenster voor het openen

                If ofdImport.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    objReader = New System.IO.StreamReader(strBestand)

                    frmVCard.Show()                                         'Laat de form zien waar de Vcard in terecht komt
                End If

                Do While objReader.Peek >= 1
                    strRegel = objReader.ReadLine()                         'Elke regel/lijn wordt 1 voor 1 gelezen
                    strWaarde = strRegel.Split(":")                         'De waarde van elke regel wordt gesplits d.m.v. ":"

                    Select Case UCase(strWaarde(0))                         '(0) = het gedeelte voor ":" en (1) komt daarna

                        Case "BEGIN", "VERSION", "END"                      'De waarden die niet worden getoond.

                        Case "N"                                            'Naam
                            strNaam = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "FN"                                           'Voornaam
                            strVoornaam = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;WORK;VOICE"                               'Telefoon op het werk
                            If boolFlag = false  ' *****
                            Then                 ' *****
                                strTelWerk = strWaarde(1)
                                sbBuilder.Append(strWaarde(1) & vbTab)
                                boolFlag = true  ' *****
                            End If               ' *****

                        Case "TEL;HOME;VOICE"                               'Telefoon thuis
                            strTelThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;CELL;VOICE"                               'Mobiele telefoon
                            strMobiel = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "TEL;VOICE"                                    'Overige nummers
                            strOverig = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "TEL;WORK;FAX"                                 'Fax op het werk
                            strFaxWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "TEL;FAX"                                      'Fax thuis
                            strFaxThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "ADR;WORK;PREF:"                               'Adres
                            strAdres = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "URL;HOME:"                                    'Website Thuis
                            strUrlThuis = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbTab)

                        Case "URL;WORK:"                                    'Website Werk
                            strUrlWerk = strWaarde(1)
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                        Case "EMAIL;PREF;INTERNET:"                         'Email adres
                            strMail = strWaarde(1)
                            sbBuilder.Append(strWaarde(1))

                        Case Else
                            sbBuilder.Append(strWaarde(1) & vbCrLf)

                    End Select
                Loop
                objReader.Close()
                frmVCard.txtVCard.Text = sbBuilder.ToString

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fout...")

            Finally
                objReader.Dispose()
                sbBuilder = Nothing
            End Try
        End If
    End Sub

I hope anyone can help me out.
Thanks already!
Posted
Updated 4-Jun-12 2:06am
v3

1 solution

Use a flag or some other element and set it to false. Then within your case clause check if the flag is false, and if so save the information and set the flag to true. If the flag is true, nothing will happen so you ignore the duplicate.
 
Share this answer
 
Comments
OdeJong 4-Jun-12 7:16am    
sorry.. I don't understand it..
can you give me an example?
Richard MacCutchan 4-Jun-12 7:18am    
bool flag = false;
...
case "xx":
if (flag == false)
{
// save the information
flag = true;
}
OdeJong 4-Jun-12 7:49am    
stil dont understand, I make it with VB.net
Richard MacCutchan 4-Jun-12 7:54am    
You're a developer, and you cannot understand this simple piece of logic? I suggest you get your programming guides out and spend the rest of the day studying.
OdeJong 4-Jun-12 8:00am    
sorry, i'm a beginner.. just learning it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900