Array advice


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Array advice

  1. #1
    Join Date
    Sep 2004
    Location
    Northampton,United Kingdom
    Posts
    230

    Exclamation Array advice

    Hi all,

    I have an array that has the following data


    501837430761
    501837430762
    501837430763
    501837430764
    501837430765
    501837430766
    501837430766
    Void,1,501837430766
    501837430767
    501837430768
    501837430769

    What I need to do is when ever it read's the Void,1,501837430766 line i would like to remove the number (1) from the array (in this case it would remove the extra 5018374320766 from the list and then removes the Void line.

    I have been trying to figure this out for the last 7hrs with no good.

    The following is what I have come up with so far

    Code:
    Public DataStorage      As String
    Public LineData         As String
    Public FreeNumber       As String
     
    Private Sub Command1_Click()
        FreeNumber = FreeFile
        Open "c:\Data1.dat" For Input As #FreeNumber
            Do While Not EOF(FreeNumber)
                Input #FreeNumber, LineData
                List1.AddItem LineData
            Loop
        Close FreeNumber
        
     
        Dim k As Long
        With List1
            For k = .ListCount - 1 To 0 Step -1
                If InStr(1, .List(k), "VOID", vbTextCompare) > 0 Then
                    .RemoveItem (k)
                    .RemoveItem (k - 1)
                End If
            Next
        End With
    End Sub
    
    Private Sub Command2_Click()
    FreeNumber = FreeFile
    
        Open "c:\SKUData.dat" For Output As #FreeNumber
            Print #FreeNumber, DataStorage
        Close FreeNumber
    End Sub
    
    Private Sub Form_Load()
        DataStorage = ""
        DataStorage = DataStorage & "5018374320761" & vbNewLine
        DataStorage = DataStorage & "5018374320762" & vbNewLine
        DataStorage = DataStorage & "5018374320763" & vbNewLine
        DataStorage = DataStorage & "5018374320764" & vbNewLine
        DataStorage = DataStorage & "5018374320765" & vbNewLine
        DataStorage = DataStorage & "5018374320766" & vbNewLine
        DataStorage = DataStorage & "5018374320766" & vbNewLine
        DataStorage = DataStorage & "Void,1,5018374320766" & vbNewLine
        DataStorage = DataStorage & "5018374320767" & vbNewLine
        DataStorage = DataStorage & "5018374320768" & vbNewLine
        DataStorage = DataStorage & "5018374320769" & vbNewLine
        DataStorage = DataStorage & "5018374320770" & vbNewLine
        DataStorage = DataStorage & "5018374320771" & vbNewLine
        DataStorage = DataStorage & "5018374320772" & vbNewLine
        DataStorage = DataStorage & "5018374320773" & vbNewLine
        DataStorage = DataStorage & "5018374320774" & vbNewLine
        DataStorage = DataStorage & "5018374320775" & vbNewLine
        DataStorage = DataStorage & "5018374320776" & vbNewLine
    End Sub
    if someone could help I would BE FOREVER greatfull as I think this
    has given me a nervous brakedown (After I have finished this project I am SOO having a Vacation)

    Thanks for all your advice in advance guys & Gals

    Drew
    Drew

    EPoS made simple
    www.jasonpos.co.uk | www.jasonpos.com

  2. #2
    Join Date
    Mar 2009
    Location
    Italy - Breganze (VI)
    Posts
    120
    First, you can get the whole contents of file:
    Next, check duplicate 'before' to add to listbox:

    Code:
    '---------- Declaration ---------- 
    Dim sFileDat As String
    Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Const LB_FINDSTRING = &H18F
    Const LB_FINDSTRINGEXACT = &H1A2
    
    '---------- code ---------- 
    Public Function GetTextFile(ByVal sPathFile As String, _
            Optional ByVal bBinaryMode As Boolean = False) As String
        Dim fnum As Integer
        fnum = FreeFile()
        If bBinaryMode Then
            Open sPathFile For Binary As #fnum
        Else
            Open sPathFile For Input As #fnum
        End If
        GetTextFile = Input(LOF(fnum), fnum)
        Close #fnum
    End Function
    
    Private Sub cmdReadData2_Click()
        Dim sContents As String
        Dim sAr() As String
        Dim i As Long
    
        sFileDat = "C:\DATA1.DAT"
    
        sContents = GetTextFile(sFileDat)
        sAr = Split(sContents, vbNewLine)
        
        For i = 0 To UBound(sAr)
            If InStr(sAr(i), "Void,1,") = 0 Then
                If SearchStringListBox(List1, sAr(i)) = -1 Then
                    List1.AddItem sAr(i)
                End If
            End If
        Next i
    End Sub
    
    Public Function SearchStringListBox(ByRef lb As ListBox, ByVal sSearch As String) As Long
        Rem Return -1 if item not exists
        SearchStringListBox = SendMessageString(lb.hWnd, LB_FINDSTRINGEXACT, -1, sSearch)
    End Function
    Last edited by gibra; 10-20-2009 at 01:57 AM.

  3. #3
    Join Date
    Jun 2009
    Posts
    135
    I tried your code...
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
    List1.AddItem "5018374320761"
    List1.AddItem "5018374320762"
    List1.AddItem "5018374320763"
    List1.AddItem "5018374320764"
    List1.AddItem "5018374320765"
    List1.AddItem "5018374320766"
    List1.AddItem "5018374320766"
    List1.AddItem "Void,1,5018374320766"
    List1.AddItem "5018374320767"
    List1.AddItem "5018374320768"
    List1.AddItem "5018374320769"
    List1.AddItem "5018374320770"
    List1.AddItem "5018374320771"
    List1.AddItem "5018374320772"
    List1.AddItem "5018374320773"
    List1.AddItem "5018374320774"
    List1.AddItem "5018374320775"
    List1.AddItem "5018374320776"
    End Sub
    
    Private Sub Command1_Click()
    Dim k As Long
    With List1
      For k = .ListCount - 1 To 0 Step -1
        If InStr(1, .List(k), "VOID", vbTextCompare) > 0 Then
          .RemoveItem (k)
          .RemoveItem (k - 1)
        End If
      Next
    End With
    End Sub
    and it works fine for me!!!



    Good Luck

    Edit: However, your code does rely upon everything being in just the right order...
    Last edited by vb5prgrmr; 10-20-2009 at 01:48 AM.

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 10:57 PM
  3. string array in c++
    By emeric in forum C++
    Replies: 11
    Last Post: 07-16-2007, 09:36 PM
  4. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  5. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center