Thursday, May 15, 2008

Project does not have a valid Sub Main

I encountered this error and it didn't make sense to me because I didn't have a Sub Main. Why vb.net gave that message is still a riddle to me. How did I resolve this? First you need to find out to what project the error pertains. Unfortunately double clicking the error does not tell you where the error resides. So select the messages tab and you will find in what project this error occurs.


Once you know what project this occurs, copy the code and paste it temporarily in a text document. Then remove that particular project. Then save all and close the solution. Then look up the physical location of the project that you just deleted and make sure that the directory of the deleted directory does not exist. Now restart your solution and add back your project again. And voila, no more errors.

Monday, May 12, 2008

Stack Frame

When you use many functions and procedures it can be handy to know where run time error occur.
The class StackFrame allows you to query the calling function.

Example:

Private Function SomeFunction() as Boolean

Try
_balance = -100
Catch ex as Exeption
Throw new Exception("Balance cannot be negative. Function: " & ((New StackFrame).GetMethod.Name), ex)
End Try
End Function


Monday, May 5, 2008

Add array to arraylist

I'd like to elaborate a bit more on arraylists. What if you utilize a class that returns an array. Maybe you made a call to a database and it returns you an array. But you want to use the nice stuff..eh. like in the arraylist..!

Well, we can still use the arraylist. In order to use the arraylist we have to add our array to an arraylist. We will use the addRange method. Here is how:

We will use our good old cars array.

dim cars(2) as string
dim listcars as new ArrayList

cars(0) = "ford"
cars(1)= "chevy"

listcars.AddRange(cars)

There you go, we added our cars array to our arrayList listcars. So now we are able to use all the nice methods that listcars exposes.

I don't know yet why Microsoft added the array and arraylist, why not use the arraylist?

happy programming

ArrayList vs Array

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)

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

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.