Pet peeves when coders ask questions
Something seen repeatedly is code that does not make sense while this is one variation.
For instance the following line of code (where BS is a BindingSource) where the developer knows the type. What would be appropriate rather than TryCast is CType or DirectCast with a meaningful variable name. The person wants to update the current record and is not updating.
Dim r = TryCast(BS.Current, DataRowView)
When uses the following to set a value
r("Area") = calculatedresult
They had the following for updating on property changed
dataTable.Columns.Add(New DataColumn() With { .ColumnName = "Area", .DataType = GetType(Decimal) })
Why isn't the value being updated? Because they went against the DataRowView rather than the DataRow -> column. I recommended using the following.
Public Class CodeSampleForm
Private ReadOnly _bindingSource As New BindingSource()
Private Sub CalculateButton_Click(sender As Object, e As EventArgs) _
Handles CalculateButton.Click
Dim currentRow = CType(_bindingSource.Current, DataRowView).Row
Dim area = currentRow.Field(Of Decimal)("Area")
currentRow.SetField("Area", area + 1)
End Sub
Private Sub CodeSampleForm_Shown(sender As Object, e As EventArgs) _
Handles Me.Shown
Dim dataTable As New DataTable()
dataTable.Columns.Add(New DataColumn() With {
.ColumnName = "Area",
.DataType = GetType(Decimal)
})
dataTable.Rows.Add(1.4D)
dataTable.Rows.Add(2.4D)
_bindingSource.DataSource = dataTable
BindingNavigator1.BindingSource = _bindingSource
Textbox_Area.DataBindings.Add("Text", _bindingSource, "Area",
True,
DataSourceUpdateMode.OnPropertyChanged)
End Sub
End Class
Imports System.ComponentModel
Public Class RemoveAndBind(Of T)
Inherits BindingList(Of T)
Protected Overrides Sub RemoveItem(ByVal index As Integer)
RaiseEvent FireBeforeRemove(Me,
New ListChangedEventArgs(ListChangedType.ItemDeleted, index))
MyBase.RemoveItem(index)
End Sub
Public Event FireBeforeRemove As EventHandler(Of ListChangedEventArgs)
End Class
I was asked to explain the code as in the example above and they had a full working example.
Imports System.ComponentModel
Imports BindingListListChangedLibrary
Public Class Form1
WithEvents personBindList As RemoveAndBind(Of Person)
Private ReadOnly operations As New Operations
Private currentlyLoading As Boolean = True
''' <summary>
''' Focus on ListBox1 and BindingList personBindList.New List
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'
' Populate a few people
'
Dim peopleList = New List(Of Person) From {
New Person() With {.FirstName = "Karen", .LastName = "Payne"},
New Person() With {.FirstName = "Bill", .LastName = "Smith"},
New Person() With {.FirstName = "Anne", .LastName = "Jones"}}
'
' Add people to BindingList DataSource
'
personBindList = New RemoveAndBind(Of Person) '(peopleList)
For Each person As Person In peopleList
personBindList.Add(person)
Next
'
' Display people in ListBox
'
ListBox1.DataSource = personBindList
End Sub
''' <summary>
''' Here we look to see if a new person was added, if
''' so pass the person to Operations class via NewPersonAdded procedure
''' which could be a function if something needs to come back
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub personBindList_ListChanged(sender As Object, e As ListChangedEventArgs) _
Handles personBindList.ListChanged
If Not currentlyLoading Then
If e.ListChangedType = ListChangedType.ItemAdded Then
Dim newPerson = personBindList.Item(e.NewIndex)
operations.NewPersonAdded(newPerson)
End If
Else
currentlyLoading = False
End If
End Sub
Private Sub AddPersonButton_Click(sender As Object, e As EventArgs) _
Handles AddPersonButton.Click
personBindList.Add(New Person() With {
.FirstName = FirstNameTextBox.Text,
.LastName = LastNameTextBox.Text})
End Sub
Private Sub RemoveCurrentPersonButton_Click(sender As Object, e As EventArgs) _
Handles RemoveCurrentPersonButton.Click
If personBindList.Count > 0 Then
personBindList.Remove(personBindList.Item(ListBox1.SelectedIndex))
End If
End Sub
Private Sub personBindList_FireBeforeRemove(sender As Object, e As ListChangedEventArgs) _
Handles personBindList.FireBeforeRemove
operations.RemovedPersonAdded(personBindList.Item(ListBox1.SelectedIndex))
End Sub
End Class
Well (to the developer) don't you study the code and read the docs along with stepping through the code? Developer replied with "I don't understand" and "I've never used breakpoints before".
This is inherently stops grow of any developer who is not reading the documentation along with never using the debugger.
Couple this in regards to growth to move past conventional methods for working with data e.g. move from DataTable to classes with proper interfaces and Entity Framework/Entity Framework Core.
Some recommendations for those developer who fall back on tried and true.
- Take time each week to self educate
- Read Microsoft documentation
- Read third party documentation when using non-Microsoft code.
- Learn to work with Visual Studio debugger.
- Learn how to write unit test
- Become a expert at searching the web for solutions rather than asking a question.
Comments
Post a Comment