none
Array Property RRS feed

  • Question

  • I haven't done any programming for years now and recently i decided to implement some code. I created a class and i tried to add some properties. However one of the property, I wanted it to be an array where its size will be decided while the code is running. I wrote:

    Public Class Blocks
        Property WhichBlocks(10) As Integer
    End Class

     

    (in this case i set my property as an array of size 10)
    I am sure the error it's something obvious but I don't get it.

    Thank you!

    Friday, July 14, 2017 5:37 PM

All replies

  • If you look at the error msg it tells all.  

    Auto-implemented properties cannot have parameters

    You would need to put in the get / set but I would rethink the use of an array and use a list instead.  Haven't used arrays for decades.

    So I would use Property WhichBlocks as list(of integer)


    Lloyd Sheen

    Friday, July 14, 2017 5:50 PM
  • Hi

    Here is some code that may be helpful. Notice that the ReDim has Preserve keyword to keep existing values. This is one way to use such a Property,but, you may be better using a List(Of Integer) instead as it offers a much better range of methods that you can use.

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim nb As New Blocks nb.WhichBlocks = {1, 2, 3} ReDim Preserve nb.WhichBlocks(10)

    ' Produces an array of 11 integers:
            ' 1,2,3,0,0,0,0,0,0,0,0
    End Sub Public Class Blocks Property WhichBlocks As Integer() End Class End Class

    Also, using arrays like this is very wasteful of memory as you could end up with several copies of the array in memory.


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, July 14, 2017 6:05 PM
    Friday, July 14, 2017 6:01 PM
  • I've never been a fan of using auto-implemented properties.

    If you're willing to give those up then like Lloyd suggested, a generic List(Of T) makes a lot of sense. One thing that I'll add here is that you might want to set it up as Read-Only.

    Just because it's read-only doesn't mean the consumer of the class can't add/remove/insert; it just means that it can't be set (to another list or to null):

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim example As New ClassA
    
            With example.Data
                .Add("This")
                .Add("That")
                .Add("The Other")
            End With
    
            Stop
    
        End Sub
    End Class
    
    
    
    
    
    Public Class ClassA
        Private _data As List(Of String)
    
        Public Sub New()
            _data = New List(Of String)
        End Sub
    
        Public ReadOnly Property Data As List(Of String)
            Get
                Return _data
            End Get
        End Property
    End Class

    If you run that you'll see all three of the strings in the list.

    *****

    If you're using an array so that the consumer of the class can't modify it then set the property to a read-only array:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim example As New ClassB(New String() _
                        {"This", "That", "The Other"})
    
            Stop
    
        End Sub
    End Class
    
    
    
    
    
    Public Class ClassB
        Private _data As List(Of String)
    
        Public Sub New(ByVal strings As IEnumerable(Of String))
            _data = New List(Of String)
    
            If strings IsNot Nothing Then
                _data.AddRange(strings)
            End If
        End Sub
    
        Public ReadOnly Property Data As String()
            Get
                Return _data.ToArray
            End Get
        End Property
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, July 14, 2017 6:23 PM
  • Hi Shrang,

    You want to create a class and try to add some properties, I do one simple sample that you can refer to:

    Public Class Form1
       
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim test As New test()
            test.stationNames = New String() {"1", "2", "3"}
            For i As Integer = 0 To test.stationNames.Length - 1
                Console.WriteLine("{0}", test.stationNames(i))
            Next
    
        End Sub
    End Class
    
    Public Class test
        Private _name As String
        Private nameList() As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(value As String)
                _name = value
            End Set
        End Property
        Public Property stationNames As String()
            Get
                Return nameList
            End Get
            Set(ByVal Value As String())
                nameList = Value
                ' Insert code to store nameList values.
            End Set
        End Property
    End Class

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 17, 2017 6:49 AM
    Moderator