|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Converted a solution to VS2005 and it is complaining: Variable 'PF2' is used before it has been assiDim 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
Show quote
Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb The compiler does not know which members of PF2 are accessed within> 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? 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 Zingler wrote:
Show quoteHide quote > " **Developer**" <REMOVEdevelo***@a-znet.com> schrieb I believe you can get the compiler to shut up by doing:>> 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 > Dim PF2 As Wnd.PARAFORMAT2 = nothing But I could be wrong... Chris 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 "I Don't Like Spam" <no@spam.com> schrieb You're right. However, I prefer not to change my code just because of this> > I believe you can get the compiler to shut up by doing: > > Dim PF2 As Wnd.PARAFORMAT2 = nothing > > But I could be wrong... 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
Show quote
Hide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message I like this best.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))". If I wanted to initialize, would I have to init each member to get rid of the error? Show quoteHide quote > > > > Armin > " **Developer**" <REMOVEdevelo***@a-znet.com> schrieb Why do you want to initialize? Using Marshal.Sizeof, you don't have to > > If I wanted to initialize, would I have to init each member to get > rid of the error? 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,
> That is clear for me, if somebody has warned you not to cross the street, > Why do you want to initialize? Using Marshal.Sizeof, you don't have to > initialize. > 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
READING EMAILS
Problem with installer made in VS 2003.NET write Override methods in Code Editor VS2005 Can we run Vb.Net Application on Apple Macintosh ? StreamReader does not read all characters... Force ASP.NET shadow directory to update? Null value with memo field, error Icons on the menus VB.Net MainMenu Control Suggestion for DB Connection |
|||||||||||||||||||||||