My Assistant
![]()
Custom Search
|
![]() ![]() |
![]() |
![]() 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 |
![]() Post#2 | |
![]() UtterAccess Moderator Posts: 11,909 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 |
![]() Post#3 | |
![]() UtterAccess Moderator Posts: 11,909 Joined: 6-December 03 From: Telegraph Hill ![]() | Yikes! Just realised this is the .Net forum - my apologies for polluting with a VBA answer. ![]() I'll try and re-work for .Net, but I may not get a chance for a while. d |
![]() 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! |
![]()
Custom Search
|
![]() | Search Top Lo-Fi | 11th December 2019 - 07:38 PM |