|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
variable usageLet 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. 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 = <keith.rollenha***@gmail.com> wrote: Show quoteHide quote > 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 =3D 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 use= d > 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 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: Show quoteHide quote > 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. > Theo Verweij wrote:
> a .net structure is a A .Net structure is *not* a class, even though you can functions and> class (yes, you can add functions and properties), and therefore, you 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 krollenhagen wrote:
> "Variable 'arrOutputData' is used before it has been assigned a value. But where did you assign a value to arrOutputData ? This looks like an> A null reference exception could result at runtime." > > I get this warning in the line: > > "arrOutputData(i).Days = sinDays" > array, perhaps you forgot to initialize the array? Can you show the code where you declare arrOutputData and how you initialize it? 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] Show quoteHide quote "Chris Dunaway" <dunaw***@gmail.com> schreef in bericht news:1152560903.794268.16470@75g2000cwc.googlegroups.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? > 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: Show quoteHide quote > 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? 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] Show quoteHide quote "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? > > krollenhagen wrote:
> I declared the array with the statement: <snip>> > 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? 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. krollenhagen wrote:
Show quoteHide quote > I am getting the warning: The Visual Studio IDE performs a data flow analisys in your code to> > "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 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.
combobox gives unexpected cast error
Redrawing graphics in userControl - Button regular expression problem Get Icon from CallingAssembly (or ...) ListView iteration order Don't understand this warning message window application refresh every minute Scrolling columns in datagrid Splash Screens and Windows 2000 Re: Listview selected item |
|||||||||||||||||||||||