New topic: 

itterate a collection ...

<http://forums.realsoftware.com/viewtopic.php?t=46454>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        BrianOBrien          Post subject: itterate a collection 
...Posted: Sat Jan 05, 2013 6:35 pm                                 
Joined: Wed Jul 07, 2010 2:55 pm
Posts: 644
Location: University Of Calgary, Faculty Of Medicine                dim c as 
new Collection

c.add 1, "ID"
c.add "Loise Lane", "Name"
c.add "Reporter",  "JobTitle"
c.add 85000, "Salary"

for i as integer=1 to c.Count
  ListBox1.AddRow
  ListBox1.Cell(ListBox1.LastIndex, 0) = c(i).Key
  ListBox1.Cell(ListBox1.LastIndex, 1) = c(i)
next i


1) I'm trying to make a listbox with two columns, but I'm not sure how to get 
key.
2) As the Value can be a variant, I'm not sure how I can determine if I need to 
do to handle data types that the ListBox can't handle.      
_________________
If at first you don't succeed... Try REALBasic.  
                             Top                BrianOBrien          Post 
subject: Re: itterate a collection ...Posted: Sat Jan 05, 2013 7:27 pm          
                       
Joined: Wed Jul 07, 2010 2:55 pm
Posts: 644
Location: University Of Calgary, Faculty Of Medicine                I ended up 
making my own collection class...
      
_________________
If at first you don't succeed... Try REALBasic.  
                             Top                doofus          Post subject: 
Re: itterate a collection ...Posted: Sat Jan 05, 2013 7:43 pm                   
              
Joined: Thu Sep 10, 2009 2:50 am
Posts: 329
Location: Santa Cruz, CA, USA                BrianOBrien wrote:I ended up 
making my own collection class...
As a subclass?

Class CollectionPlusKey inherits Collection
  
  Protected keys(0) As String
  
  Function Key(index As integer) As String
  if index >= 1 and index <= keys.Ubound then
  return keys(index)
  else
  return ""
  end
  End Function
  
  Sub add(Value as Variant, Key as String = "")
  keys.Append Key
  if Key = "" then
  Super.Add(Value)
  else
  Super.Add(Value, Key)
  end
  End Sub
  
End Class

//=============================

dim c as new CollectionPlusKey

c.add 1, "ID"
c.add "Loise Lane", "Name"
c.add "Reporter",  "JobTitle"
c.add 85000, "Salary"

for i as integer=1 to c.Count
  ListBox1.AddRow ""
  ListBox1.Cell(ListBox1.LastIndex, 0) = c.Key(i)
  ListBox1.Cell(ListBox1.LastIndex, 1) = c.Item(i)
next i


I thought Collection was new but its in my old RB, never noticed this:)   
                             Top                BrianOBrien          Post 
subject: Re: itterate a collection ...Posted: Sat Jan 05, 2013 7:53 pm          
                       
Joined: Wed Jul 07, 2010 2:55 pm
Posts: 644
Location: University Of Calgary, Faculty Of Medicine                Thank you, 
Doofus. I guess it is a subclass of collection.

Oh and thanks for clarifying the syntax of a subclass.
I'm so use to the IDE writing the Class Definition in RealBasic for me, that I 
never really learned what it was.

Thanks.
B.      
_________________
If at first you don't succeed... Try REALBasic.  
                             Top                ktekinay          Post subject: 
Re: itterate a collection ...Posted: Sun Jan 06, 2013 12:50 am                  
               
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 336
Location: New York, NY                Do not use a Collection. Collection is an 
old data type that predated the current Dictionary. I'm surprised Collection 
has not been marked deprecated yet, although the LR does say:
Quote:Therefore, we recommend that you use the Dictionary class rather than the 
Collection class whenever possible.
With a dictionary, you would do something like this:
dim d as Dictionary

d.Value( "ID" ) = 1
d.Value( "Name" ) = "Lois Lane"
d.Value( "JobTitle" ) = "Reporter"
d.Value( "Salary" ) = 85000

// To fill in the Listbox
dim k() as variant = d.Keys
dim v() as variant = d.Values
for i as integer = 0 to k.Ubound
  dim thisKey as variant = k( i )
  dim thisValue as variant = v( i )
  
  // Test for string (one way)
  dim test as string
  try
  test = thisKey.StringValue
  catch
  thisKey = "Not a string"
  end
  try
  test = thisValue.StringValue
  catch
  thisValue = "Not a string"
  end
  
  ListBox1.AddRow( thisKey, thisValue )
next i
      
_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.
  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to