473,396 Members | 1,852 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,396 software developers and data experts.

variable usage

Hello-

Let me start out by saying that I am new to .net and was only a hobby
vb6 programmer.

I am getting the warning:

"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

I also have the following in my code:

Structure Data
Dim Days As Single
Dim Events As Integer
Dim EventsPerWeek As Single
Dim TotalTime As TimeSpan
Dim TimePerWeek As TimeSpan
Dim TimePerEvent As TimeSpan
Dim TimeMin As TimeSpan
Dim TimeMax As TimeSpan
End Structure

Dim arrOutputData() As Data
Dim sinDays As Single

I don't understand why I get a warning saying that the variable is used
before it has been assigned a value when I am trying to assign it a
value at that point in the code.

Any help is appreciated. Thank you.

Jul 10 '06 #1
9 1750
Try to put this instead of the line "Dim arrOutputData() As Data":

Dim arrOutputData() As New Data

On Mon, 10 Jul 2006 18:54:20 +0100, krollenhagen
<ke***************@gmail.comwrote:
Hello-

Let me start out by saying that I am new to .net and was only a hobby
vb6 programmer.

I am getting the warning:

"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

I also have the following in my code:

Structure Data
Dim Days As Single
Dim Events As Integer
Dim EventsPerWeek As Single
Dim TotalTime As TimeSpan
Dim TimePerWeek As TimeSpan
Dim TimePerEvent As TimeSpan
Dim TimeMin As TimeSpan
Dim TimeMax As TimeSpan
End Structure

Dim arrOutputData() As Data
Dim sinDays As Single

I don't understand why I get a warning saying that the variable is used
before it has been assigned a value when I am trying to assign it a
value at that point in the code.

Any help is appreciated. Thank you.


--
Tiago Salgado
http://www.foruns.org
Jul 10 '06 #2
VB6 types and .Net structures are not the same; a .net structure is a
class (yes, you can add functions and properties), and therefore, you
have to use the new keyword to instantiate the class.

The way you did it, is comparable to the following vb6 code:

dim x as aClass
x.property = 5

The last line will give you a runtime error, because the class x hasn't
been instantiated.
In .Net, the compiler detects these errors, so you get them at compile
time instead of runtime.
krollenhagen wrote:
Hello-

Let me start out by saying that I am new to .net and was only a hobby
vb6 programmer.

I am getting the warning:

"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

I also have the following in my code:

Structure Data
Dim Days As Single
Dim Events As Integer
Dim EventsPerWeek As Single
Dim TotalTime As TimeSpan
Dim TimePerWeek As TimeSpan
Dim TimePerEvent As TimeSpan
Dim TimeMin As TimeSpan
Dim TimeMax As TimeSpan
End Structure

Dim arrOutputData() As Data
Dim sinDays As Single

I don't understand why I get a warning saying that the variable is used
before it has been assigned a value when I am trying to assign it a
value at that point in the code.

Any help is appreciated. Thank you.
Jul 10 '06 #3
Theo Verweij wrote:
a .net structure is a
class (yes, you can add functions and properties), and therefore, you
A .Net structure is *not* a class, even though you can functions and
properties.
have to use the new keyword to instantiate the class.
And you do *not* have to use new when instantiating them:

Public Structure MyStruct
Public AnInteger As Integer
Public AString As String
End Structure
'Note that New is not required
Dim structInstance As MyStruct

structInstance.AnInteger = 4
structInstance.AString = "Hello"

This is not to say that you cannot have a constructor for your
Structure.

Also, structures are stored differently in memory than classes. See
this article:

http://www.yoda.arachsys.com/csharp/memory.html

Chris

Jul 10 '06 #4
krollenhagen wrote:
"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"
But where did you assign a value to arrOutputData ? This looks like an
array, perhaps you forgot to initialize the array? Can you show the
code where you declare arrOutputData and how you initialize it?

Jul 10 '06 #5

krollenhagen wrote:
I am getting the warning:

"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

I also have the following in my code:

Structure Data
Dim Days As Single
Dim Events As Integer
Dim EventsPerWeek As Single
Dim TotalTime As TimeSpan
Dim TimePerWeek As TimeSpan
Dim TimePerEvent As TimeSpan
Dim TimeMin As TimeSpan
Dim TimeMax As TimeSpan
End Structure

Dim arrOutputData() As Data
Dim sinDays As Single
The Visual Studio IDE performs a data flow analisys in your code to
catch, among other things, when you access members of non-initialized
classes: accessing a member of a class that is still Nothing will
result in a run-time error, as you may know.

Now, arrays, in .Net, are classes -- you can, for instance, verify if
an array is Nothing (well, you can do the same with strings, also).
Therefore, you must instanciate the array before accessing an item of
it.

One way of doing this is like in VB6:

Dim arrOutputData(0 to 99) As Data

If you don't know how many items your array will have, you may declare
an empty array:

'Creates an empty array
Dim arrOutputData() As Data = {}

Or

