Solved

default list for various data types

Posted on 2006-06-26
8
183 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 142

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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