Solved

How do I compare two arrays to find a match?

Posted on 2008-10-16
19
1,242 Views
Last Modified: 2012-05-05
I need to cycle through the values of arr_lstCells(i, 2) and find a match within D.Cells.  When a match is made, I need the value of D.FullPathFIleName put in to arr_lstCells(i,4).  D.Cells can contain multiple values seperated by commas, while arr_lstCells(i,2) will only contain one value for each array item.  Also, if D.Cells contains the value "ALL" then I want to stop comparing the arrays and assign that D.FullPathFileName to arr_lstCells(i,2) for every item in the arr_lstCells array.

 Public Function Array_lstCells() As Object(,)

        Dim a As Integer = lstCells.SelectedItems.Count

        Dim arr_lstCells(a, 4)

        Dim i As Integer = 0

        Dim sb As String = ""
 

        For Each sel As Object In lstCells.SelectedItems

            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)

            'Assign project # based on txtProject

            arr_lstCells(i, 1) = txtProject.Text

            'Split out the path and project# from the data file

            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).split("-")

            'Split out the cell and file type from the first split

            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")

            'Assign the cell

            arr_lstCells(i, 2) = strSplitListItem2(0)

            'Assign the file type

            arr_lstCells(i, 3) = strSplitListItem2(1)
 

            i = i + 1

            sb = sb & sel.ToString() & vbCrLf

        Next
 

        Return arr_lstCells

    End Function
 

    Public Class WFD_Data

        Public FullPathFileName As String

        Public JetstreamIndicator As String

        Public ProjectNumber As String

        Public Cells As String

    End Class
 

    Public Function WFD_List(ByVal folderName As String) As List(Of WFD_Data)

        Dim dataList As New List(Of WFD_Data)

        Dim di As New DirectoryInfo(folderName)
 

        If di.Exists Then

            For Each fi As FileInfo In di.GetFiles("*.wfd")

                Dim D As New WFD_Data

                D.FullPathFileName = fi.FullName

                Dim parts() As String = Path.GetFileNameWithoutExtension(fi.Name).Split("_")

                D.JetstreamIndicator = parts(0)

                D.ProjectNumber = parts(1)

                Dim cells As New List(Of String)

                For Each value As String In parts(2).Split(",")

                    If value.Contains("-") Then

                        Dim numbers() As String = value.Split("-")

                        Dim startNumber, stopNumber As Integer

                        If Integer.TryParse(numbers(0), startNumber) Then

                            If Integer.TryParse(numbers(1), stopNumber) Then

                                For N As Integer = startNumber To stopNumber

                                    cells.Add(N.ToString("000"))

                                Next

                            End If

                        End If

                    Else

                        cells.Add(value)

                    End If

                Next

                D.Cells = String.Join(",", cells.ToArray)

                dataList.Add(D)

            Next

        Else

            MessageBox.Show(folderName, "Invalid Directory", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If
 

        Return dataList
 

        'For Each D As WFD_Data In WFD_List("J:\" & txtProject.Text & "\Pnet\")

        'Debug.Print("FullPathFileName = " & D.FullPathFileName)

        'Debug.Print("JetstreamIndicator = " & D.JetstreamIndicator)

        'Debug.Print("ProjectNumber = " & D.ProjectNumber)

        'Debug.Print("Cells = " & D.Cells)

        'Debug.Print("----------------------------------------------------")

        'Next
 

    End Function

Open in new window

0
Comment
Question by:stewdaddy
  • 11
  • 8
19 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22733199
Hi again stewdaddy....  =)

So from another piece of code you are calling both of these functions?

    Array_lstCells()
    WFD_List()

Thus you have two local variables, an Array and a List, holding the return values?

    Object(,)
    List(Of WFD_Data)

Then you want to look at the 3rd element of each value in the Array "arr_lstCells(i, 2)" and find the matching WFD_DATA instance that contains that value in the Cells() property?

This can be done relatively painlessly using the List.FindAll() method and a Predicate:
http://msdn.microsoft.com/en-us/library/fh1w7y8z(VS.80).aspx

Not sure about the "ALL" part though.  Shouldn't we iterate over the WFD_Data instances FIRST and see if one of them has "ALL" in it.  Then you can just assign that to all of the values in the Array without repeatedly searching.

Let me know if I have the right idea and I'll code up an example.
0
 

Author Comment

by:stewdaddy
ID: 22733528
Hello again!  8-)
Yes, you have it exactly right.  I was thinking the same thing, the "ALL" should be the first condition to check for.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22734285
Can you clarify?...

If "ALL" is NOT present, you said to put the filename value in arr_lstCells(i,4) when a match is found:

    "I need the value of D.FullPathFIleName put in to arr_lstCells(i,4)"

But if "ALL" IS found, you said to put the filename value into arr_lstCells(i,2):

    "...I want to stop comparing the arrays and assign that D.FullPathFileName to arr_lstCells(i,2)"

Did you really mean to put the filename value into the "2" slot?  (not the 4?)

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22734359
As written, it would look like this...just be sure stated correctly with the "2" and "4"!
Imports System.IO

