473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Source code macros in VB.NET

I'm a C/C++ programmer diving into VB.NET. Is there a
VB.NET equivalent to a C/C++ #define macro? For example, I
have 15 variables that I need to do the same thing to ....
in C, I'd do the following:

#define VALIDATE_AND_INIT(zMember, zNew) \
if (zNew == NULL) \
strcpy(zMember, ""); \
else \
strcpy(zMember, zNew);

Then in my code I could go:

VALIDATE_AND_INIT(FirstName, NewFirstName);
VALIDATE_AND_INIT(LastName, NewLastName);
....

Thereby making a large block of repeated code much more
manageable and easier to read and understand.

Does VB.NET provide such a mechanism?

Thanks,
-Chris
Nov 20 '05 #1
3 1676
Chris,
VB.NET does not support #define macros!

What I would do is define a sub, which is also what I would do in C++, as it
is more type safe!

Private Sub ValidateAndInit(ByRef zMember As String, ByVal zNew As
String)
If zNew Is Nothing Then
zMember = String.Empty
Else
zMember = ZNew
End If
End Sub

ValidateAndInit(FirstName, NewFirstName)
ValidateAndInit(LastName, NewLastName)

Because the function is so short there is a good chance that it will be
in-lined by the JIT compiler at runtime, so I don't have a concern with
introducing a new function. Also, because it is a function I can overload
it.

Private Sub ValidateAndInit(ByRef zMember As Integer, ByVal zNew As
Object)
If zNew Is Nothing Then
zMember = 0
Else
zMember = CInt(ZNew)
End If
End Sub

Private Sub ValidateAndInit(ByRef zMember As Date, ByVal zNew As Object)
...

ValidateAndInit(Age, NewAge)
ValidateAndInit(Birthdate, NewBirthdate)

Note on the ValidateAndInit for Date & Integer, I defined zNew as Object
under the assumption they were coming from either controls (strings) or a
database where System.DBNull may be a value. You should define zNew as
appropriate for your use!

Alternatively I would define a function, instead of a Sub, however then
overloading gets trickier. A sub with a ByRef for the member is IMHO the
cleaner solution. NOTE: Because I am changing the caller's member field,
ByRef for zMember is needed.

Hope this helps
Jay

"Chris Edgington" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
I'm a C/C++ programmer diving into VB.NET. Is there a
VB.NET equivalent to a C/C++ #define macro? For example, I
have 15 variables that I need to do the same thing to ....
in C, I'd do the following:

#define VALIDATE_AND_INIT(zMember, zNew) \
if (zNew == NULL) \
strcpy(zMember, ""); \
else \
strcpy(zMember, zNew);

Then in my code I could go:

VALIDATE_AND_INIT(FirstName, NewFirstName);
VALIDATE_AND_INIT(LastName, NewLastName);
...

Thereby making a large block of repeated code much more
manageable and easier to read and understand.

Does VB.NET provide such a mechanism?

Thanks,
-Chris

Nov 20 '05 #2
Thanks.

I noticed you set an empty string with String.Empty. Is
this the same as using ""?

Thanks,
-Chris
Nov 20 '05 #3
Chris,
Yes. String.Empty is a Shared Readonly field that contains the value "", so
yes they are equivalent. Shared Readonly fields are very similar to
constants.

I know of no real advantage of using String.Empty over "" for strings.
However for other classes, such as EventArgs, it is better to use
EventArgs.Empty over creating a new EventArgs object. As you save creating
an object that will later need to be garbage collection.

Strings are special in that String.Empty will be interned (folded) to the
exact same object as the "" literal, hence no real advantage.

Note this is not to say that you cannot create an empty string that is not
String.Empty ;-)

Hope this helps
Jay

<an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
Thanks.

I noticed you set an empty string with String.Empty. Is
this the same as using ""?

Thanks,
-Chris

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: K_Lee | last post by:
While trying to understand some issues to python's implementation of Socket, I recorded an outline to Python's socket object interface to the native implementation in a webblog type documents in...
5
by: cedric buerfent | last post by:
Hello NG, I ve a question concerning automatic generated source code. When I program the declarations of a class (and save it in a file called myclass.hh for example), its a boring work to...
0
by: Huihong | last post by:
Please check out our source code search engine here, http://www.codase.com e.g., search main method,...
20
by: Clark | last post by:
Hi all. I'm looking for good C source code to study and be able to advance my C programming skills. Do you recomend any open source project in particular that in your opinion has good writen C...
6
by: Xing Xu | last post by:
Hi guiders, sorry , since I don't know which group suit for this question,I just post this question at these group. As we know , we can get the run-time call graph by some proved tools . now...
8
by: Huihong | last post by:
Please check out our newly released source code search engine here, http://www.codase.com e.g., search socket method call,...
0
by: Huihong | last post by:
Please check out our source code search engine here, http://www.codase.com e.g., search the "main" method,...
25
by: mdh | last post by:
Hi Group, Not looking for an answer, but more of an explanation. Thinking back to those heady days when you had the time to do them, may I ask this. Exercise 1-22 asks for a program to "fold"...
5
by: Redmond | last post by:
Celerity is a powerful application for analysing C/C++ files. * It can process millions of source code lines. It supports standard C/C++. For each project, it shows the source files, include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.