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

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

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



Jan 27 '06 #1
7 2701
" **Developer**" <RE*************@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

Jan 27 '06 #2
Armin Zingler wrote:
" **Developer**" <RE*************@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
Jan 27 '06 #3

"Armin Zingler" <az*******@freenet.de> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
" **Developer**" <RE*************@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?

Armin

Jan 27 '06 #4
I'll try it just to know


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

Dim PF2 As Wnd.PARAFORMAT2 = nothing

But I could be wrong...
Chris

Jan 27 '06 #5
"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

Jan 27 '06 #6
" **Developer**" <RE*************@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

Jan 27 '06 #7
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
Jan 27 '06 #8

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

Similar topics

6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
6
by: Rob Meade | last post by:
Hi all, Ok - I've used the word gramatically - which I'm sure is incorect, however I mean when VS2005 lists "warnings" like this one: Warning 1 Variable 'Command' is used before it has been...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: GaryDean | last post by:
(my original post was inaccurate but this post accurately describes what I think is a very bad vs2005 bug) short description... Deleting a dataset and recreating it from the dataadapter causes...
10
by: dbuchanan | last post by:
Hello, >From time to time my vb2005 form disappears and is replaced by the following errors. Rebuilding the application never helps. However the errors never affects the operation of my...
5
by: cmay | last post by:
I have a big solution filled with a bunch of projects. I add a project to the solution (not adding any references mind you). Now, if I close down everything and try to just open that single...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
2
by: Chris Paxton | last post by:
I have a fairly basic VB.NET application that I converted from VS2003 to VS2005 and now it crashed the VS2005 IDE pretty regularly while trying to use the form designer. I've removed any 3rd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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,...
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.