Public Class Form1
 

    Private CellValue As String
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Arr As Object(,) = Array_lstCells()

        Dim Ds As List(Of WFD_Data) = WFD_List("J:\" & txtProject.Text & "\Pnet\")
 

        Dim Match As WFD_Data = Nothing

        If IsAllPresent(Ds, Match) Then

            For i As Integer = 0 To Arr.GetUpperBound(0)

                Arr(i, 2) = Match.FullPathFileName

            Next

        Else

            For i As Integer = 0 To Arr.GetUpperBound(0)

                CellValue = Arr(i, 2)

                Match = Ds.Find(AddressOf Me.FindCellValue)

                If Not IsNothing(Match) Then

                    Arr(i, 4) = Match.FullPathFileName

                End If

            Next

        End If

    End Sub
 

    Private Function IsAllPresent(ByVal Ds As List(Of WFD_Data), ByRef D As WFD_Data) As Boolean

        D = Ds.Find(AddressOf HasAll)

        Return Not IsNothing(D)

    End Function
 

    Private Function HasAll(ByVal D As WFD_Data) As Boolean

        Return D.Cells.Contains("ALL")

    End Function
 

    Private Function FindCellValue(ByVal D As WFD_Data) As Boolean

        Return D.Cells.Contains(Me.CellValue)

    End Function
 

    ' *****************************************************
 

    ' ...below this point is your posted existing code...
 

    ' *****************************************************
 

    Public Function Array_lstCells() As Object(,)

        Dim a As Integer = lstCells.SelectedItems.Count

        Dim arr_lstCells(a, 4)

        Dim i As Integer = 0

        Dim sb As String = ""
 

        For Each sel As Object In lstCells.SelectedItems

            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)

            'Assign project # based on txtProject

            arr_lstCells(i, 1) = txtProject.Text

            'Split out the path and project# from the data file

            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).ToString.Split("-")

            'Split out the cell and file type from the first split

            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")

            'Assign the cell

            arr_lstCells(i, 2) = strSplitListItem2(0)

            'Assign the file type

            arr_lstCells(i, 3) = strSplitListItem2(1)
 

            i = i + 1

            sb = sb & sel.ToString() & vbCrLf

        Next
 

        Return arr_lstCells

    End Function
 

    Public Class WFD_Data

        Public FullPathFileName As String

        Public JetstreamIndicator As String

        Public ProjectNumber As String

        Public Cells As String

    End Class
 

    Public Function WFD_List(ByVal folderName As String) As List(Of WFD_Data)

        Dim dataList As New List(Of WFD_Data)

        Dim di As New DirectoryInfo(folderName)

        If di.Exists Then

            For Each fi As FileInfo In di.GetFiles("*.wfd")

                Dim D As New WFD_Data
 

                D.FullPathFileName = fi.FullName

                Dim parts() As String = Path.GetFileNameWithoutExtension(fi.Name).Split("_")

                D.JetstreamIndicator = parts(0)

                D.ProjectNumber = parts(1)

                Dim cells As New List(Of String)

                For Each value As String In parts(2).Split(",")

                    If value.Contains("-") Then

                        Dim numbers() As String = value.Split("-")

                        Dim startNumber, stopNumber As Integer

                        If Integer.TryParse(numbers(0), startNumber) Then

                            If Integer.TryParse(numbers(1), stopNumber) Then

                                For N As Integer = startNumber To stopNumber

                                    cells.Add(N.ToString("000"))

                                Next

                            End If

                        End If

                    Else

                        cells.Add(value)

                    End If

                Next

                D.Cells = String.Join(",", cells.ToArray)
 

                dataList.Add(D)

            Next

        Else

            MessageBox.Show(folderName, "Invalid Directory", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Return dataList

    End Function
 

End Class

Open in new window

0
 

Author Comment

by:stewdaddy
ID: 22734612
I'm sorry... I meant that the value should be put in (i,4) in both instances.  I'll give your code a try...
0
 

Author Comment

by:stewdaddy
ID: 22734792
The code worked when there was an ALL file present, but when I removed the ALL file from the directory I received an "ArgumentNullException was unhandled" error on the following:
   
    Private Function FindCellValue(ByVal D As WFD_Data) As Boolean
        Return D.Cells.Contains(Me.CellValue)
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22734919
Some possible problems...

(1) D.Cells is Nothing
(2) Me.Cellvalue is Nothing

Here is the code that runs when "ALL" is NOT present:

            For i As Integer = 0 To Arr.GetUpperBound(0)
                CellValue = Arr(i, 2)
                Match = Ds.Find(AddressOf Me.FindCellValue)
                If Not IsNothing(Match) Then
                    Arr(i, 4) = Match.FullPathFileName
                End If
            Next

This line assigns the "cell value" to search for:

    CellValue = Arr(i, 2)

You wrote the code that populates the Array so I didn't look over it closely to make sure it was "safe".  It is most likely assigning Nothing into Arr(i, 2) and causing CellValue to be Nothing as well.  You are using OBJECT for the Array...is that necessary?  Does it really have MULTIPLE Types in it?...or is the Array completely comprised of just STRINGS?  If you can safely change the Array Type from Object to String then that Error should go away.  Be sure to change all or your variables to String(,) as well though.

Is it possible for Arr(i,2) to be blank?

We can narrow down the error with this change:

    Private Function FindCellValue(ByVal D As WFD_Data) As Boolean
        If Not IsNothing(D) Then
            If Not IsNothing(D.Cells) Then
                If Not IsNothing(Me.CellValue) Then
                    Return D.Cells.Contains(Me.CellValue)
                Else
                    MessageBox.Show("Me.CellValue is Nothing!")
                End If
            Else
                MessageBox.Show("D.Cells is Nothing!")
            End If
        Else
            MessageBox.Show("D is Nothing!")
        End If
        Return False
    End Function
0
 

Author Comment

by:stewdaddy
ID: 22734991
The MessageBox "Me.CellValue is nothing!" pops up.  I have attached ALL of my code for your review.  I really appreciate the help with this...
'Batch Spooler v1.0

'October 14, 2008
 

Option Explicit On

Imports System

Imports System.IO
 
 

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        End

    End Sub
 

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        BatchLine()

        WriteBatch()

    End Sub
 

    Private Sub txtProject_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProject.GotFocus

        Clear_lstCells()

    End Sub
 

    Private Sub txtProject_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProject.LostFocus

        Populate_lstCells()

    End Sub
 

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

        chkSignoffFiles.Checked = True

        chkProductionFiles.Checked = True

        chkSampleFiles.Checked = True

    End Sub
 

    Private Sub chkSignoffFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSignoffFiles.CheckedChanged

        Populate_lstCells()

    End Sub

 

    Private Sub chkProductionFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkProductionFiles.CheckedChanged

        Populate_lstCells()

    End Sub
 

    Private Sub chkSampleFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSampleFiles.CheckedChanged

        Populate_lstCells()

    End Sub
 

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        End

    End Sub
 

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        MessageBox.Show("Batch Spooler v1.0" & vbCrLf & "Created by Chris Stewart", "About Batch Spooler v1.0", MessageBoxButtons.OK)

    End Sub
 
 
 

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''BEGIN USER DEFINED SUBS AND FUNCTIONS'''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 

    Public Sub Populate_lstCells()

        Dim strProjectDirectory As String = ("J:\" & txtProject.Text & "\")
 

        Clear_lstCells()
 

        If txtProject.Text <> "" Then

            Try

                If chkProductionFiles.Checked = True Then

                    Dim ProdFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.prd")

                    lstCells.Items.AddRange(ProdFiles)

                End If

                If chkSignoffFiles.Checked = True Then

                    Dim SofFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.sof")

                    lstCells.Items.AddRange(SofFiles)

                End If

                If chkSampleFiles.Checked = True Then

                    Dim SamFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.smp")

                    lstCells.Items.AddRange(SamFiles)

                End If

            Catch ex As System.IO.DirectoryNotFoundException

                MessageBox.Show("The requested project # was not found.", "Project # not found...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                With txtProject

                    .Text = ""

                    .Focus()

                End With

            End Try

        End If

    End Sub
 

    Public Sub Clear_lstCells()

        With lstCells

            .Items.Clear()

        End With

    End Sub
 

    Public Function Array_lstCells() As Object(,)

        Dim a As Integer = lstCells.SelectedItems.Count

        Dim arr_lstCells(a, 4)

        Dim i As Integer = 0

        Dim sb As String = ""
 

        For Each sel As Object In lstCells.SelectedItems

            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)

            'Assign project # based on txtProject

            arr_lstCells(i, 1) = txtProject.Text

            'Split out the path and project# from the data file

            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).split("-")

            'Split out the cell and file type from the first split

            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")

            'Assign the cell

            arr_lstCells(i, 2) = strSplitListItem2(0)

            'Assign the file type

            arr_lstCells(i, 3) = strSplitListItem2(1)
 

            i = i + 1

            sb = sb & sel.ToString() & vbCrLf

        Next
 

        Return arr_lstCells

    End Function
 

    Public Function SimplexDuplex()

        Dim strSimplexDuplexProductionLine As String = ""

        If radSimplex.Checked = True Then

            strSimplexDuplexProductionLine = "S"

        ElseIf radDuplex.Checked = True Then

            strSimplexDuplexProductionLine = "D"

        Else

            MessageBox.Show("Please select an output type.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Return strSimplexDuplexProductionLine

    End Function
 

    Public Function UpnessType()

        Dim strUpness As String = ""

        Dim strUpnessProductionLine As String = ""

        Try

            If rad1up.Checked = True Then

                strUpnessProductionLine = "1"

            ElseIf rad2up.Checked = True Then

                strUpnessProductionLine = "2"

            ElseIf radOther.Checked = True Then

                Dim strUserUpness As String = ""

                strUserUpness = InputBox("How many up is the output?", "User defined up factor...", , , )

                Try

                    strUpness = Convert.ToInt16(strUserUpness) & "-up"

                    strUpnessProductionLine = Convert.ToInt16(strUserUpness)

                    radOther.Text = strUpness & "-up"

                Catch

                    MessageBox.Show("Please enter a valid up-ness value.", "Invalid parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                End Try

            Else

                MessageBox.Show("Please select an up-ness value.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End If

        Catch ex As System.Data.InvalidExpressionException

            MessageBox.Show("System error", "Function UpnessType Error...", MessageBoxButtons.OK)

        End Try

        Return strUpnessProductionLine

    End Function
 

    Public Function MICR()

        Dim strMICRProductionLine As String = ""

        Try

            If radMICR.Checked = True Then

                strMICRProductionLine = "M"

            ElseIf radNonMICR.Checked = True Then

                strMICRProductionLine = "N"

            Else

                MessageBox.Show("Please select a MICR value.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End If

        Catch ex As System.Data.InvalidExpressionException

            MessageBox.Show("System error", "Function MICR Error...", MessageBoxButtons.OK)

        End Try

        Return strMICRProductionLine

    End Function
 

    Public Sub WriteBatch()

        Dim oFile As System.IO.File

        Dim oWrite As System.IO.StreamWriter

        Dim sb As String = BatchLine()
 

        Try

            oWrite = oFile.CreateText("C:\SPOOLER\BATCH\" & txtProject.Text & ".bat")

            oWrite.WriteLine(sb)

            oWrite.Close()

        Catch

            MessageBox.Show("C:\SPOOLER\BATCH\ not found. Please contact the administrator.", "BATCH folder not found...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End Try

    End Sub
 

    Public Function BatchLine()

        Dim i As Integer = 0

        Dim a As Integer = lstCells.SelectedItems.Count

        Dim sb As String = ""
 

        Dim arr_lstCells As Object(,) = Array_lstCells()

        Dim arr_WFD As Object(,) = IdentifyWFD()

        Dim strOutputModule As String = IdentifyOutputModule()

        Dim strSimplexDuplexProductionLine As String = SimplexDuplex()

        Dim strMICRProductionLine As String = MICR()

        Dim strSignoffProductionLine As String = ""

        Dim strProductionLine As String = ""
 

        Do Until i = a

            'Assemble strProductionLine

            If arr_lstCells(i, 3).ToUpper = "SOF" Then strSignoffProductionLine = "S"

            strProductionLine = (strSimplexDuplexProductionLine & strMICRProductionLine & strSignoffProductionLine)

            'PDF

            If arr_lstCells(i, 3).ToUpper = "SOF" Then

                sb = sb & """pnet.exe"" " & """" & arr_WFD(i, 4) & """ " & "-o " & """" & strOutputModule & """ " & "-e PDF " & "-c " & """M:\JetStream_Tests\Misc\JOB_File\AFP-JetStream-Config-04292008.job"" " & "-f " & """J:\" & arr_lstCells(i, 1) & "\PDF\" & arr_lstCells(i, 1) & "_" & arr_lstCells(i, 2) & "_" & strProductionLine & ".pdf"" " & "-difDataInput1 " & """" & arr_lstCells(i, 0) & """ " & "-la " & """L:\" & txtProject.Text & "\" & txtProject.Text & ".log"" " & "-nowarnings" & vbCrLf

                'AFP

            ElseIf arr_lstCells(i, 3).ToUpper = "SMP" Or arr_lstCells(i, 3).ToUpper = "PRD" Then

                sb = sb & """pnet.exe"" " & """" & arr_WFD(i, 4) & """ " & "-o " & """" & strOutputModule & """ " & "-e AFP " & "-ec D00 " & "-c " & """M:\JetStream_Tests\Misc\JOB_File\AFP-JetStream-Config-04292008.job"" " & "-f " & """L:\" & arr_lstCells(i, 1) & "\AFP\" & arr_lstCells(i, 1) & "_" & arr_lstCells(i, 2) & "_" & strProductionLine & ".afp"" " & "-difDataInput1 " & """" & arr_lstCells(i, 0) & """ " & "-la " & """L:\" & txtProject.Text & "\" & txtProject.Text & ".log"" " & "-nowarnings" & vbCrLf

            Else

                MessageBox.Show("BatchLine Function Error. Please contact the administrator.", "BatchLine Function Error...", MessageBoxButtons.OK)

            End If

            i = i + 1

        Loop
 

        Return sb

    End Function
 

    Public Function IdentifyOutputModule()

        Dim strUpnessProductionLine As String = UpnessType()

        Dim strSimplexDuplex As String = SimplexDuplex()

        If strSimplexDuplex = "S" Then strSimplexDuplex = "Simplex"

        If strSimplexDuplex = "D" Then strSimplexDuplex = "Duplex"

        Dim strOutputModule As String = (strUpnessProductionLine & "-up " & strSimplexDuplex)

        Return strOutputModule

    End Function
 

    Public Class WFD_Data

        Public FullPathFileName As String

        Public JetstreamIndicator As String

        Public ProjectNumber As String

        Public Cells As String

    End Class
 

    Public Function WFD_List(ByVal folderName As String) As List(Of WFD_Data)

        Dim dataList As New List(Of WFD_Data)

        Dim di As New DirectoryInfo(folderName)
 

        If di.Exists Then

            For Each fi As FileInfo In di.GetFiles("*.wfd")

                Dim D As New WFD_Data

                D.FullPathFileName = fi.FullName

                Dim parts() As String = Path.GetFileNameWithoutExtension(fi.Name).Split("_")

                D.JetstreamIndicator = parts(0)

                D.ProjectNumber = parts(1)

                Dim cells As New List(Of String)

                For Each value As String In parts(2).Split(",")

                    If value.Contains("-") Then

                        Dim numbers() As String = value.Split("-")

                        Dim startNumber, stopNumber As Integer

                        If Integer.TryParse(numbers(0), startNumber) Then

                            If Integer.TryParse(numbers(1), stopNumber) Then

                                For N As Integer = startNumber To stopNumber

                                    cells.Add(N.ToString("000"))

                                Next

                            End If

                        End If

                    Else

                        cells.Add(value)

                    End If

                Next

                D.Cells = String.Join(",", cells.ToArray)

                dataList.Add(D)

            Next

        Else

            MessageBox.Show(folderName, "Invalid Directory", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If
 

        Return dataList
 

        'For Each D As WFD_Data In WFD_List("J:\" & txtProject.Text & "\Pnet\")

        'Debug.Print("FullPathFileName = " & D.FullPathFileName)

        'Debug.Print("JetstreamIndicator = " & D.JetstreamIndicator)

        'Debug.Print("ProjectNumber = " & D.ProjectNumber)

        'Debug.Print("Cells = " & D.Cells)

        'Debug.Print("----------------------------------------------------")

        'Next
 

    End Function
 

    Private CellValue As String
 

    Public Function IdentifyWFD()

        Dim arr_lstCells As Object(,) = Array_lstCells()

        Dim Ds As List(Of WFD_Data) = WFD_List("J:\" & txtProject.Text & "\Pnet\")
 

        Dim Match As WFD_Data = Nothing

        If IsAllPresent(Ds, Match) Then

            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)

                arr_lstCells(i, 4) = Match.FullPathFileName

            Next

        Else

            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)

                CellValue = arr_lstCells(i, 2)

                Match = Ds.Find(AddressOf Me.FindCellValue)

                If Not IsNothing(Match) Then

                    arr_lstCells(i, 4) = Match.FullPathFileName

                End If

            Next

        End If
 

        Return arr_lstCells

    End Function
 

    Private Function IsAllPresent(ByVal Ds As List(Of WFD_Data), ByRef D As WFD_Data) As Boolean

        D = Ds.Find(AddressOf HasAll)

        Return Not IsNothing(D)

    End Function
 

    Private Function HasAll(ByVal D As WFD_Data) As Boolean

        Return D.Cells.Contains("ALL")

    End Function
 

    Private Function FindCellValue(ByVal D As WFD_Data) As Boolean

        If Not IsNothing(D) Then

            If Not IsNothing(D.Cells) Then

                If Not IsNothing(Me.CellValue) Then

                    Return D.Cells.Contains(Me.CellValue)

                Else

                    MessageBox.Show("Me.CellValue is Nothing!")

                End If

            Else

                MessageBox.Show("D.Cells is Nothing!")

            End If

        Else

            MessageBox.Show("D is Nothing!")

        End If

        Return False

    End Function
 

End Class

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22735056
Ok...that tells me that the Nothing value is coming from YOUR Array which was poplulate by your Array_lstCells() function.

It really seems like Object is a bad choice for your Array as you are only putting String values into it.

Is "lstCells" a ListBox with Objects or Strings in it?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:stewdaddy
ID: 22735084
lstCells contains strings... it contains values such as "J:\12345\12345.SOF".  I haven't coded for a while and I picked up an example that had the array as an object, so i just used it.  I'm not really sure how to convert everything over to strings...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22735119
Alrighty...let's see if we can fix her...

<<< STANDBY >>>
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22735468
Make a BACKUP of your current work...  =)

Does this work?
'Batch Spooler v1.0

'October 14, 2008
 

Option Explicit On

Imports System

Imports System.IO
 

Public Class frmMain
 

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        End

    End Sub
 

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        BatchLine()

        WriteBatch()

    End Sub
 

    Private Sub txtProject_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProject.GotFocus

        Clear_lstCells()

    End Sub
 

    Private Sub txtProject_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtProject.LostFocus

        Populate_lstCells()

    End Sub
 

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

        chkSignoffFiles.Checked = True

        chkProductionFiles.Checked = True

        chkSampleFiles.Checked = True

    End Sub
 

    Private Sub chkSignoffFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSignoffFiles.CheckedChanged

        Populate_lstCells()

    End Sub
 

    Private Sub chkProductionFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkProductionFiles.CheckedChanged

        Populate_lstCells()

    End Sub
 

    Private Sub chkSampleFiles_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSampleFiles.CheckedChanged

        Populate_lstCells()

    End Sub
 

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        End

    End Sub
 

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        MessageBox.Show("Batch Spooler v1.0" & vbCrLf & "Created by Chris Stewart", "About Batch Spooler v1.0", MessageBoxButtons.OK)

    End Sub
 

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''BEGIN USER DEFINED SUBS AND FUNCTIONS'''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 

    Public Sub Populate_lstCells()

        Dim strProjectDirectory As String = "J:\" & txtProject.Text & "\"
 

        Clear_lstCells()
 

        If txtProject.Text <> "" Then

            Try

                If chkProductionFiles.Checked = True Then

                    Dim ProdFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.prd")

                    lstCells.Items.AddRange(ProdFiles)

                End If

                If chkSignoffFiles.Checked = True Then

                    Dim SofFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.sof")

                    lstCells.Items.AddRange(SofFiles)

                End If

                If chkSampleFiles.Checked = True Then

                    Dim SamFiles As String() = IO.Directory.GetFiles(strProjectDirectory, "*.smp")

                    lstCells.Items.AddRange(SamFiles)

                End If

            Catch ex As System.IO.DirectoryNotFoundException

                MessageBox.Show("The requested project # was not found.", "Project # not found...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                With txtProject

                    .Text = ""

                    .Focus()

                End With

            End Try

        End If

    End Sub
 

    Public Sub Clear_lstCells()

        With lstCells

            .Items.Clear()

        End With

    End Sub
 

    Public Function Array_lstCells() As String(,)

        Dim arr_lstCells(lstCells.SelectedItems.Count, 4) As String

        For i As Integer = 0 To lstCells.SelectedItems.Count - 1

            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)

            'Assign project # based on txtProject

            arr_lstCells(i, 1) = txtProject.Text

            'Split out the path and project# from the data file

            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).split("-")

            'Split out the cell and file type from the first split

            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")

            'Assign the cell

            arr_lstCells(i, 2) = strSplitListItem2(0)

            'Assign the file type

            arr_lstCells(i, 3) = strSplitListItem2(1)

        Next

        Return arr_lstCells

    End Function
 

    Public Function SimplexDuplex()

        Dim strSimplexDuplexProductionLine As String = ""

        If radSimplex.Checked = True Then

            strSimplexDuplexProductionLine = "S"

        ElseIf radDuplex.Checked = True Then

            strSimplexDuplexProductionLine = "D"

        Else

            MessageBox.Show("Please select an output type.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Return strSimplexDuplexProductionLine

    End Function
 

    Public Function UpnessType()

        Dim strUpness As String = ""

        Dim strUpnessProductionLine As String = ""

        Try

            If rad1up.Checked = True Then

                strUpnessProductionLine = "1"

            ElseIf rad2up.Checked = True Then

                strUpnessProductionLine = "2"

            ElseIf radOther.Checked = True Then

                Dim strUserUpness As String = ""

                strUserUpness = InputBox("How many up is the output?", "User defined up factor...", , , )

                Try

                    strUpness = Convert.ToInt16(strUserUpness) & "-up"

                    strUpnessProductionLine = Convert.ToInt16(strUserUpness)

                    radOther.Text = strUpness & "-up"

                Catch

                    MessageBox.Show("Please enter a valid up-ness value.", "Invalid parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                End Try

            Else

                MessageBox.Show("Please select an up-ness value.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End If

        Catch ex As System.Data.InvalidExpressionException

            MessageBox.Show("System error", "Function UpnessType Error...", MessageBoxButtons.OK)

        End Try

        Return strUpnessProductionLine

    End Function
 

    Public Function MICR()

        Dim strMICRProductionLine As String = ""

        Try

            If radMICR.Checked = True Then

                strMICRProductionLine = "M"

            ElseIf radNonMICR.Checked = True Then

                strMICRProductionLine = "N"

            Else

                MessageBox.Show("Please select a MICR value.", "Missing parameter...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End If

        Catch ex As System.Data.InvalidExpressionException

            MessageBox.Show("System error", "Function MICR Error...", MessageBoxButtons.OK)

        End Try

        Return strMICRProductionLine

    End Function
 

    Public Sub WriteBatch()

        Dim oFile As System.IO.File

        Dim oWrite As System.IO.StreamWriter

        Dim sb As String = BatchLine()
 

        Try

            oWrite = oFile.CreateText("C:\SPOOLER\BATCH\" & txtProject.Text & ".bat")

            oWrite.WriteLine(sb)

            oWrite.Close()

        Catch

            MessageBox.Show("C:\SPOOLER\BATCH\ not found. Please contact the administrator.", "BATCH folder not found...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End Try

    End Sub
 

    Public Function BatchLine()

        Dim i As Integer = 0

        Dim a As Integer = lstCells.SelectedItems.Count

        Dim sb As String = ""
 

        Dim arr_lstCells As String(,) = Array_lstCells()

        Dim arr_WFD As String(,) = IdentifyWFD()

        Dim strOutputModule As String = IdentifyOutputModule()

        Dim strSimplexDuplexProductionLine As String = SimplexDuplex()

        Dim strMICRProductionLine As String = MICR()

        Dim strSignoffProductionLine As String = ""

        Dim strProductionLine As String = ""
 

        Do Until i = a

            'Assemble strProductionLine

            If arr_lstCells(i, 3).ToUpper = "SOF" Then strSignoffProductionLine = "S"

            strProductionLine = (strSimplexDuplexProductionLine & strMICRProductionLine & strSignoffProductionLine)

            'PDF

            If arr_lstCells(i, 3).ToUpper = "SOF" Then

                sb = sb & """pnet.exe"" " & """" & arr_WFD(i, 4) & """ " & "-o " & """" & strOutputModule & """ " & "-e PDF " & "-c " & """M:\JetStream_Tests\Misc\JOB_File\AFP-JetStream-Config-04292008.job"" " & "-f " & """J:\" & arr_lstCells(i, 1) & "\PDF\" & arr_lstCells(i, 1) & "_" & arr_lstCells(i, 2) & "_" & strProductionLine & ".pdf"" " & "-difDataInput1 " & """" & arr_lstCells(i, 0) & """ " & "-la " & """L:\" & txtProject.Text & "\" & txtProject.Text & ".log"" " & "-nowarnings" & vbCrLf

                'AFP

            ElseIf arr_lstCells(i, 3).ToUpper = "SMP" Or arr_lstCells(i, 3).ToUpper = "PRD" Then

                sb = sb & """pnet.exe"" " & """" & arr_WFD(i, 4) & """ " & "-o " & """" & strOutputModule & """ " & "-e AFP " & "-ec D00 " & "-c " & """M:\JetStream_Tests\Misc\JOB_File\AFP-JetStream-Config-04292008.job"" " & "-f " & """L:\" & arr_lstCells(i, 1) & "\AFP\" & arr_lstCells(i, 1) & "_" & arr_lstCells(i, 2) & "_" & strProductionLine & ".afp"" " & "-difDataInput1 " & """" & arr_lstCells(i, 0) & """ " & "-la " & """L:\" & txtProject.Text & "\" & txtProject.Text & ".log"" " & "-nowarnings" & vbCrLf

            Else

                MessageBox.Show("BatchLine Function Error. Please contact the administrator.", "BatchLine Function Error...", MessageBoxButtons.OK)

            End If

            i = i + 1

        Loop
 

        Return sb

    End Function
 

    Public Function IdentifyOutputModule()

        Dim strUpnessProductionLine As String = UpnessType()

        Dim strSimplexDuplex As String = SimplexDuplex()

        If strSimplexDuplex = "S" Then strSimplexDuplex = "Simplex"

        If strSimplexDuplex = "D" Then strSimplexDuplex = "Duplex"

        Dim strOutputModule As String = (strUpnessProductionLine & "-up " & strSimplexDuplex)

        Return strOutputModule

    End Function
 

    Public Class WFD_Data

        Public FullPathFileName As String

        Public JetstreamIndicator As String

        Public ProjectNumber As String

        Public Cells As String

    End Class
 

    Public Function WFD_List(ByVal folderName As String) As List(Of WFD_Data)

        Dim dataList As New List(Of WFD_Data)

        Dim di As New DirectoryInfo(folderName)
 

        If di.Exists Then

            For Each fi As FileInfo In di.GetFiles("*.wfd")

                Dim D As New WFD_Data

                D.FullPathFileName = fi.FullName

                Dim parts() As String = Path.GetFileNameWithoutExtension(fi.Name).Split("_")

                D.JetstreamIndicator = parts(0)

                D.ProjectNumber = parts(1)

                Dim cells As New List(Of String)

                For Each value As String In parts(2).Split(",")

                    If value.Contains("-") Then

                        Dim numbers() As String = value.Split("-")

                        Dim startNumber, stopNumber As Integer

                        If Integer.TryParse(numbers(0), startNumber) Then

                            If Integer.TryParse(numbers(1), stopNumber) Then

                                For N As Integer = startNumber To stopNumber

                                    cells.Add(N.ToString("000"))

                                Next

                            End If

                        End If

                    Else

                        cells.Add(value)

                    End If

                Next

                D.Cells = String.Join(",", cells.ToArray)

                dataList.Add(D)

            Next

        Else

            MessageBox.Show(folderName, "Invalid Directory", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If
 

        Return dataList
 

        'For Each D As WFD_Data In WFD_List("J:\" & txtProject.Text & "\Pnet\")

        'Debug.Print("FullPathFileName = " & D.FullPathFileName)

        'Debug.Print("JetstreamIndicator = " & D.JetstreamIndicator)

        'Debug.Print("ProjectNumber = " & D.ProjectNumber)

        'Debug.Print("Cells = " & D.Cells)

        'Debug.Print("----------------------------------------------------")

        'Next
 

    End Function
 

    Private CellValue As String
 

    Public Function IdentifyWFD()

        Dim arr_lstCells As String(,) = Array_lstCells()

        Dim Ds As List(Of WFD_Data) = WFD_List("J:\" & txtProject.Text & "\Pnet\")
 

        Dim Match As WFD_Data = Nothing

        If IsAllPresent(Ds, Match) Then

            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)

                arr_lstCells(i, 4) = Match.FullPathFileName

            Next

        Else

            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)

                CellValue = arr_lstCells(i, 2)

                Match = Ds.Find(AddressOf Me.FindCellValue)

                If Not IsNothing(Match) Then

                    arr_lstCells(i, 4) = Match.FullPathFileName

                End If

            Next

        End If
 

        Return arr_lstCells

    End Function
 

    Private Function IsAllPresent(ByVal Ds As List(Of WFD_Data), ByRef D As WFD_Data) As Boolean

        D = Ds.Find(AddressOf HasAll)

        Return Not IsNothing(D)

    End Function
 

    Private Function HasAll(ByVal D As WFD_Data) As Boolean

        Return D.Cells.Contains("ALL")

    End Function
 

    Private Function FindCellValue(ByVal D As WFD_Data) As Boolean

        If Not IsNothing(D) Then

            If Not IsNothing(D.Cells) Then

                If Not IsNothing(Me.CellValue) Then

                    Return D.Cells.Contains(Me.CellValue)

                Else

                    MessageBox.Show("Me.CellValue is Nothing!")

                End If

            Else

                MessageBox.Show("D.Cells is Nothing!")

            End If

        Else

            MessageBox.Show("D is Nothing!")

        End If

        Return False

    End Function
 

End Class

Open in new window

0
 

Author Comment

by:stewdaddy
ID: 22735525
Ok... it generates the batch file I need, and the information in it is all correct, but I still get the MessageBox that says "Me.CellValue is Nothing!".
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22735546
Roger...one moment please....
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22735931
If you change your IdentifyWFD() function to the below, do you get a new MessageBox?

    Public Function IdentifyWFD()
        Dim arr_lstCells As String(,) = Array_lstCells()
        Dim Ds As List(Of WFD_Data) = WFD_List("J:\" & txtProject.Text & "\Pnet\")

        Dim Match As WFD_Data = Nothing
        If IsAllPresent(Ds, Match) Then
            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)
                arr_lstCells(i, 4) = Match.FullPathFileName
            Next
        Else
            For i As Integer = 0 To arr_lstCells.GetUpperBound(0)
                CellValue = arr_lstCells(i, 2)
                If IsNothing(CellValue) Then
                    Dim msg As String = _
                        arr_lstCells(i, 0) & vbCrLf & _
                        arr_lstCells(i, 1) & vbCrLf & _
                        arr_lstCells(i, 2) & vbCrLf & _
                        arr_lstCells(i, 3) & vbCrLf & _
                        arr_lstCells(i, 4)

                    MessageBox.Show(msg, "Is this a valid arr_lstCells() record?", MessageBoxButtons.OK, MessageBoxIcon.Question)
                Else
                    Match = Ds.Find(AddressOf Me.FindCellValue)
                    If Not IsNothing(Match) Then
                        arr_lstCells(i, 4) = Match.FullPathFileName
                    End If
                End If
            Next
        End If

        Return arr_lstCells
    End Function
0
 

Author Comment

by:stewdaddy
ID: 22735997
The message box with the "Is this a valid arr_lstCells() record?" title pops up, but the content is empty.  It popped up, I clicked ok, and then it popped up a second time.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22736105
I think the problem was with your original Array_lstCells() code.  It looks like a classic "off by one" error.  =\

Your original code was:

        Dim a As Integer = lstCells.SelectedItems.Count
        Dim arr_lstCells(a, 4)

Which I changed to:

        Dim arr_lstCells(lstCells.SelectedItems.Count, 4) As String

Both pieces of code were using ".Count" when they should have been using ".Count - 1".

This was creating an EXTRA blank record at the end of the array resulting in the error.

So make the line above this:

        Dim arr_lstCells(lstCells.SelectedItems.Count - 1, 4) As String

Is it all better now?  *** crosses fingers ***
    Public Function Array_lstCells() As String(,)

        Dim arr_lstCells(lstCells.SelectedItems.Count - 1, 4) As String

        For i As Integer = 0 To lstCells.SelectedItems.Count - 1

            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)

            'Assign project # based on txtProject

            arr_lstCells(i, 1) = txtProject.Text

            'Split out the path and project# from the data file

            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).split("-")

            'Split out the cell and file type from the first split

            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")

            'Assign the cell

            arr_lstCells(i, 2) = strSplitListItem2(0)

            'Assign the file type

            arr_lstCells(i, 3) = strSplitListItem2(1)

        Next

        Return arr_lstCells

    End Function

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22736118
This is why I like the List(Of XXX) approach better than using an Arrays.  With an Array you have to specify an initial size which can lead to errors.  The List has EXACTLY only the items you add to it...it grows automatically to hold what you put in...
0
 

Author Closing Comment

by:stewdaddy
ID: 31506729
YES!!! It finally works!  Thanks so much for you help, I greatly appreciate it.  You certianly are a VB Genius!  I will look into using the List instead of Arrays.  You have given me plenty to think about.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now