HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Setting fields, with default values, before saving a row

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withfieldssettingdefaultvaluesbeforerowsaving

Problem

I would like to find out if there's a cleaner way to go about validating input before it goes into a function. What I'm doing seems like a lot of code, I'm sure there's a better way. I would also really appreciate some constructive criticism as my goal is to grow as a developer so anything you may see wrong with it or something you would've done better please mention it.

The code below does get the job done. I have tested it and it works, however I would like to improve its beauty and efficiency.

```
Private Sub dgvproformas_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvproformas.CellContentClick

Dim colname As String = ""

colname = dgvproformas.Columns(e.ColumnIndex).Name

Select Case colname

Case "btnsave"

Dim jobid As Integer = 0
Dim proformano As String = ""
Dim paymentreceiveddate As DateTime = Nothing
Dim paymentreceived As Boolean = False
Dim proformarequired As Boolean = False
Dim proformaraised As Boolean = False

jobid = dgvproformas.Item("job id", e.RowIndex).Value

If Not IsDBNull(dgvproformas.Item("proformano", e.RowIndex).Value) Then
proformano = dgvproformas.Item("proformano", e.RowIndex).Value
End If

If Not IsDBNull(dgvproformas.Item("proformapaymentreceived", e.RowIndex)) Then
paymentreceived = dgvproformas.Item("proformapaymentreceived", e.RowIndex).Value
End If

If Not IsDBNull(dgvproformas.Item("proformarequired", e.RowIndex).Value) Then
proformarequired = dgvproformas.Item("proformarequired", e.RowIndex).Value
End If

If Not IsDBNull(dgvproformas.Item("proformaraised", e.RowIndex).Value) Then
proformaraised = dgvproformas.Item("proformaraised", e.RowIndex).Value
End If

If Not IsDBNull(dgvproformas.Item("proformapaymentreceiveddate", e.

Solution

The problem with your code is that you keep repeating the same code over and over again.

If Not IsDBNull(dgvproformas.Item("columnName", e.RowIndex).Value) Then
  columnVar = dgvproformas.Item("columnName", e.RowIndex).Value
End If


What you need is function that does the same thing, so that you have code that looks more like this:

columnVar= fn(dgvproformas, "columnName", e.RowIndex)


Except we want to keep type safety so, a bit more like:

columnVar = fn(Of Integer)(dgvproformas, "columnName", e.RowIndex)


Personally, I like extension methods for something like this, and would create an extension method for it. I would almost certainly name it GetValueOrDefault.

I believe you can eliminate the rowindex if you get the row, so combining the two...

Dim row = dgvproformas.Rows(e.RowIndex)
columnVar = row.GetValueOrDefault(Of Integer)("columnName")


Finally, I like meaningful names and dislike magic strings, so I like Nameof...

Dim row = dgvproformas.Rows(e.RowIndex)
columnName = row.GetValueOrDefault(Of Integer)(NameOf(columnName))

Code Snippets

If Not IsDBNull(dgvproformas.Item("columnName", e.RowIndex).Value) Then
  columnVar = dgvproformas.Item("columnName", e.RowIndex).Value
End If
columnVar= fn(dgvproformas, "columnName", e.RowIndex)
columnVar = fn(Of Integer)(dgvproformas, "columnName", e.RowIndex)
Dim row = dgvproformas.Rows(e.RowIndex)
columnVar = row.GetValueOrDefault(Of Integer)("columnName")
Dim row = dgvproformas.Rows(e.RowIndex)
columnName = row.GetValueOrDefault(Of Integer)(NameOf(columnName))

Context

StackExchange Code Review Q#147766, answer score: 3

Revisions (0)

No revisions yet.