Checking Existence of Variable / Bypass the compiler check

M

Michiel

Hi,

I should like to write a sub that MIGHT write data to a public variable.
Meaning:
-If the Public variable exists the sub assigns a value.
-If the public variable does not exist. It just ignores it.

For example:
------------------------------------------------------------------------------
----------------
Option Explicit
Public strOutsideVar as String 'This line might be present

Sub TheSub()
Dim strInsideVar as String

strInsideVar = "Hallo" & strInsideVar
strOutsideVar = strInsideVar 'Only be executed if the public variable
(strOutsideVar is available)
End Sub

I have the "Option Explicit" switched on.
If the Public Variable is absent , upon running I get a compile error message.


How can I prevent that or how could I get the idea implemented?

Thanks

Michiel.
 
J

Jay Freedman

What you request is not possible. But I don't understand how, if you're
writing the code, you don't know in advance whether or not the variable
exists. If you expect to need it, just declare it. It won't hurt anything if
it's never used.

If the variable could be declared in a different module that may or may not
be present, that's a different story... but I still don't think there's any
solution.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
M

Michiel

Thanks Jay!

The reason why I want it is the fact that I want to use the sub in many
different applications.
If the Public Variable is necessary I needs to be declared of course but if
it is not used I do not want to pollute my codings with Publics wich are not
used.

M.

Jay said:
What you request is not possible. But I don't understand how, if you're
writing the code, you don't know in advance whether or not the variable
exists. If you expect to need it, just declare it. It won't hurt anything if
it's never used.

If the variable could be declared in a different module that may or may not
be present, that's a different story... but I still don't think there's any
solution.
[quoted text clipped - 26 lines]
 
J

Jay Freedman

As attractive as the idea of keeping your projects "clean" may be, it
doesn't do any good if it doesn't work.

Maybe you should think about passing the variable as an argument instead of
declaring it as Public. Then only the projects that call this procedure need
to know about the argument. It also reduces the chance that the Public
variable might be corrupted by other procedures that mistakenly use its
name.
Thanks Jay!

The reason why I want it is the fact that I want to use the sub in
many different applications.
If the Public Variable is necessary I needs to be declared of course
but if it is not used I do not want to pollute my codings with
Publics wich are not used.

M.

Jay said:
What you request is not possible. But I don't understand how, if
you're writing the code, you don't know in advance whether or not
the variable exists. If you expect to need it, just declare it. It
won't hurt anything if it's never used.

If the variable could be declared in a different module that may or
may not be present, that's a different story... but I still don't
think there's any solution.
[quoted text clipped - 26 lines]
 
M

Michiel

Jay,

I follow your wise advise!
Thanks, and,...

HAPPY HOLIDAYS!

M.

Jay said:
As attractive as the idea of keeping your projects "clean" may be, it
doesn't do any good if it doesn't work.

Maybe you should think about passing the variable as an argument instead of
declaring it as Public. Then only the projects that call this procedure need
to know about the argument. It also reduces the chance that the Public
variable might be corrupted by other procedures that mistakenly use its
name.
Thanks Jay!
[quoted text clipped - 20 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top