#define in VB .net?

J

Joe HM

Hello -

I would like to find a way to use a pre-processor directive like the
#define in C++. It seems like the #const in VB can only be used within
other # statements but not outside.

The problem is that I want to set a variable at the very top of the
*.vb file so users will see it and can adjust it if necessary. Then I
want to use the variable in a function call later in the code.

I tried adding a namespace with a const variable at the very top but it
will not work since I have Import statements later on.

Is there a way to do something similiar to what the #define directive
does?

Thanks!
Joe
 
G

Guest

#Const TEST = True

I'm not sure what you mean when you say #Const can only be used within other
# statements however.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
H

Herfried K. Wagner [MVP]

Joe HM said:
I would like to find a way to use a pre-processor directive like the
#define in C++. It seems like the #const in VB can only be used within
other # statements but not outside.

The problem is that I want to set a variable at the very top of the
*.vb file so users will see it and can adjust it if necessary. Then I
want to use the variable in a function call later in the code.

Mhm... I would use a property instead of the variable which can be set to a
certain value at /runtime/ rather than letting the user set the variable to
a certain value at compile-time.
 
G

Guest

Ahhh - I see what you're trying to do now - you want to declare a constant
with #Const and access that constant from a non-pre-processor directive
statement.
Sorry - you can't do that.

But you can easily define constants at the top of your class:
Public Class TestClass
Private Const Test As Boolean = False

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
J

Joe HM

Hello -

I wanted the variable be a revision number that the user changes
whenever a change is made to the code. If this is located at the top
of the file with the revision history it would be less likely for the
user to forget to update this number.

How would I set a property?

Thanks,
Joe
 
J

Joe HM

Hello -

Yeah ... I would like to use the constant in a Console.WriteLine() or
similiar call. That's too bad that there is no such functionality. I
guess I will have to go with the definition at the top of the class.

Thanks,
Joe
 
H

Herfried K. Wagner [MVP]

Joe HM said:
I wanted the variable be a revision number that the user changes
whenever a change is made to the code. If this is located at the top
of the file with the revision history it would be less likely for the
user to forget to update this number.

I don't really understand the purpose of the revision number. In .NET,
version numbers are specified on a per-assembly basis. You can change your
project's version number in the "AssemblyInfo.vb" file. More information on
versioning in .NET can be found here:

Structure of version numbers and methods to determine the version number
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=versioning&lang=en>
 
J

Joe HM

Hello -

The revision number I need is totally unrelated to the revision number
of the project in the AssemblyInfo.vb file. The *.vb file is a script
for a test procedure and stand-alone without the *.vbproj or *.sln.

Thanks,
Joachim
 
G

Guest

Here's a work-around, if you prefer setting at the top of the file:

#Const Test = True

Public Class TestClass
Private TestValue As Boolean
Public New()
#If Test Then
TestValue = True
#Else
TestValue = False
#End If
End Sub
End Class
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 

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

Similar Threads


Top