Safe Ubound And Lbound

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > VB Programming

Safe Ubound And Lbound

imacul8
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?

CODE
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. smile.gif

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

 

 

 


Reply

Galahad
I was just about to write it was copied from PSC, but BuffaloHELP got here before me laugh.gif

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 wink.gif

Here it is:
CODE

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 smile.gif

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. php ubound lbound - 6.63 hr back. (1)
  2. vba lbound - 23.93 hr back. (1)
  3. vb varptrarray strings - 34.55 hr back. (1)
  4. php ubound - 93.40 hr back. (1)
  5. vb6 ubound empty array - 129.42 hr back. (1)
  6. ubound php - 125.07 hr back. (2)
  7. lbound ubound - 153.82 hr back. (1)
  8. lbound and ubound - 160.13 hr back. (1)
  9. ubound and lbound in vba - 166.44 hr back. (1)
  10. safeubound vb6 - 169.55 hr back. (1)
  11. vb copymemory arrays - 173.60 hr back. (1)
  12. varptrarray - 205.03 hr back. (1)
  13. php ubound - 210.36 hr back. (1)
  14. vb6 ubound - 0.97 hr back. (2)
Similar Topics

Keywords : safe, ubound, lbound

  1. Phone Gps
    it's not as safe as you think (4)
  2. No "safe" Music
    Even Streams can be copied (7)
    It doesn't take a computer hacker with special apps to steal music. Anyone who runs Windows can
    steal music, even if you are streaming, encoding, using special file types, whatever. I know that
    many individuals use special flash programs to stream music, but it still isn't safe, and
    here's why: Many improfessional streaming programs acutally download the song to
    'stream' it. Many of you are web-savvies, but for those of you who are not, there is a
    directory in Windows called "Temporary Internet Files." Now, the best streaming programs don't
    use....
  3. How To Keep Your Pc Fast And Safe
    (3)
    Well guys, this is maybe a really basic tutorial that everybody knows, but I just thinking to share
    it here as maybe not everybody in this forum know about it (correct me if I'm wrong LOL). Ok,
    lets start it out. Defrag Your Harddisk(s) Frequency : weekly/monthly Defragging your harddisk
    makes access to files faster. Do regular defrag to get maximum performance of your harddisk.
    Personally, I don't recommend defragging using Windows' default disk defragmenter, try using
    program such as Defraggler, JKDefrag, or etc. (Defraggler is my favourite!) Tweak....
  4. Thieves Leave Note At Crime Scene: “(expletive) You And Your Safe”
    (7)
    Thieves broke into the Des Moines Golf and Country Club on Monday morning. They used a reciprocating
    saw to attempt to cut into the safe but couldn’t get in. In staid they stole some tennis rackets and
    shoes and left a note to the people who owned the place. The note was scribbled on plain white
    paper in large ink letters saying “(Expletive) you and your safe” The 2 thieves then attempted to
    escape in their vehicle when a security officer saw some of the stolen good fall out of the back and
    took down their license plate which led cops to the thieves themselves. http:....
  5. Did You Know How Hard Workers Are Doing
    to keep the internet safe? (3)
    Note: This post was made in: bbs.duba.net Made by: 禹林 Translated by: lailai
    Saturday's breakfast was only a cup of milk, and 2 bread loafs. This was the food for them for
    over 3 weeks, when the first "auto" virus was created. "Eat quick, and send the source to me that
    you said last night. After bug fixes and updates, the internet was finaly out of the auto virus
    world. However, they must keep checking for new viruses. After putting the virus into OLLYDBG,
    the source code appered in seconds. Endtask, Edit regedit, deleted SSDT and the anitvirus ....
  6. Tips On Using The Internet Safely
    how to be safe on the internet (5)
    This topic is about internet safety and tips on how to protect your children on the internet 1.Warn
    your children about the dangers of the internet. 2.turn on all of your search engine filters. 3.turn
    on your computer's built in firewall or buy one from a computer store. Try Norton. 4.Always
    check up on your child/children every 5 minutes or so. And last of all ask your kids what they have
    been up to at the end of their session. Advice for parents with an internet connection.
    /cool.gif" style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> Pl....
  7. Are Artificial Sweeteners 'safe'?
    (4)
    This was in the news today (local news channel): QUOTE A small bit of research is indicating
    that zero-calorie sweeteners could be making it harder for you to lose weight. How? By tricking
    your body into thinking sweetness doesn't mean extra calories. Basically, they're saying
    these artificial substitutes could make it a little harder to gauge caloric intake (which could lead
    to gaining weight). In addition, they're also pointing to a loose connection between high sales
    of diet soda and a rise in obesity. Considering the widespread use of sweeteners now ....
  8. What Is The Advantage Of Safe Mode Off?
    Why is "safe mode off" a good thing? (3)
    I've seen a lot of webhosts where a paid upgrade is "Safe Mode OFF". I know here at Trap17 that
    it's turned off by default on all webhosting, but I'd just like to know, why is this an
    advantage? What features that are enabled are so important that they have to be disabled in safe
    mode?....
  9. Safe Mode Of Php Configuration
    in subdomain of trap17.com (4)
    hallo master.. how to change php configuration from safe_mode off become ON in this trap17.com
    subdomain? please someone tell me, /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" />....
  10. Sick Of Being Infected By Viruses, Spyware, Malware, Etc.?
    How to keep your data safe from the nasties of the Interwebs (4)
    Viruses, spyware, malware, adware, and all that extraneous bull that we have to deal with nowadays
    are becoming more frequent. Obviously we don't want this crap on our computers so I advise you
    take precautions. * Avoid downloading anything from sites or people you don't know. Duh. *
    Don't even bother looking at attachments in spam. Duh. * If you receive an e-mail from someone
    you don't know, don't click on any of the links. Duh. * Anything other than a multimedia
    file or a text file is able to harbor extra crap you're not going to want. This ....
  11. Cant Boot In Safe Mode [resolved]
    (3)
    I have a Dell Optiplex GX150, im trying to boot in safe mode to change the display driver for my
    older monitor to work with the system im using.. When i turn the computer on it shows this at the
    top: QUOTE F2 = Setup F12 = Boot Menu When i goto boot menu it shows this: QUOTE Boot
    Device Menu ============ 1. Normal 2. Diskette Drive 3. Hard Disk Drive C: 4. IDE CD-ROM Device
    i've tried the F8 & Holding Shift Methods, i still cannot find the Safe Mode Option /mad.gif"
    style="vertical-align:middle" emoid=":angry:" border="0" alt="mad.gif" />....
  12. Over-the-counter Medications Safe For Pets
    For anyone who's pet is ill:) (0)
    QUOTE Over-The-Counter Medications You Can Give To Your Pet Info is Listed in the following
    order: Medicine Notes Dosage How Often Buffered Aspirin For dogs only. Pain relief,
    anti-inflammatory. 1 tablet per 60 Lbs. 2 times per day Baby Aspirin For dogs only. Pain relief,
    anti-inflammatory. 1 tablet per 15 Lbs. 2 times per day Benedryl® Treat allergies, itching,
    reaction to insect stings, etc. 1 mg. per 1 Lb. 2 times per day Dramamine® Not for animals with
    glaucoma or bladder problems. Reduce car sickness. 3-25 Lb. pet - 12.5 mg. 26-50 Lb. pet - 25 mg. 51
    Lb. ....
  13. 2nd Method Of Disabling Safe Mode On Winxp
    Check this one out too (2)
    Another method to disable safe mode is to remove the registry key: Backup the key before deleting
    it, of course. This method does not remove the boot menu. However, it disables Windows' ability
    to actually boot to safe mode. Attempting to boot to safe mode will cause a quick Blue Screen of
    Death (BSOD) and restart.....
  14. Disabling Safe Boot On Windows Xp
    Learn how to do it (3)
    I know that there has been many at times, when for some reason or the other, people would want to
    disable the ability of Windows XP to boot into safe mode. Even though that this is not a very thing
    advisable thing to do, but still, people, due to some reasons best known to them would always want
    to do this. The other day, a friend of mine who works in (manages is the right word) a cyber cafe
    was heads over toes, trying to figure out to to disable the safe mode boot in Windows XP. His
    reason..... customers are hacking into their computers and browsing free of charge by b....
  15. I Need Free Php Hosting?( Suitable For Phproxy Safe Mode Off)
    (4)
    hi i am an iranian, and need an free hosting that support php( suitable for PHproxy safe mode off
    ) ,beacuse i need to make an PHProxy against filtering iran goverment,culd any body help me?
    thanks /sad.gif" style="vertical-align:middle" emoid=":(" border="0" alt="sad.gif" />....
  16. How Safe Are Condoms?
    Even if they break? (24)
    Apparantly, a small percentage (Somewhere between 5 and 2%) of condoms break...now I don't know
    if this is true, or how much it is on a scale of how many condoms are used /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> But condoms also have
    spermicides in that are meant to kill sperm, so if it splits, how safe is it? (Don't worry,
    I've not gone and split one and just making sure I'm ok...well, that I know of /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" />!)....
  17. Brainless Is Going To Ireland
    safe yourselve while you can (0)
    In a few days, I'll finish my community service as required by the draft. But what then? I
    actually wondered about this when I began the service 8 1/2 months ago and applied to an orphanage
    in South Africa but I never got any response from them :/ Half a month ago I decided that I
    can't wait for a response any longer (well, 6 months should be long enough for anyone to at
    least confirm that they received an application) and began working on the Camphill application form
    which I sent in to some irish Camphill Communities . To my surprise I received the first re....
  18. No Startup Response
    Won't respond to disc or cd or Can't Safe Mode (9)
    A friend has a Toshia Satellite 2530CDS Laptop which he re-formatted the Hard-drive and tried to
    load Windows XP Pro onto. (It previously had Win 2000) And now the thing won't start-up to a
    disc or even trying to load windows 98se from a CD fails. The CD does go round and round, but it
    doesn't do anything. The display "lights up", and occasionally a Splash page appears, but
    pressing F3 or F8 or any other key combination doesn't get into Safe Mode, even. Almost like it
    has lost its BIOS, I guess. What to do??? I have a Windows 98SE disk and key to use from a....
  19. Java Applets
    Are they safe (5)
    hello. I'm am a new member and i am trying to carifty one thing, are applets safe?. fort people
    who don't know, applets, by using a decompiler, can be easily hacked. i want to know that if
    people can make a program safe, so that hackers wont be able to get it. thanks in advance....
  20. Scanning For Virus In Safe Mode
    (5)
    is it better to scan your system in safe mode? i've been having virus problems recently and i
    really need to scan my system, but whenever i try to do it, the system hangs because of the virus
    (or at least that's what i think is happening). at first i assumed it was hardware problems, but
    i checked it and i didn't find any problem. i recently tried scanning in safe mode, but after i
    switched back to normal mode, it seemed as if the scanning didn't help at all. or maybe it
    worked, that i just didn't notice it.....
  21. Safe_mode Disabled?
    PLEASE READ (4)
    Hey i was wondering is trap17.net alow scripts like proxys because finding a good host is hard all
    hell for a proxy site Sorry if im a n00b and all but im desperate for a free host with safe mode
    disabled I NEED HELP any one have a .com website that wants to partner with my site (not up yet
    wsunlock.com pm me) ....
  22. Security Not Safe
    (2)
    Hi everyone!!!!!!! This is the last one!! /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> Ok guys, I heard
    somewhere that if we protect some page with password, it is steel not safe at all, if we dont hace a
    secure connction (http s ://...) How is it true? is there a posibility that some one can see a page,
    even if it is protected by password? (the scrit in tha page don't allow IDs that didn't past
    from the login page) is that script sufficent? thanks a lot to every one /biggrin.....
  23. Plan To Upload Joomla
    how abaout safe-mode in Trap17 (5)
    iam going to install joomla, but i can find free webhost that have safe-mode = off. how about Trap17
    ? cause its really important to have that safe-mode = off because i can't extract (mkdir) to
    directory when i install a component for joomla. thanx.....
  24. Is It Safe To Run A Computer Off A Non-grounded Line?
    Are there any dangers, and if so how much? (15)
    Hey all. The outlets in my room are not grounded, and I want to eventually get a computer up and
    running in there. Do you think that it is dangerous to do so, and if so, how dangerous is it? Are
    we talking computer-dying dangerous, electrocuting myself dangerous, or some other kind of
    dangerous? Thanks, folks.....
  25. Safe@home- My New Site
    Not 100% Done Yet (6)
    I am working on this business, and I want the website to look have way decent. it doesn't have
    to be Microsoft perfect, ya know? But I would like your opinions. http://www.dbdbdesigns.roxr.com
    The above address is a temp site till I work out all the bugs, get my domain registered and
    transfer everythign over to trap. /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" /> Thanks in advance. ....
  26. Xoftspy! A good spyware remover & scanner
    XoftSpy spywere redmover (very safe) (5)
    This is the best spywere program xoftspy.com try it! ....
  27. Is Paying With Paypal Safe?
    your opinion and experiences please (35)
    http://www.paypalwarning.com/ I have considered opening a pay pal account to conduct transactions
    on the web but after reading this webite I am not so sure... It offers plenty of warnings and
    stories of people that were scammed. I am curious about your opinions and experiences if any. EDIT
    : Please check your spellings....
  28. Paypal.. Safe Or Not?
    well is it? (15)
    Recently I stumbled upon this story. -->
    http://web.ask.com/redir?u=http%3A%2F%2Ftm...snip&Complete=1 I have no idea why it is so long.
    Anyway it states how many people are choosing to create their own merchant accounts rahter than use
    the free Paypal. I am asking because I have a paypal account and I don't find it to be that bad.
    Though I am not entirely sure because I have only used it once. Do any of you not trust paypal?....
  29. Winxp-sp2 Firewall
    Safe enough if all files backed-up on external hard drive? (9)
    Other posts about firewalls haven't satisfied my questions about Windows XP Service Pack 2's
    Firewall. I want to know if there's any reason why not to trust it alone. I already have Norton,
    but the damn thing takes up so many resources... this is my situation: I'm just about to get a
    new external hard drive, on which I will back-up all my files. Then I'm going to reformat my
    hard drive, as it hasn't been since I got this comp two years ago and it needs it. I don't
    have a whole lot of resources to compromise on this computer, so I want to mini....
  30. liposuction
    safe? (8)
    Oprah Winfrey has said that she's never recieved liposuction. I don't know if this is true
    or not, and I don't care. But my question is, if she is so rich, why wouldnt' want to
    liposuction. I am just curious as to how dangerous it is because I'm expecting to get some lipo
    done after i have all the kids i want so I can get my body back to normal faster as I exersize. I
    mean, I'm going to try my hardest not to gain too much and to exersize while i'm pregnate
    and all, i'm just curious as to what are the dangers of it. i'm NOT pregnate or ....

    1. Looking for safe, ubound, lbound

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for safe, ubound, lbound

*MORE FROM TRAP17.COM*
Similar
Phone Gps - it's not as safe as you think
No "safe" Music - Even Streams can be copied
How To Keep Your Pc Fast And Safe
Thieves Leave Note At Crime Scene: “(expletive) You And Your Safe”
Did You Know How Hard Workers Are Doing - to keep the internet safe?
Tips On Using The Internet Safely - how to be safe on the internet
Are Artificial Sweeteners 'safe'?
What Is The Advantage Of Safe Mode Off? - Why is "safe mode off" a good thing?
Safe Mode Of Php Configuration - in subdomain of trap17.com
Sick Of Being Infected By Viruses, Spyware, Malware, Etc.? - How to keep your data safe from the nasties of the Interwebs
Cant Boot In Safe Mode [resolved]
Over-the-counter Medications Safe For Pets - For anyone who's pet is ill:)
2nd Method Of Disabling Safe Mode On Winxp - Check this one out too
Disabling Safe Boot On Windows Xp - Learn how to do it
I Need Free Php Hosting?( Suitable For Phproxy Safe Mode Off)
How Safe Are Condoms? - Even if they break?
Brainless Is Going To Ireland - safe yourselve while you can
No Startup Response - Won't respond to disc or cd or Can't Safe Mode
Java Applets - Are they safe
Scanning For Virus In Safe Mode
Safe_mode Disabled? - PLEASE READ
Security Not Safe
Plan To Upload Joomla - how abaout safe-mode in Trap17
Is It Safe To Run A Computer Off A Non-grounded Line? - Are there any dangers, and if so how much?
Safe@home- My New Site - Not 100% Done Yet
Xoftspy! A good spyware remover & scanner - XoftSpy spywere redmover (very safe)
Is Paying With Paypal Safe? - your opinion and experiences please
Paypal.. Safe Or Not? - well is it?
Winxp-sp2 Firewall - Safe enough if all files backed-up on external hard drive?
liposuction - safe?
advertisement



Safe Ubound And Lbound



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE