Converted a solution to VS2005 and it is complaining: Variable 'PF2' is used before it has been assi

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

Public Shared Sub junk...

Dim PF2 As Wnd.PARAFORMAT2

PF2.cbSize = Marshal.SizeOf(PF2) 'ERROR HERE

PF2.dwMask = Wnd.PFM_LINESPACING

....snip

I just converted a solution to VS2005 and it is complaining:

Warning 22 Variable 'PF2' is used before it has been assigned a value. A
null reference exception could result at runtime. Make sure the structure or
all the reference members are initialized before use

How do I initialize PF2.

Each of its members?

What is the rational for requiring initialization - simply to document what
the developer wanted?



Thanks
 
**Developer** said:
Public Shared Sub junk...

Dim PF2 As Wnd.PARAFORMAT2

PF2.cbSize = Marshal.SizeOf(PF2) 'ERROR HERE

PF2.dwMask = Wnd.PFM_LINESPACING

...snip

I just converted a solution to VS2005 and it is complaining:

Warning 22 Variable 'PF2' is used before it has been assigned a
value. A null reference exception could result at runtime. Make sure
the structure or all the reference members are initialized before
use

How do I initialize PF2.

Each of its members?

What is the rational for requiring initialization - simply to
document what the developer wanted?


The compiler does not know which members of PF2 are accessed within
Marshal.SizeOf. Therefore there is a /potential/ risk that SizeOf accesses a
member that has not been initialized, thus you get the warning because it
would lead to a run time error (NullrefernceExeption). As you know this is
not true in this specific case, you can ignore the warning - it's only a
warning. The compiler only says "be aware, if you pass this object to a
function, the function might access it's members, thus have an eye on
initalizing all members before". You can change the behavior in the project
properties under the "compile" tab.


Alternatively, use "Marshal.SizeOf(Gettype(Wnd.paraformat2))".



Armin
 
Armin said:
The compiler does not know which members of PF2 are accessed within
Marshal.SizeOf. Therefore there is a /potential/ risk that SizeOf
accesses a
member that has not been initialized, thus you get the warning because it
would lead to a run time error (NullrefernceExeption). As you know this is
not true in this specific case, you can ignore the warning - it's only a
warning. The compiler only says "be aware, if you pass this object to a
function, the function might access it's members, thus have an eye on
initalizing all members before". You can change the behavior in the
project properties under the "compile" tab.


Alternatively, use "Marshal.SizeOf(Gettype(Wnd.paraformat2))".



Armin

I believe you can get the compiler to shut up by doing:

Dim PF2 As Wnd.PARAFORMAT2 = nothing

But I could be wrong...
Chris
 
Armin Zingler said:
The compiler does not know which members of PF2 are accessed within
Marshal.SizeOf. Therefore there is a /potential/ risk that SizeOf accesses
a
member that has not been initialized, thus you get the warning because it
would lead to a run time error (NullrefernceExeption). As you know this is
not true in this specific case, you can ignore the warning - it's only a
warning. The compiler only says "be aware, if you pass this object to a
function, the function might access it's members, thus have an eye on
initalizing all members before". You can change the behavior in the
project properties under the "compile" tab.


Alternatively, use "Marshal.SizeOf(Gettype(Wnd.paraformat2))".
I like this best.


If I wanted to initialize, would I have to init each member to get rid of
the error?
 
I Don't Like Spam said:
I believe you can get the compiler to shut up by doing:

Dim PF2 As Wnd.PARAFORMAT2 = nothing

But I could be wrong...

You're right. However, I prefer not to change my code just because of this
warning. I know that it does not make sense to set it to Nothing, thus I
better ignore the warning.

If I want to cross the street and somebody tells me I have to be careful
because I might be hit by a car, I do not avoid crossing the street. The
warning was ok, but I still decide whether to do it or not. In this case, I
know that it's safe to cross it, in other cases perhaps not. ;-)


Armin
 
**Developer** said:
If I wanted to initialize, would I have to init each member to get
rid of the error?

Why do you want to initialize? Using Marshal.Sizeof, you don't have to
initialize.

In other cases, you might have to initialize. Which members you need to
initialize depends on what you want to do. If you pass the object to a
function that needs some members, you have to initialize them before.
Depends on the function called.


Armin
 
Armin,
Why do you want to initialize? Using Marshal.Sizeof, you don't have to
initialize.
That is clear for me, if somebody has warned you not to cross the street,
than you can go to a marshall to ask if you are big enough to cross the
street.

I know you don't understand nothing at all from what I write here.

Before somebody misunderstand, there is nothing serious in my message.

However, I could not resist.

:-)

Cor
 
Back
Top