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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you set a User Defined Variable to Empty?

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I need to set some Public User Defined Variables to Empty. Is it possible? If so how?
 
Empty" is a keyword to deallocate an Object.

A text variable can be set to an empty string.

Dim var1 as string
var1 = ""

I believe a variant variable can be set to null.
Dim vari1 as Variant
vari1 = null

 
I would have to do that to each variable for each of my variables and still wouldn't help as I need the variable Empty when it checks later. Under some conditions the variable will be Empty so the code has to work differently when it does. Because these are used Public then if it runs through again these may not be empty when they get to the code and will slip through when it should actually be empty. Also because of the way VB treats Empty and Null variables I have to be able to set them back to "Empty".
 
Empty" is a keyword to deallocate an Object?

I think you may be referring to "Nothing" not empty.
Set myObj = nothing

Empty refers to a variant that has not been initialized as seen below.

Dim varVariant As Variant
Debug.Print IsEmpty(varVariant) 'True
varVariant = Null
Debug.Print IsNull(varVariant) 'True
Debug.Print IsEmpty(varVariant) 'False
varVariant = Empty
Debug.Print IsNull(varVariant) 'False
Debug.Print IsEmpty(varVariant) 'True

Dim myObj As Object
Debug.Print IsEmpty(myObj) 'False
Set myObj = Nothing
Debug.Print IsEmpty(myObj) 'False
Debug.Print IsNull(myObj) 'False

Dim myString As String
Debug.Print myString = "" 'True
Debug.Print IsEmpty(myString) ' False

Now what you describe sounds like poor coding. I think what you are asking, is to do is perform something different the first time a specific check is made. This could then be done with a boolean variable.

Dim blnAlreadyChecked

if not blnAlreadyChecked then
code
blnAlreadyChecked = true
else
code
 
Empty refers to a variant that has not been initialized as seen below.

Yes that is what I mean. Let me put as much of the code as I'm allowed and maybe that will explain better.

Here is an example of one type. There are about 10 of various sizes and using various data types
Code:
Public Type SelectedFiles
    Id As String
    Name As String
    LName As String
    Member As String
    Format As String
    TotalCount As Long
End Type
Public CurrentSelectedFiles() as SelectedFiles
I later do a ReDim if there is data to put into it (There may be no data so this wouldn't be used at all) depending on how many I need.

Then later I have to check if any data has been added to CurrentSelectedFiles.
Code:
If IsEmpty(CurrentSelectedFiles(0).TotalCount) = False Then SpecifyFileSelections
So If CurrentSelectedFiles(0).TotalCount is not Empty (IsEmpty = False) then it will run the function SpecifyFileSelections. If the variable has not been set then it will move on to the next bit of code and not run SpecifyFileSelections.

So I run this once and lets say it is empty the first time. Everything will run fine. I run it again and this time it sets the data. Everything will run fine. Next I run it again and there is no data to set. It will break as the global variable is not Empty because the last time it ran there was data to put into it. So when I'm done I have to set CurrentSelectedFiles to Empty so that the next time it runs if there is no data IsEmpty will equal True.

Thanks for any help on this.
 
Sorry. Part of what you said clicked. I had tried xxxxvariable = Empty on some and it failed. I just tried it again on all of them and it works if the variable is an array to equal it to empty, but if it isn't an array then it gives the error "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions". The Types are created in the exact same place the only difference being that most are arrays while the ones that get the error are not.
 
NM. I'm still getting the same error on all of it if I try to xxxVariable = Empty.
 
Can't you just redim the array? If you ReDim the array to the samne size without using the PRESERVE keyword, it achieves the desired result.
I would do all of this with a custom "SelectedFile" class and a "SelectedFiles" collection. OOP is the way to go in my book.
 
Thank you. I was going to say that doesn't work, but I was being stupid...or rather not watching what I was actually typing. I did ReDim at the start, but stupidly continued to use Preserve without even thinking about it. DOH!!! Thanks for the help.

I've never done much with classes. My formal programming was Basic, Pascal, C++ and then stopped half way through my OOP class (right when we started really using classes). Since then everything (Borland C++ Builder, VB, VBA) has been self taught.

The reason I say all that is because I don't see that a class would be all that useful. These are not objects to me but sets of data. So I create a data type to lump all my like data together.
 
Thanks. That is really cool for future use, but doesn't do what I need in this instance unless I set every variable in my data type to variant. Which would be an option, but Redim the variable is working. I had not heard of Erase before and can see it will be helpful in other things. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top