Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I have a class set up as follows:

VB
Public Class Tags

    Public Sub New()

    End Sub

    Public Sub New(ByVal sTagName As String, _
                   ByVal sTagValue As String)

        _tagName = sTagName
        _tagValue = sTagValue

    End Sub

    Private _tagName As String
    Public ReadOnly Property TagName() As String

        Get
            Return _tagName
        End Get

    End Property

    Private _tagValue As String
    Public ReadOnly Property TagValue() As String

        Get
            Return _tagValue
        End Get

    End Property

End Class


I have a function I call that will load an array of Tags.

What I have so far is this

VB
Public Class WMAID3Tag.vb

    dim aryOfTags as Tags()

    public function GetTags(ByVal fn as String) as Tags()

        ' Do some stuff to get a tagName and tagValue
        aryOfTags = New Tags() {New Tags(tagName, tagValue)

        ' Look for more tags
        aryOfTags = New Tags() {New Tags(tagName, tagValue)

        ' Found all the tags there are
        Return aryOfTags

    End Function

End Class



The problem I am having is that aryOfTags never grows beyond 1 element. Each new tag found simply replaces the last.
Posted
Updated 26-Apr-12 14:21pm
v2
Comments
VJ Reddy 26-Apr-12 20:22pm    
Edit: pre tag for VB code added.

1 solution

The reason is that in the Function
VB
public function GetTags(ByVal fn as String) as Tags()

    ' Do some stuff to get a tagName and tagValue
    aryOfTags = New Tags() {New Tags(tagName, tagValue)

    ' Look for more tags
    aryOfTags = New Tags() {New Tags(tagName, tagValue)

    ' Found all the tags there are
    Return aryOfTags

End Function

Under Look for more tags, An new array of tags is assigned to aryOfTags, which replaces the array of tags already assigned under the first step. So, only aryOfTags contains only one element assigned in the second step.
Two return two tags, the following modification can be done.
VB
Public Class WMAID3Tag.vb

    dim aryOfTags(1) as Tags

    public function GetTags(ByVal fn as String) as Tags()

        ' Do some stuff to get a tagName and tagValue
        aryOfTags(0) = New Tags(tagName, tagValue)

        ' Look for more tags
        aryOfTags(1) = New Tags(tagName, tagValue)

        ' Found all the tags there are
        Return aryOfTags

    End Function
End Class

However, it is preferable to use List to dynamically assign number of elements without first defining the size. In the above function the size of array is declared as 2 Nos. If more are expected, then size shall be fixed accordingly.
The function using List is as follows
VB
public function GetTagsList(ByVal fn as String) as List(Of Tags)
    dim listOfTags as new List(Of Tags)
    ' Do some stuff to get a tagName and tagValue
    listOfTags.Add(New Tags("tag1", "value1"))

    ' Look for more tags
    listOfTags.Add(New Tags("tag2", "value2"))
    listOfTags.Add(New Tags("tag3", "value3"))

    ' Found all the tags there are
    Return listOfTags

End Function
 
Share this answer
 
v2
Comments
rctaubert 27-Apr-12 12:05pm    
Reddy,
Thank you for your quick reply. Wow! Not only did you solve my problem you taught me something new and improved my code. Thank you very much.
VJ Reddy 27-Apr-12 12:19pm    
You're welcome.
Thank you for the response.

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