UtterAccess.com
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    
 
   
khieyzer
post Jun 23 2017, 05:45 PM
Post#1



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

myPerson.Name="Me"

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

myPerson.Friends(0).Name="You"
myPerson.Friends(1).Name="Him"

end sub

sample:

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

end class
This post has been edited by khieyzer: Jun 23 2017, 05:48 PM
Go to the top of the page
 
cheekybuddha
post Jun 24 2017, 05:51 AM
Post#2


UtterAccess VIP
Posts: 9,264
Joined: 6-December 03
From: Telegraph Hill


Hi,

Your property get/set code is a bit wrong.

You can do something like this:
CODE
' 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:
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!

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
cheekybuddha
post Jun 25 2017, 04:15 AM
Post#3


UtterAccess VIP
Posts: 9,264
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.

d

--------------------


Regards,

David Marten
Go to the top of the page
 
khieyzer
post Jun 27 2017, 05:39 AM
Post#4



Posts: 30
Joined: 3-April 15



Hi
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    17th November 2017 - 08:00 PM