Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2004, 10:41 AM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default declare empty variable

How do I declare a variable but leave it empty so I can check if it has been assigned a value later?

I have Dim intMyVar As Integer = nothing,

and then later I check it: If intMyVar = nothing Then

but it gives me the compilation error that Name intMyVar has not been declared.

In my code this variable may or may not be assigned a value, but I need it to exist and be able to check if it is empty or not.

Can someone show me the way?

Thanks,

John
 
Old April 8th, 2004, 10:57 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Could you set it to "", then check to see if it is "" or something else?

Snib

P2P Member
<><
 
Old April 8th, 2004, 11:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi John,

An Integer is a value type and will always be initialized to a default value of 0. So, even if you don't assign a value explicitly, the value is 0.

If 0 has no special meaning in your app, you can compare against that value. Often, -1 is used for this scenario as well.

Alternatively, you can assign Integer.MinValue or Integer.MaxValue to your int. If later in your app the Int still has that value, it hasn't been changed:
Code:
Dim i As Integer = Integer.MinValue
...
If i = Integer.MinValue Then
  ' Not changed
Else
  ' i has changed
End If
HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Microphone Friend by Rage Against The Machine (Track 1 from the album: Renegades)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Must declare the scalar variable stapes ASP.NET 2.0 Basics 1 August 15th, 2008 10:03 AM
Must declare the scalar variable kofibull Visual Studio 2005 0 November 24th, 2006 05:47 PM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 10:56 AM
How to declare global variable gaurav_jain2403 General .NET 3 February 21st, 2006 01:31 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.