Discuss Anything: Safe Ubound And Lbound - Discuss Anything

Jump to content

1

Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Page 1 of 1

Safe Ubound And Lbound Rate Topic: -----

#1 User is offline   imacul8 

  • Member [Level 3]
  • PipPipPipPipPipPip
  • Group: Kontributors
  • Posts: 95
  • Joined: 21-May 06
  • Gender:Male
  • Location:Adelaide, Australia
  • Current Mood: Current Mood: None Chosen

Posted 20 August 2006 - 02:35 AM

Ever wanted to use LBound and UBound to get arrays boundaries without jumping over error message when the array is empty? These functions will replace the ordinary LBound and UBound procedures so you don’t need to worry about errors. I've also included a way to get the dimensions of an array. Just paste the following code into a module, and the problem is solved.

Inputs: SafeUBound and SafeLBound: [Address to the array], [What dimension you want to obtain]
ArrayDims: [Address To the array]
Returns: As expected from the ordinary functions, except that they will return -1 when the array is empty.
Assumes: You obtain the address to an array by passing it to the VarPtrArray API call. So if you want to get the boundaries of an array called aTmp, you need to call the functions like this:
lLowBound = SafeLBound(VarPtrArray(aTmp))
lHighBound = SafeUBound(VarPtrArray(aTmp))
lDimensions = ArrayDims(VarPtrArray(aTmp))
When dealing With String arrays that isn't allocated at design time, you *must* add the value 4 To the lpArray-paramenter:
lLowBound = SafeLBound(VarPtrArray(aString) + 4)

Side Effects: Since the return value is minus when the array is empty it's a big chance you will get problems with minus dimensioned arrays, but who use them anyway?

Option Explicit


Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long


Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Public Function SafeUBound(ByVal lpArray As Long, Optional Dimension As Long = 1) As Long
	Dim lAddress&, cElements&, lLbound&, cDims%


	If Dimension < 1 Then
		SafeUBound = -1
		Exit Function
	End If
	CopyMemory lAddress, ByVal lpArray, 4


	If lAddress = 0 Then
		' The array isn't initilized
		SafeUBound = -1
		Exit Function
	End If
	' Calculate the dimensions
	CopyMemory cDims, ByVal lAddress, 2
	Dimension = cDims - Dimension + 1
	' Obtain the needed data
	CopyMemory cElements, ByVal (lAddress + 16 + ((Dimension - 1) * 8)), 4
	CopyMemory lLbound, ByVal (lAddress + 20 + ((Dimension - 1) * 8)), 4
	SafeUBound = cElements + lLbound - 1
End Function


Public Function SafeLBound(ByVal lpArray As Long, Optional Dimension As Long = 1) As Long
	Dim lAddress&, cElements&, lLbound&, cDims%


	If Dimension < 1 Then
		SafeLBound = -1
		Exit Function
	End If
	CopyMemory lAddress, ByVal lpArray, 4


	If lAddress = 0 Then
		' The array isn't initilized
		SafeLBound = -1
		Exit Function
	End If
	' Calculate the dimensions
	CopyMemory cDims, ByVal lAddress, 2
	Dimension = cDims - Dimension + 1
	' Obtain the needed data
	CopyMemory lLbound, ByVal (lAddress + 20 + ((Dimension - 1) * 8)), 4
	SafeLBound = lLbound
End Function


Public Function ArrayDims(ByVal lpArray As Long) As Integer
	Dim lAddress As Long
	CopyMemory lAddress, ByVal lpArray, 4


	If lAddress = 0 Then
		' The array isn't initilized
		ArrayDims = -1
		Exit Function
	End If
	CopyMemory ArrayDims, ByVal lAddress, 2
End Function


Hope this solves any problems u have of getting errors with empty arrays. <_<

Notice from BuffaloHELP:
Plagiarism is never tolerated in Trap17 forum. If you're going to copy the program code, do not leave out the copyright statement! This code is copyrighted. Source http://www.planetsourcecode.com/vb/scripts...&lngWId=-10

This post has been edited by BuffaloHELP: 20 August 2006 - 03:20 PM

0


Page 1 of 1

Other Replies To This Topic

#2 User is offline   Galahad 

  • Neurotical Squirrel
  • PipPipPipPipPipPipPipPipPip
  • Group: Kontributors
  • Posts: 590
  • Joined: 04-November 04
  • Gender:Male
  • Location:Novi Sad, Vojvodina
  • Interests:Programming, Hardcore dance/Trance/House music. Girls, girls and more girls ;) ... In short, everything youg people like, I like :D Oh yeah, and dont't forget politics :)
  • myCENT:48.25
  • Current Mood: Current Mood: None Chosen

