Home All Groups Group Topic Archive Search About

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

Author
27 Jan 2006 3:31 PM
**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

Author
27 Jan 2006 3:49 PM
Armin Zingler
Show quote Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
> 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
Author
27 Jan 2006 3:57 PM
I Don't Like Spam
Armin Zingler wrote:
Show quoteHide quote
> " **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
>> 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
>

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

Dim PF2 As Wnd.PARAFORMAT2 = nothing

But I could be wrong...
Chris
Author
27 Jan 2006 4:08 PM
**Developer**
I'll try it just to know

Show quoteHide quote
>>
>
> I believe you can get the compiler to shut up by doing:
>
> Dim PF2 As Wnd.PARAFORMAT2 = nothing
>
> But I could be wrong...
> Chris
Author
27 Jan 2006 4:09 PM
Armin Zingler
"I Don't Like Spam" <no@spam.com> schrieb
>
> 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
Author
27 Jan 2006 4:07 PM
**Developer**
Show quote Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:OUpSPl1IGHA.3452@TK2MSFTNGP12.phx.gbl...
>" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
>> 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))".
I like this best.


If I wanted to initialize, would I have to init each member to get rid of
the error?

Show quoteHide quote
>
>
>
> Armin
>
Author
27 Jan 2006 4:14 PM
Armin Zingler
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
>
> 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
Author
27 Jan 2006 4:31 PM
Cor Ligthert [MVP]
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