While I was working with arrays I, in particular the array, I noticed something annoying. The annoying thing is that the array class in vb.net does not expose a delete or remove method. So, in order to remove an element you would have to use a For..Next loop to find the item, set it to Nothing and resize the array. vb.net being a powerful programming language should have a better and shorter way to do this. Well guess what?... There is!.
What would we use then?:
ArrayList
ArrayList exposes the remove method and will resize the array for you after you remove an item.
How does that work ?
Let's use our cars array again, but now, make it an arraylist.
(you need to use new so the cars will be an object of arraylist)
Dim cars As New ArrayList(3)
Now let's add some cars:
cars.Add("ford")
cars.Add("chevy")
cars.Add("dodge")
We don't even have to tell the index, now isn't that handy?!
Ok, now we want to remove the "chevy" from our cars arraylist.
We simply do this:
cars.Remove("chevy")
You can check the length of the cars arraylist using the count property. (cars.count) and you will see that it now contains 2 elements.
Hope you learned something new ;o)
Monday, May 5, 2008
binary search in vb.net
vb.net allows you to use arrays (zero based) to hold data.
One feature that is a very handy and fast method of the array class is : binarysearch.
It returns the position of an element within an array (one dimensional).
This is how it works:
Say for instance you declare an array with 3 items:
dim cars() as string = ("ford", "chevy", "dodge")
Now say that for some reason you want to find the position of the element "chevy" out of the cars() array.
vb.net has a very handy method for that: binarysearch
Here is how it works:
dim findindex as integer = array.binarysearch(cars,"chevy")
findindex would return 1. If you were to search "ford" it would return 0, hence vb.net arrays are zero-based.
dim findindex as integer = array.binarysearch(cars,"toyota") would return -1 (not found)
Well, that's it for now.
c ya next time
One feature that is a very handy and fast method of the array class is : binarysearch.
It returns the position of an element within an array (one dimensional).
This is how it works:
Say for instance you declare an array with 3 items:
dim cars() as string = ("ford", "chevy", "dodge")
Now say that for some reason you want to find the position of the element "chevy" out of the cars() array.
vb.net has a very handy method for that: binarysearch
Here is how it works:
dim findindex as integer = array.binarysearch(cars,"chevy")
findindex would return 1. If you were to search "ford" it would return 0, hence vb.net arrays are zero-based.
dim findindex as integer = array.binarysearch(cars,"toyota") would return -1 (not found)
Well, that's it for now.
c ya next time
vb.net Intro
hey all,
I thought I would just start a blog to record the intricacies that I encounter coding in vb.net.
I am rather new in the vb.net area but I hope to become more proficient while using this language.
I always have dreaded writing, but I think that if you are able to write down your thoughts, you probably know what you are talking about.
I am currently a vb.net programmer at a mid size company in Ohio. I am currently working on a project that utilizes vb.net to access a web service that is being exposed by one of our clients.
I divided the solution up in 5 projects, just to establish a good organization of the code. It's an application with a simple front end that's access a database layer and a business layer. I created a separate error class to collect non-fatal errors, e.g errors that are non runtime errors that would halt the application. Anyhow, that's my introduction. I hope to share a lot of vb.net stuff.
I thought I would just start a blog to record the intricacies that I encounter coding in vb.net.
I am rather new in the vb.net area but I hope to become more proficient while using this language.
I always have dreaded writing, but I think that if you are able to write down your thoughts, you probably know what you are talking about.
I am currently a vb.net programmer at a mid size company in Ohio. I am currently working on a project that utilizes vb.net to access a web service that is being exposed by one of our clients.
I divided the solution up in 5 projects, just to establish a good organization of the code. It's an application with a simple front end that's access a database layer and a business layer. I created a separate error class to collect non-fatal errors, e.g errors that are non runtime errors that would halt the application. Anyhow, that's my introduction. I hope to share a lot of vb.net stuff.
Subscribe to:
Posts (Atom)