|
|
|
| Web Hosting Guide |
![]() ![]() |
How To Access Child Node / Collection Items In A Class, Ways of accessing the child nodes of a Tree Node |
Jan 3 2007, 11:31 AM
Post
#1
|
|
|
Premium Member Group: [HOSTED] Posts: 445 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:65.75 |
CODE Dim A As New TreeNode MsgBox(A.Nodes(15).Text) MsgBox(A.Nodes("WTF").Text) Here are two ways of accessing the child nodes of a Tree Node, in this case, node A. In the first case, we have accessed the child node with Index = 15 of node A. In the second case, we have accessed the child node "WTF" by its key. I want to be able to access the members of the Responses class in a similar fashion. Example:- CODE MsgBox(Responses("ASL").Text) Anybody knows how to go about creating the class? What the... I missed out on the subject of the Topic.. [note=Mark420]
I fixed the Topic title and Description for you [/note] |
|
|
|
Jan 3 2007, 06:17 PM
Post
#2
|
|
|
Super Member Group: [HOSTED] Posts: 500 Joined: 5-November 06 Member No.: 17,016 myCENTs:79.88 |
QUOTE(turbopowerdmaxsteel @ Jan 3 2007, 07:31 PM) [snapback]95014[/snapback] CODE Dim A As New TreeNode MsgBox(A.Nodes(15).Text) MsgBox(A.Nodes("WTF").Text) CODE MsgBox(Responses("ASL").Text) I don't quite see the connection between the 2. Do you mean that you want to access the Responses as a Nodes? Are you referring to VB or ASP? It seems more like ASP to me. I think you need to give us more information regarding what you're trying to achieve. |
|
|
|
Jan 3 2007, 06:28 PM
Post
#3
|
|
|
Premium Member Group: [HOSTED] Posts: 445 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:65.75 |
Its in VB .NET. I used the Node just an example, the same thing can be done using the Items collection of the ListView class. The idea is to create a collection of the class Responses and be able to access the individual items by using "key as String" instead of the regular way of doing "Index as Integer".
Plainly speaking, we can access the individual elements of an array via the index number. How do we access the elements by a string "key" as we can in PHP. I don't think its possible to do the same for arrays in VB .NET, but doing that for a class is certainly. The TreeNode and the ListViewItem classes can be seen exhibiting this property. |
|
|
|
Jan 3 2007, 07:00 PM
Post
#4
|
|
|
Super Member Group: [HOSTED] Posts: 500 Joined: 5-November 06 Member No.: 17,016 myCENTs:79.88 |
QUOTE(turbopowerdmaxsteel @ Jan 4 2007, 02:28 AM) [snapback]95036[/snapback] Its in VB .NET. I used the Node just an example, the same thing can be done using the Items collection of the ListView class. The idea is to create a collection of the class Responses and be able to access the individual items by using "key as String" instead of the regular way of doing "Index as Integer". Plainly speaking, we can access the individual elements of an array via the index number. How do we access the elements by a string "key" as we can in PHP. I don't think its possible to do the same for arrays in VB .NET, but doing that for a class is certainly. The TreeNode and the ListViewItem classes can be seen exhibiting this property. You can use Microsoft.VisualBasic.Collection. It has a built in key access for it's item. If you need to implement into your own class then you can try this CODE Public Class Responses Private _responses As Microsoft.VisualBasic.Collection Public Sub New() Me._responses = New Microsoft.VisualBasic.Collection End Sub Default Public ReadOnly Property Item(ByVal Key As String) As String ' You can define the default type for this property ' Only readonly as per default of Collection.Item Get 'You need to cast it if you have turn on Strict Mode Return CType(Me._responses(Key), String) End Get End Property ' This is to ease the adding function ' In case you have a different type you want to store Public Sub Add(ByVal Value As String, ByVal Key As String) Me._responses.Add(Value, Key) End Sub End Class to use it CODE Dim a As Responses = New Responses a.Add("testing", "key1") MsgBox(a("key1")) You can add as many member function as you like to customize or overrides the original base function. I didn't override the Add function exactly, but you should override all of "Add" if you want to implement any one of it, that way is to avoid confusion or improperly accessing the incorrect base function. |
|
|
|
Jan 5 2007, 05:37 AM
Post
#5
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,248 Joined: 29-January 05 From: Bangkok, Thailand Member No.: 2,411 myCENTs:19.10 |
You can definitely use the Microsoft.VisualBasic.Collection class as faulty.lee showed you - but a more elegant approach would be to create a separate class (inherited from CollectionBase) to hold your items and then use this class inside your Responses class.
For example - say I create a class called ResponsesCollection: CODE Imports System.Collections Public Class ResponsesCollection Inherits CollectionBase End Class CollectionBase provides you with an inbuilt searchable & sortable collection called List which can be accessed by both keys and index number. Any class inheriting CollectionBase gains these properties... The members methods which can operate on the List are Add, AddRange, Remove, RemoveAt, Clear, IndexOf, Item, Contains etc. Once you inherit a class from CollectionBase, you don't need to declare the List separately... you can directly access it as List. A simple implementation of the members would be... CODE Imports System.Collections Public Class ResponsesCollection Inherits CollectionBase Public Sub Add ( value As Object ) List.Add ( value ) End Sub ' AddRange Public Sub AddRange( value[] As Object ) ' Add items ForEach vItem As Object in value Add( vItem ) End Sub ' Remove Public Sub Remove( value As Object ) ' Remove item List.Remove( value ) End Sub ' RemoveAt Public Sub RemoveAt( index As Integer ) ' If index is out of bounds - throw exception If index > Count - 1 Or index < 0 Throw New ArgumentOutOfRangeException( "Index out of bounds." ) ' If all is fine, remove item at specified index List.RemoveAt( index ) End Sub ' Clear Public Sub Clear() ' Clear List List.Clear() End Sub ' IndexOf Public Function IndexOf( value As Object ) As Integer Return List.IndexOf( value ); End Function ' Item Public Function Item( index As Integer ) As Object ' If index is out of bounds - throw exception If index > Count - 1 Or index < 0 Throw New ArgumentOutOfRangeException( "Index out of bounds." ) Return CType( List(index), Object) End Function ' Insert Public Sub Insert( index As Integer, value As Object ) List.Insert( index, value ) End Sub ' Contains Public Function Contains( value As Object ) As Boolean Return List.Contains( value ); End Function End Class Once you're done with the collection class as shown above, go over to your Responses class and implement this class by a familiar object name (say Item) inside it... make sure you instantiate it in the constructor. CODE Public Class Responses Public Item As ResponsesCollection ........ ........... ' Constructor Public Sub New ' Other constructor code ...... ' Instantiate Collection Item = New ResponsesCollection .... End Sub End Class From now on whenever you instantiate Responses class anywhere in code, you'll be able to access/modify the list by Responses.Item (x) or Responses.Item.Add (x) or Responses.Item.RemoveAt (x) etc.... Let me know if this helped Cheers, m^e |
|
|
|
Jan 5 2007, 06:00 AM
Post
#6
|
|
|
Super Member Group: [HOSTED] Posts: 500 Joined: 5-November 06 Member No.: 17,016 myCENTs:79.88 |
QUOTE(miCRoSCoPiC^eaRthLinG @ Jan 5 2007, 01:37 PM) [snapback]95166[/snapback] You can definitely use the Microsoft.VisualBasic.Collection class as faulty.lee showed you - but a more elegant approach would be to create a separate class (inherited from CollectionBase) to hold your items and then use this class inside your Responses class. Hi miCRoSCoPiC^eaRthLinG, I do agree with you, in fact i tries to avoid the VisualBasic namespace as much as possible. But looking at the situation, i think visualbasic.collection is much suited to get started. Maybe after turbopowerdmaxsteel got used to it, then he can try the actual more powerful collectionbase class. turbopowerdmaxsteel, one thing to take note though, the starting index is different for visualbasic.collection and the collectionbase class. Visual basic one started with 1, for backward compatibility, where as collectionbase or system.collections starting with 0, similar to array EDIT : You can also inherits the visualbasic.collection. my last comment QUOTE You can add as many member function as you like to customize or overrides the original base function. I didn't override the Add function exactly, but you should override all of "Add" if you want to implement any one of it, that way is to avoid confusion or improperly accessing the incorrect base function. actually referring to inheritance. Sorry for the mistake Regards faulty This post has been edited by faulty.lee: Jan 5 2007, 06:03 AM |
|
|
|
Jan 5 2007, 08:37 AM
Post
#7
|
|
|
Premium Member Group: [HOSTED] Posts: 445 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:65.75 |
Woah, that's a lot of detail. Thanx for the help, guys. It'll take a while to get accustomed to all these. I'll start off with the Microsoft.VisualBasic.Collection implementation.
This post has been edited by turbopowerdmaxsteel: Jan 5 2007, 08:54 AM |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
14 | sandeep | 2,315 | 3rd November 2009 - 09:11 PM Last post by: iG-rick |
|||
![]() |
10 | himanshurulez | 3,510 | 16th October 2009 - 08:59 AM Last post by: iG-mike |
|||
![]() |
11 | soleimanian | 4,373 | 8th October 2009 - 07:57 PM Last post by: HannahI |
|||
![]() |
8 | vicky99 | 3,449 | 8th October 2009 - 07:56 PM Last post by: HannahI |
|||
![]() |
11 | dserban | 1,381 | 8th October 2009 - 07:47 PM Last post by: HannahI |
|||
![]() |
4 | Kushika | 2,842 | 23rd September 2009 - 03:16 PM Last post by: iG-Lynn |
|||
![]() |
11 | ginginca | 3,516 | 22nd September 2009 - 07:40 AM Last post by: iG-Vinay Kr. Sharma |
|||
![]() |
2 | ljj | 782 | 18th July 2009 - 11:49 AM Last post by: iG-vasanth.a |
|||
![]() |
9 | Arkane | 1,487 | 17th June 2009 - 03:00 AM Last post by: thepinder |
|||
![]() |
1 | astdesy | 413 | 27th May 2009 - 04:57 PM Last post by: dougeg |
|||
![]() |
1 | kencycles | 778 | 7th May 2009 - 03:43 AM Last post by: iG-merv |
|||
![]() |
6 | dhanesh | 1,184 | 25th April 2009 - 08:43 AM Last post by: iG-faulkj |
|||
![]() |
1 | falcorassassin | 649 | 8th April 2009 - 11:37 AM Last post by: iG-Francis |
|||
![]() |
0 | joe.k | 294 | 26th March 2009 - 05:17 PM Last post by: joe.k |
|||
![]() |
4 | Jeigh | 3,240 | 26th February 2009 - 03:40 AM Last post by: jelsa |
|||
|
Lo-Fi Version | Time is now: 21st November 2009 - 04:21 PM |
© 2009 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Jan 3 2007, 11:31 AM




