Coding format

scottsanpedro

Member
Joined
Jan 24, 2009
Messages
21
Programming Experience
1-3
Slightly confused on what is the best format of coding between Methods and properties.
Here is some code, a shared property that I use instead of using a method with one argument.
VB.NET:
Public Shared WriteOnly Property GetComboList() As ComboBox
        Set(ByVal _ComboBox As ComboBox)
            Try
                Dim td As New RAIDDataSetTableAdapters.COMBO_GET_VALUESTableAdapter
                Dim dt As New RAIDDataSet.COMBO_GET_VALUESDataTable

                td.Fill(dt, _ComboBox.Name.ToString)

                _ComboBox.DisplayMember = dt.CB_ValueColumn.ToString
                _ComboBox.ValueMember = dt.CB_ValueColumn.ToString
                _ComboBox.DataSource = dt
            Catch ex As Exception
                MessageBox.Show("error found." & vbCrLf & ex.Message, clsGeneral.ConTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End Set
    End Property
Is there any better way of doing this, or is this perfectly acceptable instead of using a method?
Many thanks
 
Why would you call a WriteOnly property Get....

Get implies it returns a value. WriteOnly means it cannot return a value


Properties are used to provide protected access to fields, ensuring that the values assigned to them are sensible and valid (or on the GET side of things, if there is some work to be done before it is returned such as If Nothing then 'make a new one)

Further you should note that properties typically have NOUN names:
Url, Host, Port, Height, Width

whereas methods typically have VERB names:
Send, Receive, Shorten, Raise, Process


Your process seems to perform quite an involved set of steps each time so it should be a METHOD
It should probably be a sub rather than a function, shouldnt return anything, and should take a single combobox as a parameter to wich a newly filled datatable of RAID whatevermajigs should be assigned:

VB.NET:
  Public Sub PopulateRaidCombo(_Combobox As ComboBox)

            Try
                Dim td As New RAIDDataSetTableAdapters.COMBO_GET_VALUESTableAdapter
                Dim dt As New RAIDDataSet.COMBO_GET_VALUESDataTable

                td.Fill(dt, _ComboBox.Name.ToString)

                _ComboBox.DisplayMember = dt.CB_ValueColumn.ToString
                _ComboBox.ValueMember = dt.CB_ValueColumn.ToString
                _ComboBox.DataSource = dt
            Catch ex As Exception
                MessageBox.Show("error found." & vbCrLf & ex.Message, clsGeneral.ConTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try


    End Sub
 
cjard,
Appreciate your quick response. It makes me think that the method is the preferred route. I am simply using this property SET as a way of calling a procedure. It works! but I wasn't too sure that it was the way to go.
It's nice when calling it from a form class as the
VB.NET:
clsGeneral.GetComboList = me.comboboxName
effectively calls the procedure and runs it.
I think your advise would be the way to go.
Thanks
Scott
 
It should probably be a sub rather than a function, shouldnt return anything
Though some ppl prefer to use functions only, that either return a "value" of something, or at least return true or false to indicate success or failure.
 
With this procedure in a separate class, would it be better to
1. Create the procedure in the class as shared and call it with arguments from the form class.
VB.NET:
clsCombo.GetComboList(me.ComboBoxName.ToString)
OR
2. Create it a private and create a new instance of the class and pass the parameter through via a SET property?
VB.NET:
Dim clsSomething as New clsComboBox
clsSomething.ComboName = me.ComboBoxName.ToString
clsSomething.Execute
Thanks again.
Scott
 
With this procedure in a separate class, would it be better to
1. Create the procedure in the class as shared and call it with arguments from the form class.
VB.NET:
clsCombo.GetComboList(me.ComboBoxName.ToString)
OR
2. Create it a private and create a new instance of the class and pass the parameter through via a SET property?
VB.NET:
Dim clsSomething as New clsComboBox
clsSomething.ComboName = me.ComboBoxName.ToString
clsSomething.Execute
Thanks again.
Scott


What are you actually trying to do, in high-level english?

I'm looking for an answer like:
"I want to dynamically create and add a combobox to a form and then fill it with some data from a database that will change according to what day it is"
 
Rather than looking at what the code does, it was a matter of looking more at the best practice to running a call to a class.
I think though it's more about 'if I want to expose or hide functionality to the application' which then creates the basis for the type of call I would make.
Please don't worry, and thanks for your time.
Scott
 
Back
Top