-
Jan 13th, 2010, 07:22 AM
#1
Thread Starter
Hyperactive Member
[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:
Public Class MyClass
Private c_Text As String()
Public Sub New()
ReDim c_Text(0)
End Sub ' New
ReadOnly Property NumText() As Integer
Get
Return UBound(c_Text) + 1
End get
End Property ' NumText
Property Texts(ByVal Index As Integer) As String
Get ' Return Text
If (0 <= Index) And (Index < NumText()) Then
Return c_Text(Index)
Else
Return ""
End If
End Get
Set(ByVal value As String)
If (0 <= Index) And (Index < NumText()) Then
c_Text(Index) = value
Else
ReDim Preserve c_Text(NumText() + 1)
c_Text(NumText()) = value
End If
End Set
End Property ' Texts
End Class ' My Class
My Problem starts when I try to use the Property Texts.
Causes no errors.
However, when I try to use the Set part I get an error 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
-
Jan 13th, 2010, 08:31 AM
#2
Re: Property Access Problem
Just compiled your example and it works fine.
Here's the complete code:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mc As New MyClass1
mc.Texts(0) = "Test"
Debug.Print(mc.Texts(0)) ' It works and outputs Test
End Sub
End Class
Public Class MyClass1
Private c_Text As String()
Public Sub New()
ReDim c_Text(0)
End Sub ' New
ReadOnly Property NumText() As Integer
Get
Return UBound(c_Text) + 1
End Get
End Property ' NumText
Property Texts(ByVal Index As Integer) As String
Get ' Return Text
If (0 <= Index) And (Index < NumText()) Then
Return c_Text(Index)
Else
Return ""
End If
End Get
Set(ByVal value As String)
If (0 <= Index) And (Index < NumText()) Then
c_Text(Index) = value
Else
ReDim Preserve c_Text(NumText() + 1)
c_Text(NumText()) = value
End If
End Set
End Property ' Texts
End Class ' My Class
I just want to point out that it's better to use collection instead of array.
-
Jan 13th, 2010, 08:41 AM
#3
Re: Property Access Problem
Here, a much easier way to do the same thing:
vb Code:
Public Class MyClass1 Private c_Text As List(Of String) Public Sub New() c_Text = New List(Of String) End Sub ' New ReadOnly Property NumText() As Integer Get Return c_Text.Count End Get End Property ' NumText Default Property Texts(ByVal Index As Integer) As String Get Return c_Text(Index) End Get Set(ByVal value As String) If c_Text.Count <= Index Then c_Text.Add(value) Else c_Text(Index) = value End If End Set End Property End Class ' My Class
-
Jan 13th, 2010, 09:47 AM
#4
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
-
Jan 13th, 2010, 10:47 AM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|