Showing posts with label orientated. Show all posts
Showing posts with label orientated. Show all posts

Thursday, March 29, 2012

Object Orientation Articles?

Can anyone suggest me some good articles on the object orientated programming on the internet? any links would do.
Thanks
Hi,
here you can find an introduction toOOP in VB.NET.
Grz, Kris.

object orientated dropdownlist

I'm trying to create a simple way to bind a dropdown, suppling a table name a field for a value and a field for the name i want bound. I have setup properties elsewhere in the class

1Public Class DataBinderClass2Protected WithEvents ddlAs System.Web.UI.WebControls.DropDownList3Private _tableAs String4 Private _fieldNameAs String5 Private _fieldValueAs String67 Public Sub DropDownBinder()8Dim objConnectionAs System.Data.OleDb.OleDbConnection9 objConnection =New OleDbConnection(ConfigurationManager.ConnectionStrings("DocumentConn").ConnectionString)10 objConnection.Open()11'So Now the Application has an open connection to the specific db12Dim strSQLAs String ="SELECT " & fieldValue &", " & fieldName() &" FROM " & table() &""13Dim objCmdAs New OleDbCommand(strSQL, objConnection)14'Create/Populate the DataReader15Dim objDRAs OleDbDataReader16 objDR = objCmd.ExecuteReader()17 ddl.DataTextField ="fld_username"18 ddl.DataValueField ="fld_id"19 ddl.DataSource = objDR20 ddl.DataBind()21 objDR.Close()22 objConnection.Close()23End Sub2425 Property table()As String' sets the table we are goin to get data from26Get27 Return _table28End Get29 Set(ByVal ValueAs String)30 _table = Value31End Set32 End Property3334 Property fieldName()As String' sets the field to retrieve from table35Get36 Return _fieldName37End Get38 Set(ByVal ValueAs String)39 _fieldName = Value40End Set41 End Property4243 Property fieldValue()As String' sets the field which will hold the value44Get45 Return _fieldValue46End Get47 Set(ByVal ValueAs String)48 _fieldValue = Value49End Set50 End Property51 End Class
Dim dbindAs New DataBinderClass dbind.table ="tbl_users" dbind.fieldName ="fld_username" dbind.fieldValue ="fld_id" dbind.DropDownBinder()

I have a dropdown declared like this

<form runat="server">
<asp:DropDownList ID="ddl" runat="server">
</asp:DropDownList></form>

However I get an object not initialised error at line 17 even though I have a

Protected WithEvents ddlAs System.Web.UI.WebControls.DropDownList at the top of my class
Anyone any ideas how to hook up the codebehind with my control? 

The second line of your code should be

Protected WithEvents ddlAs New System.Web.UI.WebControls.DropDownList
Thanks 


Pass the DDL reference into the sub

Public Sub DropDownBinder(ByRef yourDDL as DropDownList)

I'd also suggest passing in the object collection. Then you can reuse this easily. I've done similar and it's been a great timesaver.


Many thanks for the quick reply guys- super I'll give that a go. Curt the dropdownlist ID is a good idea, but as I was debugging the current error, I didn't want to confuse things too much!

It seems by your connection string call sytnax that you are using VS 2005. If so it is very much cleaner to use the object tags that accompany the dropdownlist when you drag it onto the page. With the FlyOut Tag you can quickly define a SQL statement to pull in your data and then choose the text and values shown in the dropdown.

But the old "alot of code way" :

