Scope of private varable in class

tnorton

Member
Joined
Sep 28, 2007
Messages
19
Programming Experience
1-3
I have what should be simple but I am stumped.

I have a class to store an array that is accessable from several other classes.
I have a class.add method, that addes a new entry to the array.
My problem is it adds the new data but the previous item becomes nothing.

VB.NET:
Public Class MyLicense 'holds the array of licenses and MAC addresses
    Private _mac() As String
    Private _license() As String
    Private _site() As String

    Public Sub add(ByRef m_mac As String, ByRef m_license As String, ByRef m_site As String)
        Dim a As Int16 = Me.count
        ReDim _mac(a)
        ReDim _license(a)
        ReDim _site(a)
        _mac(a) = m_mac
        _license(a) = m_license
        _site(a) = m_site
    End Sub
    Public Function count() As Integer
        Try
            count = _mac.Length
        Catch ex As Exception
            count = 0
        End Try

    End Function
    Public ReadOnly Property Mac(ByVal index) As String
        Get
            Return _mac(index)
        End Get
    End Property
    Public ReadOnly Property License(ByVal index) As String
        Get
            Return _license(index)
        End Get
    End Property
    Public ReadOnly Property Site(ByVal index) As String
        Get
            Return _site(index)
        End Get
    End Property
 

End Class

and the routine that initially loads the data into the class.
There is also another 2 forms that needs to access and read /add entries

VB.NET:
    Public Sub Licenses_Load()
        Dim registry As New RegistryTools

        'TODO: load registry to string array
        Dim RegistryPath As String = "Nortronics\Indigo\Licenses"
        Dim LicenseCount As Integer = registry.GetKeyCount(RegistryPath) 'count the number of licenses in registry

        ' ReDim myLicenses(LicenseCount)
        Dim SubKeyName As String

        Dim Result(2) As String
        Dim a As Integer

        'get and array of the registry entries for MAC Address, License and Site.
        'index starts from 1
        For a = 1 To LicenseCount
            SubKeyName = "Key" & a



            If registry.DoesSubKeyExist(RegistryPath, SubKeyName) Then

                Result(0) = registry.GetValue(RegistryPath & "\" & SubKeyName, "MAC")
                Result(1) = registry.GetValue(RegistryPath & "\" & SubKeyName, "License")
                Result(2) = registry.GetValue(RegistryPath & "\" & SubKeyName, "Site")
                myLicenses.add(Result(0), Result(1), Result(2))


            End If

        Next

        'TODO: if license is demo, start program time and display anoyance message

    End Sub
 
Use "ReDim Preserve" to redimension and preserve previous content.

try this set up also:
VB.NET:
Sub blahblah()
    Dim l As New License("mac", "license", "site")
    Licenses.Add(l)
End Sub

Private Licenses As New List(Of License)

Public Class License
    Private _mac As String
    Private _license As String
    Private _site As String

    Public Sub New(ByVal mac As String, ByVal license As String, ByVal site As String)
        _mac = mac
        _license = license
        _site = site
    End Sub

    Public ReadOnly Property Mac() As String
        Get
            Return _mac
        End Get
    End Property
    Public ReadOnly Property License() As String
        Get
            Return _license
        End Get
    End Property
    Public ReadOnly Property Site() As String
        Get
            Return _site
        End Get
    End Property

End Class
 
Thanks John,
I'll give it a go.

Is there any benifit of your suggetion of Private Licenses As New List(Of License) as opposed to ReDim Preserve when it comes to seaching the array?

I have has some troubles using the array.binarycompare(Licenses) and ended up resorting to a simple for next loop with an if licenses.mac = "00:00...", which I know is not efficient but works.
And yes I did do and array.sort(licnese) first
 
Is there any benifit of your suggetion of Private Licenses As New List(Of License) as opposed to ReDim Preserve when it comes to seaching the array?
No, it's just better OOP and easier to work with in general. If you need some special indexing or sorting you can also instead of List(Of Tvalue) use generics Dictionary, SortedList or SortedDictionary, all (Of Tkey, Tvalue).
 
Back
Top