'Creates an empty array also
Dim arrOutputData(-1) As Data

Afterwards you're free to Redim the array:

Dim Max As Integer = arrOutputData.GetLowerBound(0)
Redim Preserve arrOutputData(0 To Max + 1)

And *then* access items from the array =))

Regards,

Branco.

Jul 11 '06 #6
To prevent the nagging warning a lot of programmers have already set the
option off ( cause it warns sometimes without a good reasson )

sometimes the warning is even wrong when using a select case for instance
where you assign in every case a value
what will happen is that it will still display the nag warning that it
hasn`t been assigned a value or that a function doesn`t return a value on
all codepaths while it does

so to prevent this i learned myself to

1. assign a Nothing pointer after delcaration when such a situation occurs

2. use the return statement in functions where i use a slect case and as
last codeline i return a default value ( wich will never get executed )

regards

Michel Posseth [MCP]


"Chris Dunaway" <du******@gmail.comschreef in bericht
news:11*********************@75g2000cwc.googlegrou ps.com...
krollenhagen wrote:
>"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

But where did you assign a value to arrOutputData ? This looks like an
array, perhaps you forgot to initialize the array? Can you show the
code where you declare arrOutputData and how you initialize it?

Jul 11 '06 #7
I declared the array with the statement:

Dim arrOutputData() As Data

I don't know the length of the array when I am declaring it. Is that
why I have to initialize the array? If I were to redim the array,
would I have to initialize it?

Thanks for the help so far.

Keith

Chris Dunaway wrote:
krollenhagen wrote:
"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."

I get this warning in the line:

"arrOutputData(i).Days = sinDays"

But where did you assign a value to arrOutputData ? This looks like an
array, perhaps you forgot to initialize the array? Can you show the
code where you declare arrOutputData and how you initialize it?
Jul 11 '06 #8


you could go for a construct like this

Dim arrOutputData() As Data
Dim sindays As Single = 5 'just a dummy value
For i As Integer = 0 To 50
ReDim Preserve arrOutputData(i)
arrOutputData(i).Days = sindays
Next

regards

Michel Posseth [MCP]

"krollenhagen" wrote:
I declared the array with the statement:

Dim arrOutputData() As Data

I don't know the length of the array when I am declaring it. Is that
why I have to initialize the array? If I were to redim the array,
would I have to initialize it?

Thanks for the help so far.

Keith

Chris Dunaway wrote:
krollenhagen wrote:
"Variable 'arrOutputData' is used before it has been assigned a value.
A null reference exception could result at runtime."
>
I get this warning in the line:
>
"arrOutputData(i).Days = sinDays"
>
But where did you assign a value to arrOutputData ? This looks like an
array, perhaps you forgot to initialize the array? Can you show the
code where you declare arrOutputData and how you initialize it?

Jul 11 '06 #9
krollenhagen wrote:
I declared the array with the statement:

Dim arrOutputData() As Data

I don't know the length of the array when I am declaring it. Is that
why I have to initialize the array? If I were to redim the array,
would I have to initialize it?
<snip>

Redim and Redim Preserve both 'understand' non-initialized arrays,
therefore you may pass your aray to them and they'll do the right
thing, *unless*... remember that you can only access members of your
array after it's already initialized. So, a typical use of Redim
Preserve, like the following code, may fail if you forget the array
initialization:

'adding a new element to the array:
Redim Preserve arrOutputData(arrOutputData.GetLowerBound(0) + 1)

The code above will fail because GetLowerBound(0) wil be accessing
Nothing. If you use this pattern, the safest approach is to initialize
the array on the declaration, as suggested, or use something like the
following:

'adding a new element to the array:
If arrOutputData Is Nothing Then
Redim arrOutputData(0)
Else
Redim Preserve arrOutputData(arrOutputData.GetLowerBound(0) + 1)
End If

Personally, I prefer the initialization upon declaration, so I don't
need to test:

Dim arrOutputData(-1) As Data

Notice that in the current version of the framework you can use generic
lists, which are more versatile than arrays.

Dim OutputData As New List(Of Data)

Since using generics I don't remember ever declaring an array
anymore... =))

HTH

Regards,

Branco.

Jul 11 '06 #10

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

Similar topics

8
by: Jim Moon | last post by:
Hi. I'm curious about this syntax: <variable>=function(){...} I'm not finding a definition of this syntax, but I see it used at this website:...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
9
by: Newbie | last post by:
Hello, I need to declare a global variable e.g. database connection handle. So that I hava an access to this variable all over my class. How can I do it in c#? Do I have to declare it as static...
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
8
by: Tuxedo | last post by:
How can I modify any one of these global variable identified via a function argument? var x1 = "bla"; var x2 = "bla"; var x3 = "bla"; function modify(variable) { ???? ???? = "blabla":
1
by: tvance929 | last post by:
This should be simple, I just cannot find out the answers easily. At work I have 9 Aircards. I am responsible for checking them out and keeping track of them. I wanted to make a simple C#...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
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?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.