1'Loads Interesction drop down box: LstTo23Sub GetXStreets(ByVal stcodeAs Long,ByRef oConnAs SqlConnection)45Dim sqlQueryAs String67Dim oreader2As SqlDataReader89Dim intersectionsAs ArrayList1011Dim sqlqueryIntClauseAs String1213Dim intersectionAs Long1415Dim sqlquerystClause1617Dim icAs Integer1819Dim cstreetAs String2021intersections =New ArrayList(100)2223sqlQuery ="SELECT Distinct Intersection# FROM Geo_St_Pairs WHERE (ArbStreetCode2 = " & stcode &") OR (ArbStreetCode1 = " & stcode &")"2425Dim ocmd2As New SqlCommand(sqlQuery, oConn)2627oreader2 = ocmd2.ExecuteReader()2829intersections.Clear()3031Do While oreader2.Read()3233intersections.Add(oreader2("Intersection#"))3435Loop36373839oreader2.Close()4041LstTo.Items.Clear()4243sqlQuery ="Select distinct StreetOrFeatureName from GEO_STREETS where arbStreetCode in ("4445sqlqueryIntClause ="Intersection# IN ("4647For Each intersectionIn intersections4849sqlqueryIntClause = sqlqueryIntClause & Str(intersection) &","5051Next5253sqlqueryIntClause = sqlqueryIntClause &"-1) "5455sqlQuery ="SELECT DISTINCT ArbStreetCode1 AS StreetCode FROM Geo_st_Pairs WHERE arbstreetcode1 <> " & Str(stcode)5657sqlQuery = sqlQuery &" AND " & sqlqueryIntClause5859sqlQuery = sqlQuery &" UNION SELECT DISTINCT ArbStreetCode2 AS streetcodex FROM Geo_st_pairs WHERE arbstreetcode2 <> " & Str(stcode)6061sqlQuery = sqlQuery &" AND " & sqlqueryIntClause6263' Label3.Text = sqlQuery6465ocmd2.CommandText = sqlQuery6667oreader2 = ocmd2.ExecuteReader()6869sqlquerystClause =" ArbStreetCode IN ("7071While oreader2.Read()7273sqlquerystClause = sqlquerystClause & oreader2("streetCode") &","7475End While7677sqlquerystClause = sqlquerystClause &"-1)"7879sqlQuery ="SELECT ArbStreetCode, StreetPrefixDirection, StreetOrFeatureName, StreetType " _8081&"FROM GEO_STREETS WHERE " & sqlquerystClause _8283&" ORDER BY StreetOrFeatureName"8485'oreader2 = ocmd2.ExecuteReader(CommandBehavior.CloseConnection)8687oreader2.Close()8889ocmd2.CommandText = sqlQuery9091oreader2 = ocmd2.ExecuteReader()9293ic = 09495Do While oreader2.Read()9697'Build up the street String!!!9899If Not IsDBNull(oreader2("StreetPrefixDirection"))Then100101cstreet = oreader2("StreetPrefixDirection") &" "102103Else104105cstreet =" "106107End If108109cstreet = cstreet & oreader2("StreetOrFeatureName")110111If Not IsDBNull(oreader2("StreetType"))Then112113cstreet = cstreet &" " & oreader2("StreetType")114115End If116117LstTo.Items.Add(cstreet)118119LstTo.Items(ic).Value = oreader2("arbStreetCode")120121ic = ic + 1122123Loop124125oreader2.Close()126127' Label3.Text = SqlQuery128129130131Dim stCode2As Long132133Dim stCode1As Long134135Try136137stCode1 = LstAlong.SelectedItem.Value138139stCode2 = LstTo.Items(0).Value'push the first item even though not selected140141Dim sb77As System.Text.StringBuilder =New System.Text.StringBuilder142143sb77.Append("<SCRIPT LANGUAGE=javascript>")144145sb77.Append("SelectBoth" & Chr(40) &"'supersegs'," & stCode1 & "," & stCode2 & Chr(41) & ";")146147sb77.Append("</SCRIPT>")148149'Inject the script onto the page--control where exactly the script enters the page150151'must be after the header--where javascript.js in included152153RegisterStartupScript("IntersectScript", sb77.ToString())154155Catch ex As Exception156157Response.Write("<script>alert(" & Chr(34) & "check spelling of street: use first letter and scroll!" & Chr(34) & ");</script>")158159160161End Try162163164165End Sub166

Lines 117-123 are the most imprtant--showing the loop and the dropdownlist adding the values via this loop based on the sql queries.

LstTo dropdown called like this in HTML code:
<asp:dropdownlist id="LstTo" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 144px" runat="server"Width="160px" Height="19px" AutoPostBack="True"></asp:dropdownlist><BR>

Rox


I'd also suggest passing in the object collection.

Can you explain what you mean by the "object collection"?


Sub mySub(byRef ddl as DropDownList, byRef myObjColl as Generic.ListOf(Object))

or if you want you could actually do the proper object but then you would need a sub for ever object collection type you have.

I take it a few steps further...here is what I use (note the sub is overridden to allow a selected value):

Public Overridable Sub FillDropDown(ByVal DropDownList As DropDownList, _
ByVal ObjectCollection As Object, _
ByVal ValueField As String, _
ByVal TextField As String, _
ByVal AddBlank As Boolean)

DropDownList.DataTextField = TextField
DropDownList.DataValueField = ValueField
DropDownList.DataSource = ObjectCollection
DropDownList.DataBind()
If (AddBlank) Then DropDownList.Items.Insert(0, "")
End Sub

Public Overridable Sub FillDropDown(ByVal DropDownList As DropDownList, _
ByVal ObjectCollection As Object, _
ByVal ValueField As String, _
ByVal TextField As String, _
ByVal AddBlank As Boolean, _
ByVal SelectedValue As String)

DropDownList.DataTextField = TextField
DropDownList.DataValueField = ValueField
DropDownList.DataSource = ObjectCollection
DropDownList.DataBind()
If (AddBlank) Then DropDownList.Items.Insert(0, "")
DropDownList.SelectedValue = SelectedValue
End Sub