X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Sub Class Property Array, VB.net 2013    
post Jun 23 2017, 05:45 PM

Posts: 30
Joined: 3-April 15

Hi Guys

I need your help with subclassing

currently im creating a class to hold information of a person but I'm facing challenges with how I want the class structuce is

I want to target this way of properties

Sub Sample

dim MyPerson as new person


'~> here's what I want to do but I can't


end sub


Public Class Person
Private sName As String
Public Property PName As String
Return sName
End Get
Set(value As String)
sName = value
End Set
End Property

end class
Go to the top of the page
post Jun 24 2017, 05:51 AM

UtterAccess Moderator
Posts: 12,091
Joined: 6-December 03
From: Telegraph Hill


Your property get/set code is a bit wrong.

You can do something like this:
' Class Person
Option Compare Database
Option Explicit

Private m_strName As String
Private m_colFriends As Collection

Public Property Get Name() As String
  Name = m_strName
End Property

Public Property Let Name(ByVal strName As String)
  m_strName = strName
End Property

Public Property Get Friends() As Collection
  If m_colFriends Is Nothing Then Set m_colFriends = New Collection
  Set Friends = m_colFriends
End Property

Public Function AddFriend(strName As String) As Boolean

  Dim f As New Person, _
      iFriends As Integer
  f.Name = strName
  With Me.Friends
    iFriends = .Count
    .Add f, f.Name
    AddFriend = (.Count = iFriends + 1)
  End With
End Function

Private Sub Class_Terminate()

  Dim i As Integer

  If Not m_colFriends Is Nothing Then
    If m_colFriends.Count Then
      For i = m_colFriends.Count To 1 Step - 1
        m_colFriends.Remove 1
      Next i
    End If
    Set m_colFriends = Nothing
  End If
End Sub

So you add a collection property to store friends, and an AddFriend() method to add new friends to the collection. Each friend is another instance of Person, so they can have friends too.

In your code:
Sub Sample()

  Dim MyPerson = New Person

  With MyPerson
    .Name = "Me"
    Call .AddFriend("You")
    Call .AddFriend("Him")
  End With

End Sub

There is also some cleanup code in the class' Terminate() event to remove the Friends collection from memory.

Note - all untested aircode!


Go to the top of the page
post Jun 25 2017, 04:15 AM

UtterAccess Moderator
Posts: 12,091
Joined: 6-December 03
From: Telegraph Hill

Yikes! Just realised this is the .Net forum - my apologies for polluting with a VBA answer. blush.gif

I'll try and re-work for .Net, but I may not get a chance for a while.

Go to the top of the page
post Jun 27 2017, 05:39 AM

Posts: 30
Joined: 3-April 15

Thanks for putting in some efforts to answer my query, I've tried my head around this and wasn't able to put it into a runnable codes

I'm still new with VB.net . Appreciate your help on this and can you point me out on the right term of what im trying to do. Im trying to google search but no luck but im guessing it is because I don't know the correct term. again Thanks!
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    22nd January 2020 - 03:09 AM