Solved

default list for various data types

Posted on 2006-06-26
8
185 Views
Last Modified: 2010-04-23
vb2005

when i declare a data type (e.g. Dim total as Double), what is the default value which variable total gets instanced with?

i would like a list or a link that tells which are the defaulted values for each data type. my goal is to not be redundant if i declare like Dim total as Double = 0, because maybe 0 is the default instanced value for that variable.

thanks
0
Comment
Question by:newyuppie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 16986224
all numeric data defaults to 0

>my goal is to not be redundant if i declare like Dim total as Double = 0
you are actually not redundant with that, but rather show best practice, as you clearly show what the value is initialized with, and avoid compiler warnings due to uninitialized values.

Also, in the final .exe it will be the same, as the compiler will put the (default) assignment anyhow.
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16986310
what does string default to?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16986453
Default string value is Nothing, since this is reference type.
Default value of numeric type is 0, Boolean is false.

Test your program with Microsoft FxCop program, it shows all unnecessary initializations (among lot of other very useful things).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16986464
actually, in .net, variables all default to null object, hence are not initalialzed (my comment above was in the context of vb6, sorry)
you need to initalize all your variables, of any data type.
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16986663
angel, so you mean i need to put a value to every declaration?

alex thanks for some clarifications.

finally, this question would obviusly go in the following direction:
is there a performace improvement if i declare and initialize every variable to its default value? i mean, does
Dim total as Double , and
Dim total as Double = 0

have any performance gains or any other benefit besides good coding practices?

thanks to both for your answers
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16986666
Not exactly, Microsoft coding guidelines recommend to avoid unnecessary initialization of value types, if initial value is equal to default. All default null is right for reference types.

FxCop handles this situation:
http://www.gotdotnet.com/team/fxcop/
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 350 total points
ID: 16986704
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16986758
great, thanks to both of you for the answers. i will increase points to split between both. thanks again
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question