Posted 21 August 2006 - 12:44 PM

I was just about to write it was copied from PSC, but BuffaloHELP got here before me <_<

Anyways, this code is good, since it uses Windows API, but this same code can be written in pure VB, with just a little error handling :D

Here it is:
Private Function SafeLBound() As Long
On Local Error GoTo SafeLBound_Err

SafeLBound = LBound(Niz)

SafeLBound_Exit:
Exit Function

SafeLBound_Err:
If Err.Number = 9 Then SafeLBound = -1
Resume SafeLBound_Exit
End Function

Private Function SafeUBound() As Long
On Local Error GoTo SafeUBound_Err

SafeUBound = UBound(Niz)

SafeUBound_Exit:
Exit Function

SafeUBound_Err:
If Err.Number = 9 Then SafeUBound = -1
Resume SafeUBound_Exit
End Function

This code will work only with project-level, or form/module-level arrays, since building a function that checks arrays, would require a function for each data-type in Visual Basic, because useing arrays as function or procedure parameters, requires passinge them by refference (ByRef), so you can for example declare input parameter as array of variants, and pass array of strings... That would cause another run-time error :P
0

Share this topic:


Page 1 of 1


Similar Topics Collapse

  Topic Forum Started By Stats Last Post Info
New Replies Icon Safe Mode Enabled Or Not? Computer Security Issues & Exploits martino 
  • 3 Replies
  • 800 Views
New Replies Icon 2nd Method Of Disabling Safe Mode On Winxp
Check this one out too
Tutorials odomike 
  • 3 Replies
  • 2,867 Views
New Replies Icon Some Guys, I Swear.....
possibly not safe for younger veiwers
The Vent Spathi 
  • 11 Replies
  • 4,344 Views
New Replies Icon Sick Of Being Infected By Viruses, Spyware, Malware, Etc.?
How to keep your data safe from the nasties of the Interwebs
Computer Security Issues & Exploits rayzoredge 
  • 4 Replies
  • 1,038 Views
New Replies Icon Discovery Touches Down Safely...
Congratulations to Nasa and humunity
North America kasm 
  • 2 Replies
  • 1,096 Views
New Replies Icon Home Security And Safety With Trusted Alarm Company
adt authorized dealer's special offer, just about $1 a day
Home Safety & Security BuffaloHelp 
  • 2 Replies
  • 1,870 Views
Hot Topic (New) Icon Which Is The Safest Internet Browser? Web Browsers friendforever 
  • 17 Replies
  • 1,360 Views
New Replies Icon Did You Know How Hard Workers Are Doing
to keep the internet safe?
Computer Security Issues & Exploits lailai 
  • 3 Replies
  • 1,888 Views
New Replies Icon Scanning For Virus In Safe Mode Computers master_bacarra 
  • 5 Replies
  • 2,181 Views
Hot Topic (New) Icon "safe" Peer To Peer?
Spyware and adware free?
Software JeffS 
  • 26 Replies
  • 6,921 Views
New Replies Icon Are You Ready For Terrorists?
really funny safety sheet parody
Other Funny Stuff psychiccyberfreak 
  • 6 Replies
  • 3,192 Views
New Replies Icon Php Safe Mode... On Or Off?
wordpress problems
Questions & Queries gummybear 
  • 1 Reply
  • 1,741 Views
New Replies Icon Safe@home- My New Site
Not 100% Done Yet
Website Showcase Moolkye 
  • 6 Replies
  • 745 Views
New Replies Icon Phone Gps
it's not as safe as you think
Mobility, Mobile Phones jopak134 
  • 4 Replies
  • 734 Views
New Replies Icon Kitchen Safety
A must !!
Home Safety & Security nirmaldaniel 
  • 1 Reply
  • 904 Views
New Replies Icon Easy And Safety Optimize In Vista * - - - -
By Disabling Unnecessary Vista Services.
Operating Systems puneye 
  • 3 Replies
  • 638 Views
New Replies Icon Internet Accesories
what you need to have for safe browsing
Computer Security Issues & Exploits ejohn 
  • 9 Replies
  • 1,742 Views
New Replies Icon Accident-free Highways
If you were offered safer roads...
Debates salamangkero 
  • 8 Replies
  • 820 Views
New Replies Icon Thieves Leave Note At Crime Scene: “(expletive) You And Your Safe” North America KainRacure 
  • 7 Replies
  • 910 Views
New Replies Icon What Is The Advantage Of Safe Mode Off?
Why is "safe mode off" a good thing?
your Secret HideOut tricky77puzzle 
  • 3 Replies
  • 970 Views

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Google Fan :-) We Recommend Firefox.     Customize / Settings Connect Us on facebook