Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to tell if keyname exist in collection without watching a runtime

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have a collection, which use also assign unique keynames
'like below

[tt]
ReadCol.rows.Add tmprow, tmprow.Variable
[/tt]

tmprow.Variable being the keyname

anyways, if I did SetFile.Rows(&quot;SomeKeyName&quot;) if that doesnt exist I Get a runtime error '5', is there a way to check to see if a keyname exist before hand? [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
This is how I do one of them.

[tt]
...
On Error Resume Next
Location = Left(VariableLbl(Index).Caption, Len(VariableLbl(Index).Caption) - 3)
Set row = SetFile.rows(Location)
If row Is Nothing Then
Set row = SetFile.rows(Location & &quot;Start&quot;)
End If
FrontFont = row.Value
BackFont = ValueText(Index).Text
...
[/tt]
basically I have it resume next, which would leave rows as a nothing, then I Check to see if it is, then I try it with the other naming convention.
so it would skip any bad errors, but I would prefer using a more clean method to determine if that key exists. [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
Hiya,

Here's a way of checking for a certain thing in a collection of objects, although I can't quite follow your code, so this'll be just sort of generic code:

for each CollectionObject in Collection
if CollectionObject.UniqueKey = DesiredKey then
set ObjectVariable = CollectionObject
else
'The CollectionObject does not exist in the Collection
end if
next


Where;

Collection: your collection of objects
CollectionObject: an object variable of the object type found in the Collection
ObjectVariable: an object variable of the object type found in the Collection, to assign a CollectionObject to
DesiredKey: a variable containing the UniqueKey you are looking for

Of course, to be able to use the For Each..Next syntax, your Collection must support the following property, which allows you to enumerate the collection:

Public Property Get NewEnum() As IUnknown
Set NewEnum = mCol.[_NewEnum]
End Property


where mCol is a collection variable to hold the collection itself.

If you could show some of your variable declarations so it's more obvious what is what in your code, it might be easier to apply this generic snippet to your code, but I'm sure you get the idea anyway...

Toodle-pip,

Paul
[sig][/sig]
 
1) a For Each is much too slow to the method I'm using now(just with the error trapping)
2) you cannot retreive the keyname for a collection like that, wouldnt allow it.

but since you didnt understand what I meant

if I did

set row = SetFile.rows(&quot;The5thRow&quot;)

it'll return the row that holds that keyname, it does it for me. problem tho, if &quot;The5thRow&quot; doesnt exist in the collection a runtime error occurs. There are a few naming conventions I need to check for , if one doesnt exist I Try the other (As you seen above) but I Want to check to return a True/False is the keyname exist in the collection W/O sequencially searching the whole collection (if I wanted to do that, I woulda just made a static array)

understand? [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
Karl,
What's wrong with the method you're using? It's definitely the fastest way to do what you want.

Code:
Function KeyExists(collection As collection, keyname As String) As Boolean
    Dim junk
    On Error Resume Next
    junk = collection(keyname)
    KeyExists = Not (Err = 5)
    Err.Clear
End Function
[sig]<p>nick bulka<br><a href=mailto: > </a><br><a href= </a><br>Get your technical books at Bulka's Books<br>
[/sig]
 
I was just hopping there was a method, without generating or catching an error. But if you think my method is the best for the task, then I'll stick with it.

Thanks [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
You could use a Dictionary instead of a collection. Then you can use the Exists method. In fact a Dictionary has many advantages over a collection, such as being faster. [sig][/sig]
 
have an example?
also this collectoins of &quot;rows&quot; are the rows of a file (some marked as &quot;noDataRow&quot;) that allow me to change constants and resave them after I've done preveiwing and changing each page. [sig]<p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top