Minimalist fruit basket program in VB2005

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sat, 10 Nov 2007 17:27:30 -0500 (EST)

From the zip archive at
http://www.EmpowermentZone.com/vb_fruit.zip

This fruit basket program is written in Visual Basic 2005.  After my
attempt at optimizing the scripting capabilities of Boo, I became
interested in determining how much the VB compiler can infer types without
the "Option Strict" or "Option Explicit" directives.  I found that types
could be omitted for all but the event handling code:  they had to be
specified for the buttons and for the parameters of the routine that
processes click events.  Since these types could also be inferred from
context, the compiler is smart (more than the C# 2005 one), though could
be smarter.  From what I have read, Microsoft is enhancing the VB and C#
compilers with more intelligence and support for the Dynamic Language
Runtime that is also under development, as the .NET Framework and its
tools evolve.

Besides maximizing type inferences, this program tries to emulate other
stylistic aspects of dynamic languages, such as setting multiple
properties on a single line of code.  The code is thus my attempt at a
minimalist VB fruit basket, being as concise yet readable as possible.

The batch file, compile.bat, creates the executable, vb_fruit.exe, which
is about 9K in size.  The "NoWarnings" parameter is used so that only
compiler errors are shown, not warnings about types not fully specified.

Jamal

' content of vb_fruit.vb
' Fruit Basket program in Visual Basic 2005
' Public domain by Jamal Mazrui

' Import namespace
Imports System.Windows.Forms

Module FruitBasket
' Initialize dialog and controls
Dim dlg = new Form()
Dim lblFruit = new Label()
Dim txtFruit = new TextBox()
Dim WithEvents btnAdd as Button = new Button()
Dim lblBasket = new Label()
Dim lstBasket = new ListBox()
Dim WithEvents btnDelete as Button = new Button()

' Define entry point of program
Sub Main()
' Specify control properties
With lblFruit
.Text = "&Fruit:" : .Left = 14 : .Top = 14 : .Width = 44 : .Height = 16 :
.Parent = dlg
End With

With txtFruit
.Left = 64 : .Top = 14 : .Width = 200 : .Height = 16 : .Parent = dlg
End With

With btnAdd
.Text = "&Add" : .Left = 272 : .Top = 14 : .Width = 100 : .Height = 20 :
.Parent = dlg
End With

With lblBasket
.Text = "&Basket:" : .Left = 14 : .Top = 38 : .Width = 44 : .Height = 16 :
.Parent = dlg
End With

With lstBasket
.Left = 64 : .Top = 38 : .Width = 200 : .Height = 200 : .Parent = dlg
End With

With btnDelete
.Text = "&Delete" : .Left = 272 : .Top = 38 : .Width = 100 : .Height = 20
: .Parent = dlg
End With

With dlg
.Text = "Fruit Basket" : .StartPosition = FormStartPosition.CenterScreen :
.Width = 400 : .Height = 285 : .AcceptButton = btnAdd
.ShowDialog()
End With
End Sub

' Define event handler
Sub Button_Click(sender as Object, e as EventArgs) Handles btnAdd.Click,
btnDelete.Click
If sender is btnAdd Then
Dim sFruit = txtFruit.Text
if sFruit = "" Then
MessageBox.Show("No fruit to add!", "Alert")
Return
End If

lstBasket.Items.Add(sFruit)
txtFruit.Text = ""
lstBasket.SelectedIndex = lstBasket.Items.Count - 1

Else If sender is btnDelete Then
Dim iFruit = lstBasket.SelectedIndex
if iFruit = (-1) Then
MessageBox.Show("No fruit to delete.", "Alert")
Return
End If

lstBasket.Items.RemoveAt(iFruit)
if iFruit > (lstBasket.Items.Count - 1) Then iFruit =
(lstBasket.Items.Count - 1)
lstBasket.SelectedIndex = iFruit
End If
End Sub
End Module


__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: