473,406 Members | 2,549 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,406 software developers and data experts.

preventing default value in vb.net

Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example
Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable test
was never initialized. I want to prevent giving default value of 0.

Thanks
May 25 '06 #1
7 2008

Justin wrote:
Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example
Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable test
was never initialized. I want to prevent giving default value of 0.


No. From the specification of VB (my emphasis):

"
4.8 Variables
A variable represents a storage location. Every variable has a type
that determines what values can be stored in the variable. Because
Visual Basic is a type-safe language, every variable in a program has a
type and the language guarantees that values stored in variables are
always of the appropriate type. ***Variables are always initialized to
the default value of their type before any reference to the variable
can be made.*** It is not possible to access uninitialized memory.
"
The default value for an Integer is, as you know, zero.

--
Larry Lard
Replies to group please

May 25 '06 #2
To be short No this is not possible

The only language i know of tat behaves the way you described is Javascript

by the way oftopic but maybe nice to know

you did
Dim test As Integer
test = test + 1
i would say

Dim test As Integer
test += 1

saves you some typing and behaves the same :-)

regards

Michel Posseth


"Justin" <ju****@hotmail.com> schreef in bericht
news:uP**************@TK2MSFTNGP03.phx.gbl...
Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example
Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable
test was never initialized. I want to prevent giving default value of 0.

Thanks

May 25 '06 #3
You might be able to set VS up to flag that as a warning in the IDE.
Check the compile properties for the project and see if it will let you
do this.

May 25 '06 #4
"Chris Dunaway" <du******@gmail.com> schrieb:
You might be able to set VS up to flag that as a warning in the IDE.
Check the compile properties for the project and see if it will let you
do this.

ACK, this should work (and is the default behavior) in VS 2005. However, I
have turned off the warning because I find the warning useless in 99 % of
its occurances. 'Dim x As Integer' is simply the same as 'Dim x As Integer
= Nothing', with 'Nothing' referring to the type's default value which is 0
in VB, and thus equivalent to 'Dim x As Integer = 0'. So why type 'Dim x As
Integer = 0' if 'Dim x As Integer' is already doing that?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

May 25 '06 #5

Herfried K. Wagner [MVP] wrote:
"Chris Dunaway" <du******@gmail.com> schrieb:
You might be able to set VS up to flag that as a warning in the IDE.
Check the compile properties for the project and see if it will let you
do this.

ACK, this should work (and is the default behavior) in VS 2005.


Not for value types. The compile setting that can be warned or errored
on is 'Use of variable prior to assignment' - however as the spec
extract I quoted earlier shows, value types *always* have a value, even
when they haven't been explicitly assigned one, and this value is
always 'safe'.

You can try it yourself:

(on My Project | Compile, set 'Use of variable prior to assignment' to
be a warning), then:

Dim x As Integer
Dim o As Object

Console.WriteLine(x.ToString)
Console.WriteLine(o.ToString)

Only one warning.

However, I
have turned off the warning because I find the warning useless in 99 % of
its occurances. 'Dim x As Integer' is simply the same as 'Dim x As Integer
= Nothing', with 'Nothing' referring to the type's default value which is 0
in VB, and thus equivalent to 'Dim x As Integer = 0'. So why type 'Dim x As
Integer = 0' if 'Dim x As Integer' is already doing that?


Since such a warning would be useless 100% of the time _for value
types_, the compiler doesn't issue one :)

--
Larry Lard
Replies to group please

May 26 '06 #6
Justin,
the statement "test = test + 1" should give me an error since variable
test was never initialized. I want to prevent giving default value of 0.
Did you test it?

In my idea it is initialized at the first use

test = test ' gives zero

Cor

"Justin" <ju****@hotmail.com> schreef in bericht
news:uP**************@TK2MSFTNGP03.phx.gbl... Is there any way to prevent VB.net to give default value to a variable
(changing settings what not...)?

For example
Dim test As Integer
test = test + 1

the statement "test = test + 1" should give me an error since variable
test was never initialized. I want to prevent giving default value of 0.

Thanks

May 26 '06 #7
"Larry Lard" <la*******@hotmail.com> schrieb:
> You might be able to set VS up to flag that as a warning in the IDE.
> Check the compile properties for the project and see if it will let you
> do this.


ACK, this should work (and is the default behavior) in VS 2005.


Not for value types. The compile setting that can be warned or errored
on is 'Use of variable prior to assignment' - however as the spec
extract I quoted earlier shows, value types *always* have a value, even
when they haven't been explicitly assigned one, and this value is
always 'safe'.


You are right. I have mixed it up with something else because it was the
first thing I have turned off.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

May 26 '06 #8

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

Similar topics

5
by: J C-W | last post by:
I have a directory with files (of various formats) contained within a website which uses PHP to control user access via session variables. I would like to protect the directory from direct external...
7
by: Robb Meade | last post by:
Hi all, A recent project that I had finished and went live with no apparant problems. My client received an email from a user who mentioned that by accident they had been typing (over the...
3
by: shortbackandsides.no | last post by:
I've been having trouble preventing users pressing Enter part way down a form so the incomplete form gets submitted. I came up with a possible solution - the code below seems to work in both...
0
by: richard | last post by:
Hi, I have a Schema for a Dataset which I am populating from Excel. One of the columns is a string datatype. When I Fill the Dataset I want any Null values to be set to a string value. I have...
10
by: et | last post by:
I have an asp.net program that uses a connection string, using integrated security to connect to a sql database. It runs fine on one server, but the other server gives me the error that "Login...
6
by: javelin | last post by:
In ASP classic pages, I want to know if it's possible to prevent session variables from becoming zero length strings? I have tried setting the Session.Timeout to a large value, but alwas, after 20...
1
by: Pavils Jurjans | last post by:
Hello, It is possible to set a high value in the web.config file, to prevent request timeout for a very complex pages that take long time to generate: <system.web> <httpRuntime...
2
by: Weeble | last post by:
Back in C++, I had a type that looked something like this (entering from memory so this might not quite compile): template <class T> class NotNull { private: T* value; public: NotNull(T*...
2
by: donpro | last post by:
Hi, I have the following function that check for the <Enterkey being pressed on an <Inputfield: function disableCR(e) { if (window.event) { key = window.event.keyCode; } else { key =...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.