Posted by: Sourav | September 5, 2016

Indexer Example with object initialization and linq in VB,VB Teacher Sourav,Kolkata 09748184075


Public Class employee

Private employeeid As Integer
Private name As String
Private gender As String
Public Property idmanipulate As Integer
Set(ByVal employeeid As Integer)
Me.employeeid = employeeid
End Set
Get
Return Me.employeeid

End Get
End Property

Public Property namemanipulate As String
Set(ByVal name As String)
Me.name = name
End Set
Get
Return Me.name

End Get
End Property

Public Property gendermanipulate As String
Set(ByVal gender As String)
Me.gender = gender
End Set
Get
Return Me.gender

End Get
End Property

End Class

Public Class company
Private Listemployees As List(Of employee)
Public Sub New()
Listemployees = New List(Of employee)
Listemployees.Add(New employee() With {.idmanipulate = 1, .namemanipulate = “Sourav”, .gendermanipulate = “Male”})
Listemployees.Add(New employee() With {.idmanipulate = 2, .namemanipulate = “Subir”, .gendermanipulate = “Male”})
Listemployees.Add(New employee() With {.idmanipulate = 3, .namemanipulate = “Sougata”, .gendermanipulate = “Male”})
Listemployees.Add(New employee() With {.idmanipulate = 4, .namemanipulate = “Subrata”, .gendermanipulate = “Male”})

End Sub

Default Public Property Item(ByVal employeeid As Integer) As String

Get
Return Convert.ToString((From emp In Listemployees
Where emp.idmanipulate = employeeid
Select emp.namemanipulate).ToList()(0))
End Get
Set(ByVal value As String)
Dim query = From l In Listemployees Where l.idmanipulate = employeeid Select l
query.Single().namemanipulate = value

End Set
End Property
Default Public Property Item(ByVal gender As String) As String

Get
Dim query = From l In Listemployees Where l.gendermanipulate = gender Select l

Return query.Count

End Get
Set(ByVal value As String)
Dim query = From l In Listemployees Where l.gendermanipulate = gender Select l
For Each l In query
l.gendermanipulate = value
Next l

End Set
End Property

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
Dim companyobj As New company
Response.Write(“Name of employee with an id of 2 ” + companyobj(2))
Response.Write(“<br />”)
companyobj(2) = “Suchanda”
Response.Write(“Name of employee with an id of 2 ” + companyobj(2))
Response.Write(“<br />”)
Response.Write(“Number of Male Employees is  ” + companyobj(“Male”))
Response.Write(“<br />”)
Response.Write(“Changing the sex of the employees  “)
companyobj(“Male”) = “Female”
Response.Write(“<br />”)
Response.Write(“Number of Male Employees is  ” + companyobj(“Male”))
Response.Write(“<br />”)
Response.Write(“Number of Female Employees is  ” + companyobj(“Female”))
End Sub

End Class

 

Reference

http://stackoverflow.com/questions/14629966/linq-vb-net-change-value

http://www.codeproject.com/Articles/26652/Using-LINQ-to-Objects-in-Visual-Basic

 

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: