www.borneoexchanger.com

Thursday, September 4, 2008

VB.Net Simple Connection String

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources

Public Class MainClass
Shared Sub Main()
'Declare variables and objects
Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Employee.mdb;"
Dim objConnection As New OleDbConnection(strConnectionString)
Dim strSQL As String = _
"SELECT FirstName, LastName FROM Employee"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("Employee")
Dim objDataRow As DataRow

Try
'Open the database connection
objConnection.Open()

'Fill the DataTable object
objDataAdapter.Fill(objDataTable)

'Load the list box on the form
For Each objDataRow In objDataTable.Rows
Console.WriteLine(objDataRow.Item("FirstName") & " " & _
objDataRow.Item("LastName"))
Next
Catch OleDbExceptionErr As OleDbException
'Write the exception
Console.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
'Write the exception
Console.WriteLine(InvalidOperationExceptionErr.Message)
End Try

'Close the database connection
objConnection.Close()

'Clean up
objDataRow = Nothing
objDataTable.Dispose()
objDataTable = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing

End Sub
End Class

Initializing a jagged array, one in which the length of each array differs

Imports System
Imports System.Data
Imports System.Collections

public class MainClass
Shared Sub Main()
Dim c()() As Integer = {New Integer() {1, 2}, New Integer() {1, 2, 3}}

c(1) = New Integer() {4, 5}
Console.WriteLine(c(1)(1))
c(1) = System.Array.CreateInstance(GetType(System.Int32), 10)
Console.WriteLine(c(1)(1))
Dim d() As Integer = {6, 7}
c(1) = d
Console.WriteLine(c(1)(1))
End Sub
End Class

MyClass - Implementing Polymorphism in VB.Net

MyClass - Implementing Polymorphism in VB.Net



Learn how to take advantage of polymorphism in VB.Net with William's quick tutorial. Learn how polymorphism can eleviate your OOP-related headaches!

This article explains and extends the know-how of implementing polymorphism in VB.Net. Of course, polymorphism is a huge topic and I will only focus on the little known VB.Net keyword MyClass.

There was a recent tip published titled, "Polymorphism Can Lead to Head Scratching in VB.Net". It can be found here.

I would like to stress that VB.Net is a full compliant object-oriented language and thus, OOP should be practiced at all times within .NET Development, albeit with careful planning and design so as not to lose focus.

In the aforementioned article, the author stresses that polymorphism can lead to nightmares and headaches. I would like to stress that not implementing polymorphism can lead to much worse nightmares and headaches in terms of code maintenance and extensibility.

The above problem, as stated by the author, can essentially be solved with the VB.Net keyword: MyClass.

I will replicate the exact same code, as first published in the original tip, and I will place the keyword in the appropriate place.

Lets see some code below:

Class Parent
Public Overridable Sub p1()
Console.Write("Parent.p1")
End Sub
Public Sub p2()
MyClass.p1()
'Implementing keyword MyClass here tells all derived classes to refer to the base / abstract class wherein the keyword MyClass appears
End Sub
End Class

Class Child
Inherits Parent
Public Overrides Sub p1()
Console.Write("Child.p1")
MyBase.p2()
End Sub
End Class

Sub Main()
Dim p As Parent
Dim c As Child
p = New Parent()
p.p1() 'OK
p.p2() 'OK
p = New Child()
p.p1() 'stack overflow error is prevented

If MyClass is not implemented above in the base class, you will get a stack overflow error. So why does the stack overflow occur?

  • Parent.p2() calls Parent.p1()
  • Parent.p1() is polymorphic because it can be overridden
  • Child.p1() overrides Parent.p1() so any calls to Parent.p1() will actually call Child.p1() if you have an instance of class Child
  • Child.p1() calls MyBase.p2()
  • MyBase.p2() is actually Parent.p2()

Now if you are still with me, the pitfall comes when you have an instance of class Child and call procedures p1 and p2. Calling p1 produces the following execution flow:

Child.p1 -> Parent.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> etc.

And the cycle repeats until we have no stack space left. Calling p2 produces the following execution flow:

Child.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> Parent.p2 -> Child.p1 -> etc.

Implementing the keyword MyClass in the base class tells all derived classes to use the method in the base class itself and, therefore, a stack overflow error is prevented.

p.p2() 'stack overflow error is prevented
c = New Child()
c.p1() 'stack overflow error is prevented
c.p2() 'stack overflow error is prevented
End Sub

I hope this will help VB.Net developers along the way to develop software the object-oriented way.

Google Chrome: A New Rival For Browser Vendors

Google Chrome: A New Rival For Browser Vendors


It's like that there's nothing on this world that can't be made in Google. They have conquered the search engine market, and then they travel throughout other market as well, such as Social network, Image management tools, Local search engine, Earth project, Map project, and many other things on their labs waiting to be published.

Today, they announced another product, which called Google Chrome. It's a web browser (YES, it's yet another WEB BROWSER). Like all Google's product, they tried to make their product as simple as possible, with clean interface. But that's only in the front end. Deep inside the code, they have put Java Script engine called V8 which should give better performance compared to other products available. They are focusing on the JS engine since probably most of their products uses JS. They combined WebKit and Mozilla's Firefox to produce a better product and even better, they make the source code available to public.

Right now, they don't have a Linux version yet, but they are working on it. Mac version is also on their TODO list big grin. The Windows version will be published in 24 hours, so stay tuned

about Me

I 'am from Indonesia.
My Name is Aril

" If we are to achieve results never before accomplished, we must expect to employ methods never before attempted."