VS 2008 [RESOLVED] Property Access Problem-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Property Access Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] Property Access Problem

    I have created a class with a Private Dynamic Array of String.

    I wrote the following class with properties to access the array.

    VB Code Code:
    1. Public Class MyClass
    2. Private c_Text As String()
    3.  
    4. Public Sub New()
    5.    ReDim c_Text(0)
    6. End Sub ' New
    7.  
    8. ReadOnly Property NumText() As Integer
    9.    Get
    10.       Return UBound(c_Text) + 1
    11.    End get
    12. End Property ' NumText
    13.  
    14. Property Texts(ByVal Index As Integer) As String
    15.         Get ' Return Text
    16.             If (0 <= Index) And (Index < NumText()) Then
    17.                 Return c_Text(Index)
    18.             Else
    19.                 Return ""
    20.             End If
    21.         End Get
    22.         Set(ByVal value As String)
    23.             If (0 <= Index) And (Index < NumText()) Then
    24.                 c_Text(Index) = value
    25.             Else
    26.                 ReDim Preserve c_Text(NumText() + 1)
    27.                 c_Text(NumText()) = value
    28.             End If
    29.         End Set
    30. End Property ' Texts
    31.  
    32. End Class ' My Class

    My Problem starts when I try to use the Property Texts.

    VB Code Code:
    1. MsgBox (Texts(0))

    Causes no errors.

    However, when I try to use the Set part I get an error message.

    VB Code Code:
    1. Texts ("Test Message")

    Property access must assign to the property or use its value.

    In the documentation I have looked at (Various books and MSDN and Online
    Help) it clearly indicates that properties can have parameters, and my
    declaration of Texts throws up no errors.

    But nowhere can I find an example showing a Property with a Parameter List being used.

    Can you advise where I am going wrong, and how to correct it please.
    So far all the examples I have found on MSDN and VBForums
    Signature Under Construction

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Property Access Problem

    Just compiled your example and it works fine.
    Here's the complete code:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim mc As New MyClass1
    5.         mc.Texts(0) = "Test"
    6.  
    7.         Debug.Print(mc.Texts(0)) ' It works and outputs Test
    8.     End Sub
    9. End Class
    10.  
    11. Public Class MyClass1
    12.     Private c_Text As String()
    13.  
    14.     Public Sub New()
    15.         ReDim c_Text(0)
    16.     End Sub ' New
    17.  
    18.     ReadOnly Property NumText() As Integer
    19.         Get
    20.             Return UBound(c_Text) + 1
    21.         End Get
    22.     End Property ' NumText
    23.  
    24.     Property Texts(ByVal Index As Integer) As String
    25.         Get ' Return Text
    26.             If (0 <= Index) And (Index < NumText()) Then
    27.                 Return c_Text(Index)
    28.             Else
    29.                 Return ""
    30.             End If
    31.         End Get
    32.         Set(ByVal value As String)
    33.             If (0 <= Index) And (Index < NumText()) Then
    34.                 c_Text(Index) = value
    35.             Else
    36.                 ReDim Preserve c_Text(NumText() + 1)
    37.                 c_Text(NumText()) = value
    38.             End If
    39.         End Set
    40.     End Property ' Texts
    41.  
    42. End Class ' My Class



    I just want to point out that it's better to use collection instead of array.

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Property Access Problem

    Here, a much easier way to do the same thing:

    vb Code:
    1. Public Class MyClass1
    2.     Private c_Text As List(Of String)
    3.  
    4.     Public Sub New()
    5.         c_Text = New List(Of String)
    6.     End Sub ' New
    7.  
    8.     ReadOnly Property NumText() As Integer
    9.         Get
    10.             Return c_Text.Count
    11.         End Get
    12.     End Property ' NumText
    13.  
    14.     Default Property Texts(ByVal Index As Integer) As String
    15.         Get
    16.             Return c_Text(Index)
    17.         End Get
    18.         Set(ByVal value As String)
    19.             If c_Text.Count <= Index Then
    20.                 c_Text.Add(value)
    21.             Else
    22.                 c_Text(Index) = value
    23.             End If
    24.         End Set
    25.     End Property
    26.  
    27. End Class ' My Class

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    Re: Property Access Problem

    Yeah, a List (of T) thoroughly replaces dynamic arrays.

    The problem with your test is that you assign to a property. The argument you needed to pass it was the index, an integer, not a string.

    Texts(NumText)="Test Message"

    was what you needed, though I didn't read the code carefully enough to know if it is NumText or NumText+1 that should be the argument.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: Property Access Problem

    Thanks guys,

    I am just starting to teach myself VB.Net, only used VBA up until now.
    Signature Under Construction

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.