Posted by: Sourav | September 23, 2016

Using Dictionary in a webform,Vb Teacher Sourav,Kolkata 09748184075


Public Class Country
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Code() As String
Get
Return m_Code
End Get
Set(ByVal value As String)
m_Code = Value
End Set
End Property
Private m_Code As String
Public Property Capital() As String
Get
Return m_Capital
End Get
Set(ByVal value As String)
m_Capital = Value
End Set
End Property
Private m_Capital As String
End Class

Public Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsNothing(Session(“CountriesData”)) Then

Dim country1 As New Country() With { _
.Code = “AUS”, _
.Name = “AUSTRALIA”, _
.Capital = “Canberra” _
}

Dim country2 As New Country() With { _
.Code = “IND”, _
.Name = “INDIA “, _
.Capital = “New Delhi” _
}

Dim country3 As New Country() With { _
.Code = “USA”, _
.Name = “UNITED STATES”, _
.Capital = “Washington D.C.” _
}

Dim country4 As New Country() With { _
.Code = “GBR”, _
.Name = “UNITED KINGDOM”, _
.Capital = “London” _
}

Dim country5 As New Country() With { _
.Code = “CAN”, _
.Name = “CANADA”, _
.Capital = “Ottawa” _
}

‘Dim listcountries As List(Of Country) = New List(Of Country)
‘listcountries.Add(country1)
‘listcountries.Add(country2)
‘listcountries.Add(country3)
‘listcountries.Add(country4)
‘listcountries.Add(country5)
Dim dictionarycountries As Dictionary(Of String, Country) = New Dictionary(Of String, Country)
dictionarycountries.Add(country1.Code, country1)
dictionarycountries.Add(country2.Code, country2)
dictionarycountries.Add(country3.Code, country3)
dictionarycountries.Add(country4.Code, country4)
dictionarycountries.Add(country5.Code, country5)
Session(“CountriesData”) = dictionarycountries
End If
End Sub

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
Dim dictionarycountries As Dictionary(Of String, Country) = DirectCast(Session(“CountriesData”), Dictionary(Of String, Country))
Dim usrchoice As String = String.Empty
Dim rescountry As Country

‘Console.WriteLine(“Please enter country code”)
‘Dim strcountrycode As String = Console.ReadLine().ToUpper
‘If dictionarycountries.ContainsKey(strcountrycode) Then

rescountry = If(dictionarycountries.ContainsKey(TextBox1.Text.ToUpper), dictionarycountries(TextBox1.Text.ToUpper), Nothing)

‘Else
‘rescountry = Nothing
‘End If
If IsNothing(rescountry) Then
‘  Console.WriteLine(“Country Code not valid”)
TextBox2.Text = “”
TextBox3.Text = “”
Label1.Text = TextBox1.Text + ” is not a valid country code”
Else
‘Console.WriteLine(“The name of the country is {0} and the name of the capital is {1}”, rescountry.Name, rescountry.Capital)
TextBox2.Text = rescountry.Name
TextBox3.Text = rescountry.Capital
Label1.Text = “”

End If

End Sub
End Class

 

<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”WebForm1.aspx.vb” Inherits=”dictionaryoverlistwebvb.WebForm1″ %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>

Country Code:
Name:
Capital

</form>
</body>
</html>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: