VB How To:

Sirp,

It really depends on what sort of array you are working with....

If you are working with a string array, it is do-able in a clever - but fast - manner. If you're not, then it is doable, but a bit slower.

Basically, for any array, just loop through from the lower bound to the upper bound and test each value.


Eg:

Code:
Private Function CheckArray(vArray() As Variant) As Boolean
Dim vTest As Variant
For Each vTest In vArray
    If (vTest <> 0) Then
        CheckArray = False
        Exit Function
    End If
Next vTest

CheckArray = True
End Function

For stringarrays, you can use the join function to check it a bit faster...

Code:
Private Function CheckStringArray(sArray() As String) As Boolean
Dim sTotal As String
sTotal = Join(sArray, "")
If Len(sTotal) = 0 Then
    CheckStringArray = True
Else
    CheckStringArray = False
End If
End Function

These functions will both return "true" for an empty array, false otherwise. :) I haven't tried it on control or object arrays though....
 
ainwood: that's how to test if all the members of an array are empty values. I want to know if the array doesn't have any items in it at all.

i.e. code equivalent to empty() for a container in C++. Not code equivalent to

find_if(v.begin(),v.end(),identity)

in C++, like you've shown....

(apologies if you don't happen to know C++, but that's what I program in, I've just got this annoying VB code I have to interface with a COM object I wrote and it's driving me insane!)

-Sirp.
 
Originally posted by Sirp
Cartouche Bee: sizeof? Is that a VB Function? What's the syntax for using it?

-Sirp.

Ooops, I thought you were asking for C++, sizeof() could be used for C.

You can use the LBound and Ubound functions in VB to determine how many elements are in an array (you will get a trappable error at this point if the array is indeed empty with no elements, and that is rather strange for VB). If the array is truely empty (from erasing?) you will get an error if you try to access it (lets say the LBound element). Trapping the error would then be the way to determine if your array is empty.

Are you passing the array to the Com Object?

If you need a more specific solution for a particular problem, maybe PM me.
 
CB is right - you can check for an uninitialised array by checking the Ubound, and then trapping the error. If an error is raised (err.number = 9) then the array is not initialised. Stupidity of VB not to have that function, but.... :rolleyes:

One special case is if you use a variant data type to hold the array: - you can then redimension the variable as an array, and use the IsEmpty function:

Code:
Sub testit()
Dim aArray As Variant
If IsEmpty(aArray) Then
    Debug.Print "Empty"
Else
    Debug.Print "Initialised"
End If
ReDim aArray(3) As Variant
aArray(3) = "s"
If IsEmpty(aArray) Then
    Debug.Print "Empty"
Else
    Debug.Print "Initialised"
End If
End Sub

Problem being that you are then using variants, so its not as efficient. Note too, that in the initial declaration, you must declare it as a variable only, not a variant array!

For the trappable error thingy:

Code:
Sub testit()
Dim aArray() As String
Debug.Print IsArrayEmpty(aArray)
ReDim aArray(5) As String
aArray(3) = "s"
Debug.Print IsArrayEmpty(aArray)
End Sub


Function IsArrayEmpty(InputArray As Variant) As Boolean
On Error GoTo ErrorHandler
    If UBound(InputArray) Then
        'An empty array will generate an error
    End If
    
    'else - no error so it is not populated!
    IsArrayEmpty = False
    Exit Function

ErrorHandler:
    IsArrayEmpty = True
End Function


Note that as soon as you give an array dimensions (ie from the redim above), it becomes 'initialised', with all elements set to the default data type (eg 0 for numeric arrays, an empty string for string arrays etc). Not sure how the C++ works.

You may also want to check out the LenB function - it is as close to the C / C++ SizeOf function as you are ever going to get with VB ;)
 
eek! So much code to tell if an array is empty! :) But thanks...it seems to work! Although my code just ended up having an 'isEmpty' flag.

CB: hmm....sizeof() wouldn't work in C or C++ either, since you can't have an empty array in C or C++. In fact a popular idiom to ensure a type is complete in C++ is

char type_not_fully_defined[sizeof(sometype)]

is sometype is complete, the code will compile fine and type_not_fully_defined will be optimized away since it's not referenced. If sometype is incomplete, then sizeof(sometype) is zero, making the array definition illegal, and the compiler will propagate an error, hopefully including the variable name, giving the programmer a clue as to what the problem is.

This is used to see if a type is complete before calling delete on that type for instance.

Of course, the C++ equivalent to VB arrays is the standard vector class template, but, you don't use sizeof() to find if one of them is empty either: you use the empty function.

-Sirp.
 
Sirp, you a 100% correct, I normally just think of the root of an array as a pointer, so my answer was over simplified. An unassigned pointer is set to null, which essentially is an empty array in most languages. Of course you can't point to a size 0 buffer, that would be a bug. :)
 
CB: well if you're using a dynamic/pointer-based array, you can't use sizeof to find the size of the array either....

char* ptr = NULL;

sizeof(ptr); //returns the size of a pointer, 4 on intel
sizeof(*ptr); //returns the same as sizeof(char) which is 1

But, I rarely use dynamic arrays directly in C++. It's far too unsafe, using standard containers normally, and builtin arrays when the speed is really necessary is far better imo.

-Sirp.
 
Originally posted by Sirp
CB: well if you're using a dynamic/pointer-based array, you can't use sizeof to find the size of the array either....

either?

You can use sizeof on statically dimensioned arrays and the sizeof function will also allow you determine the sizeof the elements in the array. But... they can't be empty by your own definition cause the compiler will optimize them out or halt on a compiler error. A pointer to an empty dynamic array will be set to NULL on intialization and memory deallocation.

Anyway the crux of the problem was that you were interfacing a com object with VB, with an array, that apparently was dynamic and at times empty. Sounds like you resolved the issue by modifying the VB software. :goodjob:
 
I just installed Visual Studio on my new Win XP machine, but was pretty disappointed that the all the controls didn't follow the theme in windows. The theme-controls have rounder corners and for example the command buttons gets an orange border when you move the mouse over it.
I therefore checked the microsoft knowledgebase and found an example of how to do it, but according to them it wasn't really supported by VB. The annoying part was that you had to make an additional file and place it in the same folder as the exe-file, and it only works after you've compiled the file.

My idea was that it would be nice if the program itself could create that extra file, so I placed the text found in the additional file in a resourcefile, which I added to the project and programmed it to create the file in the same folder.

So, if you want XP-themecontrols in your VB program without distributing the extra file with the program, use the project below as the base of your program, and before making the exe-file comment out "exit sub" in "Form_initialize" to activate the code.

If anyone have a better solution to the problem, please help me improve it:)
 
Funxus,

Looks good :goodjob:

What happens when you try the executable on a non-XP machine? When you call the control initialise function, does it crash or simply load whichever win 9x version of the same function?

I guess you could always wrapper this in a IfWinXP() type function :)
 
Yes, the program worked in Win ME, but it still creates the manifest. I guess it shouldn't be too hard to check which OS it is and only create the file if it's XP.:)
 
Originally posted by CornMaster
Winsock.

I've been messing with it...and are writing 4 different chat programs. All for different purposes. Anyway...I want to add file transfering to them.

Any tutorials?

Your wish is my command ;)

This is NOT my tutorial. can't remember where I found it, but it does give the basics. :) Hopefully its of some use. Perhaps you could weed out the rubbish and repost what you come up with? :)
 
Back
Top Bottom