I altered it to use a database with two fields: State Full Name and State Abbreviation. It could be used for any key value hash that you might like to cache from a database for a web based application.
Code:
Imports System
Imports System.Web.Caching
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
' Borrowed from 4guysfromrolla.com
' modified to use A Database instead of XML file
Public Class DBState
Private m_name As String
Private m_abbreviation As String
Public Sub New(ByRef nameArg As String, ByRef abbreviationArg As String)
m_name = nameArg
m_abbreviation = abbreviationArg
End Sub
Public ReadOnly Property Name() As String
Get
Return m_name
End Get
End Property
Public ReadOnly Property Abbreviation() As String
Get
Return m_abbreviation
End Get
End Property
Public ReadOnly Property FullAndAbbrev() As String
Get
Return m_name & " (" & m_abbreviation & ")"
End Get
End Property
End Class
''' <summary>
''' Provides the functionality related to retrieving the list
''' of states for a system; this is meant for US states,
''' territories, and Canadian provinces. It can also be used
''' for other countries that have states or analogous areas. It
''' uses the States.config file as its data source.
''' </summary>
Public Module DBStateManager
' Cache object that will be used to store and retrieve items from
' the cache and constants used within this object
Private myCache As Cache = System.Web.HttpRuntime.Cache()
Private stateKey As String = "StateKey"
'Public applicationConstantsFileName As String = Replace(System.AppDomain.CurrentDomain.BaseDirectory & "States.config", "/", "\")
Private stateArray As DBState()
Private errorList As ArrayList
' Tells you whether or not any errors have occurred w/in the module
Public ReadOnly Property hasErrors() As Boolean
Get
If errorList Is Nothing OrElse errorList.Count = 0 Then
Return False
Else
Return True
End If
End Get
End Property
' Retrieves an array list of Exception objects
Public ReadOnly Property getErrors() As ArrayList
Get
Return errorList
End Get
End Property
' Private method used to add errors to the errorList
Private Sub addError(ByRef e As Exception)
If errorList Is Nothing Then errorList = New ArrayList
errorList.Add(e)
End Sub
''' <summary>
''' Gets all the states
''' </summary>
''' <returns>An array of State objects</returns>
Public Function getStates() As DBState()
If myCache(stateKey) Is Nothing Then
PopulateCache()
End If
Return stateArray
End Function
''' <summary>
''' Gets the state object based on the abbreviation
''' </summary>
''' <param name="abbreviation">The abbreviation of the state
''' to retrieve</param>
''' <returns>A State object for the abbreviation
''' given</returns>
Public Function getStateByAbbreviation(ByRef abbreviation As String) As DBState
If myCache(stateKey & abbreviation) Is Nothing Then PopulateCache()
Return myCache(stateKey & abbreviation)
End Function
Private Sub PopulateCache()
'Dim DB InfoFile As New DB InfoDocument
'Dim theState As State
'Dim theNode As DB InfoNode
Dim mySQL As String
Dim theName, theAbbreviation As String
Dim i As Integer = 0
Dim conStr As String = ConfigurationSettings.AppSettings("PdmlDBConStr")
Dim con As New OleDbConnection(conStr)
Dim dr As OleDbDataReader
mySQL = "SELECT [STATE Abbreviation],[State Full Name] from DBState"
Try
con.Open()
Dim comm As New OleDbCommand(mySQL, con)
dr = comm.ExecuteReader()
' TODO: %%% Magic 100 states in array!
stateArray = Array.CreateInstance(GetType(DBState), 100)
i = 0
While dr.Read
theAbbreviation = Trim(dr.GetString(0))
theName = Trim(dr.GetString(1))
'Populate that location in the array with the
' values for the tag
stateArray(i) = New DBState(theName, theAbbreviation)
'Insert the state into cache
myCache.Insert(stateKey & theAbbreviation, stateArray(i))
i = i + 1
End While
myCache.Insert(stateKey, stateArray)
dr.Close()
con.Close()
Catch e As Exception
addError(e)
End Try
End Sub
End Module
Then use this code to call it.
Code:
Dim TheStateObject As DBState
Dim TheStateLongName As String
In Visual Studio GDI+, for drawing any item we need to pick up a color for either creating a brush or a pen. .NET framework has provided System.Drawing.Color structure which contains 141 colors.
The .NET framework procides a very powerful feature called Reflection. All the colors in System.Drawing.Color structure are declared as public static properties.
I had a need to create a set of buttons to match items in an arraylist.
Add a placeholder control to your ASP.NET web page named placeholder1
Now put this code in the codebehind page. Add a call to the LoadButtonsToPlaceHolder() subroutine in the page load control. Make sure it also appears on postback (do not put in the IsPostback check.)
Code:
Private Sub LoadButtonsToPlaceHolder()
Dim GroupArray As ArrayList
Dim ButtonNumber As Int16 = 1
Dim AButton As System.Web.UI.WebControls.Button
GroupArray.Add("Press Button For Test1")
GroupArray.Add("Press Button For Test2")
GroupArray.Add("Press Button For Test3")
For Each GroupName As String In GroupArray
AButton = New Button
AButton.Text = "**"
AButton.ID = Trim(Str(ButtonNumber))
AddHandler AButton.Click, AddressOf ButtonClickHandler
AddHandler AButton.Command, AddressOf ButtonCommandHandler
PlaceHolder1.Controls.Add(AButton)
PlaceHolder1.Controls.Add(New LiteralControl(GroupName & "<BR>"))
ButtonNumber = ButtonNumber + 1
Next
End Sub
Add the code to handle the button click. One handler covers any click and makes the decision based upon the button ID.
Code:
' Handle any button click, look up by ID
Private Sub ButtonClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim WhichButton As System.Web.UI.WebControls.Button
Dim WhichTest As Int16
WhichButton = CType(sender, System.Web.UI.WebControls.Button)
WhichTest = Val(WhichButton.ID)
End Sub
' Note: For some reason, if you do not assign a buttoncommandhandler with a buttonhandler on a dynamic set of buttons
' then it will not work....
Private Sub ButtonCommandHandler(ByVal sender As System.Object, ByVal e As System.web.ui.WebControls.CommandEventArgs)
' Do Nothing
End Sub
This will build the dynamic buttons and will be handled by the buttonhandler.
Ten tools to download and Ten add-ins for VSTUDIO developers
NUnit to write unit tests
NDoc to create code documentation
NAnt to build your solutions
CodeSmith to generate code
FxCop to police your code
Snippet Compiler to compile small bits of code
Two different switcher tools, the ASP.NET Version Switcher and the Visual Studio .NET Project Converter
Regulator to build regular expressions
.NET Reflector to examine assemblies
TestDriven.NET to write unit tests
GhostDoc to create XML comments
Smart Paster to paste clipboard text as comments or strings
CodeKeep to reuse snippets of code
PInvoke.NET PInvoke.NET is a wiki that can be used to document the correct P/Invoke signatures
VSWindowManager PowerToy to save custom Visual studio IDE layouts
WSContractFirst create web service schema files
VSMouseBindings - assign each of your mouse buttons to a Visual Studio command.
CopySourceAsHTML - Code is exponentially more readable when certain parts of that code are differentiated from the rest by using a different color text
Cache Visualizer
IsNull in VB 6 to comparing Null/Nothing in VB.NET
Posted: Wed May 10, 2006 12:36 pm
BB
Regular
Joined: Jun 23, 2004
Posts: 340
COmparing null objects in Visual Basic
In vbscript/vb6 the syntax is:
Code:
If Not (obj Is Nothing) Then
end if
In VB.NET 8.0 (VB.NET 2005), it's:
Code:
If obj IsNot Nothing Then
end if
When using the Visual Studio 2005 to upgrade a Visual Basic 6.0 project, you may encounter this warning.
Code:
UPGRADE_WARNING: Use of Null/IsNull() detected.
It will erroneously change the IsNull(variable) function to variable = System.DBNull.Value. This would only apply when comparing a database value. Use one of the above methods to check null strings and objects in VB.NET.
There is no VB.NET equivalent of the null keyword. And the IsNull function is gone. Instead, VB.NET uses the Nothing keyword.
Code:
Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If
There is a ReadOnly member of the String Class called Empty.
Code:
Dim s As String
' Some other code
If s = String.Empty Then
' Do something
end if
There is the IsNothing function available in the Microsoft.VisualBasic namespace. And, there is nothing wrong with using this namespace in VB code. It can be used in C# as well. It is part of the .NET framework
Code:
If Not IsNothing(d) Then
' Do something
end if
IsNullOrEmpty, new in Visual Basic 2005 for strings, combines both of the above tests and combines them into one new method in the String class - IsNullOrEmpty. It is a Shared function that returns a Boolean value, so the syntax is:
Code:
'(where s is a String variable)
If String.IsNullOrEmpty(s) Then
' Do Something
end if
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest (c) 2006 by Group29 Productions.
You can syndicate Group29 Productions news with an RSS Feeder using the file backend.php