Array Null Reference

tamandt

Member
Joined
Apr 1, 2006
Messages
13
Programming Experience
3-5
I'm new to .net, having trouble with arrays.
I have declared an array

Here are the array declarations.

VB.NET:
Dim MasterList() As String
Dim MasterPath() As String

Here is the code

VB.NET:
Case "dll"
DllCount += 1
MasterPath.SetValue("C:\Windows\System32\", MasterCount)
MasterList.SetValue(System32List(I), MasterCount)
MasterCount += 1
Case "exe"
ExeCount += 1
MasterPath(MasterCount) = "C:\Windows\System32\"
MasterList(MasterCount) = System32List(I)
MasterCount += 1

As you can see ive tried simple "=" as well as setvalue.

Both give me a Null Reference. So how do I initialize this array?


Your help is appreciated.
 
Last edited:
No Bound Array... Is it possible

Ive been looking around and it looks like every bit of code has an array with bounds. If not set right away, set later on before use. When I do this my code works.

but.........


My array stores file names ranging from 5000 files to 20000 files....
Isnt it unefficient to just declare an array of 20000 strings if you only need 5000?
 
You could for instance start by declaring what you consider minimum 5000 elements, then resize up 1000 when array is filled, finally downsize to exact when finished, this is the same way the dynamic ArrayList works to save memory and processing time. An array is always fixed size and isn't actually resizable, when Redim'ed a new array is setup internally (and elements copied if you also specify Preserve).
 
ArrayList better option

Ok, i did read that arrays have been depreciated and should no longer be used, but i didnt really believe it.

the .add method will add an item to the arraylist

but i dont see how to read from it.

perhaps a sample of how to read items 0 through 3

Thanks.
 
tamandt said:
Ok, i did read that arrays have been depreciated and should no longer be used, but i didnt really believe it.
Oh really? Where did you read that?
ArrayList isn't much different that using a regular array. Collections are usually accessed by Item property Arraylist.Item(83094), but since Item is the Default property you can skip the property identifier just like using array Arraylist(83094)
http://msdn2.microsoft.com/en-US/library/system.collections.arraylist.item(VS.80).aspx
 
Thanks

Thanks thats simple, i think ill switch over to arraylist.

As far as where did i read about arrays being depreciated? Well I really dont feel like looking through my history, but i it was on a site for newbies about .net. Since im new to .net, I went to many of them.

They didnt exact say they were not used. They said Arrays are no longer an important part of .net and should be avoided whenever possible.

Regardless, thank you all for you help.
 
Arrays are still an importanty part of .NET programming an always will be. Collections are convenient but for situations where you don't need the ability to resize arrays are more efficient. Whoever said that arrays should be avoided whenever possible is an idiot, quite frankly. Having said that, if you are going to use a collection for strings specifically then you should use a Specialized.StringCollection instead of an ArrayList because the items in a StringCollection are strongly typed to String.
 
Specialized

It seems to me the MS has created a lot of specialized objects since vb6. Is it really more efficient to use these objects instead of doing things the old way. After all arent they just putting the old way into new containers? making things slower? Or am i wrong.

.net is definetly object oriented

Thanks.
 
Many new .NET classes are much more convenient to work with. They require less code to get more done in many situations. Collections are convenient in mnay situations. Whether a collection or array is more efficient depends on the circumstances. Collections are more efficient if resizing occurs regularly because when a collection grows the existing items do not need to be moved. There is no such thing as resizing an array. It is an illusion covering the fact that a new array is created and the existing elements copied. That does not happen with a collection. Sure things get slower the more layers you add but if speed is so critical then you shouldn't be using VB.NET in the first place. You should write the most efficient code that you realistically can, but if you try to eek out every little bit of performance then you won't be very productive. Follow good programming practices and make sensible decisions and your app will perfrom efficiently. Modern GUI apps can afford a few nanoseconds here and there to make coding easier.
 
Thanks

Oh yes I do understand that. I dont need a fast app for what im writing now. If i wanted speed I would use C++ or ASM. I was just curious if you thought those extra layers did slow things down.

thanks for the info.
 
Back
Top