Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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':
VB
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:
VB
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")

        'Loop All
        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 'SubKey

            LastLabelName = MainKey.Key
        Next 'MainKey

        objWriter.Write("QUIT" & vbCrLf)

        objWriter.Close()

    End Sub

End Class
Posted
Updated 4-Feb-14 0:01am
v2
Comments
Sergey Alexandrovich Kryukov 3-Feb-14 11:26am    
The question does not really make sense. You don't explain why would you need that weird text file, what should be its general content and, again, why. Are you trying to create a simple scripting language? But then, why are you asking about some particular class, not explaining what it should do? My impression is, you have very vague and poop ideas based on lack of understanding or programming. I would advise to grow in development on some simpler problem, until you gain knowledge, understanding and confidence.
—SA
Homebunny 4-Feb-14 6:04am    
Hi Sergey, I've reformulated the question to clarify my question, and I've a added working code snipped.
Sergey Alexandrovich Kryukov 4-Feb-14 10:50am    
You see, as the purpose of this not clear, hard to tell anything. What need improvement is all those hard-coded immediate constants, especially strings. Also, all repeated string concatenation is bad, should be replaced with string.Format.
(Do you know that strings are immutable, it makes concatenation also bad for performance if it is repeated, can you see why?)
—SA

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