I could use some help in creating a custom class.
I've been reading about IEnumerable, arraylists, structures, ...
But I'm finding it hard to point what I really need, hopefully someone can point me in the right way.
Below is what I've coded, but even to me it looks like a mess, and I believe there is improvement possible, especially in the class 'clsNiceLabelJOB'.
Target: Creating a TEXT flat file that will be used as input by NiceLabel (=label software)
Example TEXT file:
LABEL = "C:\temp\mylabelA.lbl"
SET Variable1="1"
SET Variable2="2"
PRINT 2
LABEL = "C:\temp\mylabelA.lbl"
SET Variable1="11"
SET Variable2="22"
PRINT 1
LABEL = "C:\temp\mylabelB.lbl"
SET Variable1="111"
SET Variable2="222"
PRINT 2
QUIT
Usage off the class 'clsNiceLabelJOB':
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
Dim Parameters As New SortedDictionary(Of String, String)
Dim cNL As New clsNiceLabelJOB("c:\temp\Output\")
Parameters = New SortedDictionary(Of String, String)
Parameters.Add("Variable1", "111")
Parameters.Add("Variable2", "222")
cNL.AddLabel("C:\temp\mylabelB.lbl", Parameters, 2)
Parameters = New SortedDictionary(Of String, String)
Parameters.Add("Variable1", "1")
Parameters.Add("Variable2", "2")
cNL.AddLabel("C:\temp\mylabelA.lbl", Parameters, 2)
Parameters = New SortedDictionary(Of String, String)
Parameters.Add("Variable1", "11")
Parameters.Add("Variable2", "22")
cNL.AddLabel("C:\temp\mylabelA.lbl", Parameters, 1)
cNL.SaveJOB()
End Sub
Class clsNiceLabelJOB:
Public Class clsNiceLabelJOB
#Region "Public Structures"
Public Structure strucLabelOptions
Dim Parameters As SortedDictionary(Of String, String)
Dim PrintQuantity As Integer
End Structure
Public Structure strucNiceLabel
Dim LabelOptions As IList(Of strucLabelOptions)
End Structure
#End Region
#Region "Private Declarations"
Private tstrucNiceLabel As New SortedDictionary(Of String, strucNiceLabel)
Private m_FolderLocation As String = ""
#End Region
Public Sub New(FolderLocation As String)
m_FolderLocation = FolderLocation
End Sub
Public Sub AddLabel(LabelName As String, Parameters As SortedDictionary(Of String, String), PrintQuantity As Integer)
Dim tempstrucNiceLabel As New strucNiceLabel
Dim tempstrucLabelOptions As New strucLabelOptions
If Not tstrucNiceLabel.ContainsKey(LabelName) Then
tstrucNiceLabel.Add(LabelName, tempstrucNiceLabel)
tempstrucNiceLabel.LabelOptions = New List(Of strucLabelOptions)()
Else
tempstrucNiceLabel = tstrucNiceLabel(LabelName)
End If
tempstrucLabelOptions.Parameters = Parameters
tempstrucLabelOptions.PrintQuantity = PrintQuantity
tempstrucNiceLabel.LabelOptions.Add(tempstrucLabelOptions)
tstrucNiceLabel(LabelName) = tempstrucNiceLabel
End Sub
Public Sub SaveJOB()
Dim LastLabelName As String = ""
Dim objWriter As New System.IO.StreamWriter(m_FolderLocation & "\" & Now.ToString("yyyy-MM-dd-HH-mm-ss") & ".JOB")
For Each MainKey In tstrucNiceLabel
For Each SubKey In MainKey.Value.LabelOptions
If MainKey.Key <> LastLabelName Then
objWriter.Write("LABEL = " & """" & MainKey.Key & """" & vbCrLf)
End If
For Each Param In SubKey.Parameters
objWriter.Write("SET " & Param.Key & "=" & """" & Param.Value & """" & vbCrLf)
Next
objWriter.Write("PRINT " & SubKey.PrintQuantity & vbCrLf)
objWriter.Write("" & vbCrLf)
Next
LastLabelName = MainKey.Key
Next
objWriter.Write("QUIT" & vbCrLf)
objWriter.Close()
End Sub
End Class