Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > How do i see of array element exists?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2004, 06:55 PM
Joshm00 Joshm00 is offline
Centurion
 
Join Date: Mar 2003
Posts: 89
Default How do i see of array element exists?


How would I go about getting a true or false value to see if an array element exists...



if wsc(number) exists then
value = 1
else
value = 0
end if



... how would i do this?
Reply With Quote
  #2  
Old 01-26-2004, 07:04 PM
GavinO's Avatar
GavinO GavinO is offline
Coder of Fortune

Retired Leader
* Expert *
 
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
Default

How about:
Code:
function ElementExists(Array() As Any, Index as Integer) As Boolean if index>=ubound(array) and index<=lbound(array) then elementexists=true else elementexists=false endif end function
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer
Reply With Quote
  #3  
Old 01-26-2004, 07:08 PM
Joshm00 Joshm00 is offline
Centurion
 
Join Date: Mar 2003
Posts: 89
Default

that doesnt quite seem to work...

How would I use that from a command button?

I want to check to see if # 3 in the array exists how would i do that from a command button?
Reply With Quote
  #4  
Old 01-26-2004, 07:22 PM
Joshm00 Joshm00 is offline
Centurion
 
Join Date: Mar 2003
Posts: 89
Default

that code doesnt work
Reply With Quote
  #5  
Old 01-26-2004, 07:27 PM
Joshm00 Joshm00 is offline
Centurion
 
Join Date: Mar 2003
Posts: 89
Default

maybe i need to give you more info so u can make the code more specific to what i need so i can understand it..

My arraY name is wsc and i want to check to see if element # 3 exists in this array.


I dont know how to use the function you gave me
Reply With Quote
  #6  
Old 01-26-2004, 07:36 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,909
Default

If you just want to see if the index exist, then you can check to if the
index >= lbound(array) And index <= ubound(array) as GavinO meant to
do (I often reverse my > and < myself if I don't check myself, don't know why).
Passing the array as a variant, also seems to work. I don't know if this
is what you meant by "exist".

Code:
Dim B(1 To 20) As Integer Private Function ElementExists(A As Variant, Index As Integer) As Boolean If Index >= LBound(A) And Index <= UBound(A) Then ElementExists = True Else ElementExists = False End If End Function Private Sub Command1_Click() Print ElementExists(B, 15) Print ElementExists(B, 30) End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #7  
Old 01-26-2004, 09:41 PM
Diurnal Diurnal is offline
Enthusiast

Retired Leader
* Expert *
 
Join Date: Apr 2002
Location: Bellevue, WA.
Posts: 3,233
Default

Sometimes you need to check if the array is dimensioned or has been erased. In the example above, an error would occur if the array was not dimenesioned. This post by Onerr0r discusses this very well and offers a great piece of code to do the checking.
Reply With Quote
  #8  
Old 01-26-2004, 10:51 PM
Joshm00 Joshm00 is offline
Centurion
 
Join Date: Mar 2003
Posts: 89
Default

I came up with my own solution to my problem, I used the on error event..you can see what i used it for on a winsock application in the communications section... thanks for all of your guys' help tho. it is much appreciated.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Standards and Practices loquin Tutors' Corner 10 07-28-2006 12:16 PM
Write 2D array to a file Dolptrmn File I/O and Registry 4 11-08-2003 07:42 PM
Determining if an element of a control array exists webbone General 4 08-20-2003 12:54 PM
Array and String Functions rhawke General 5 07-10-2003 02:33 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->