views:

89

answers:

3

What is the best / good way to implement method calls.

For eg: From the below which is generally considered as best practice. If both are bad, then what is considered as best practice.

Option 1 :

   private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        getValue();
    }

    private void getValue()
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            TbxValue.Text = Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        else
        {
            TbxValue.Text = 0;
        }
    }

Option 2 :

    private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        int Value = getValue(FileName, PageNo);

        TbxValue.Text = Value.ToString();

    }

    private int getValue(string FileName, int PageNo)
    {
        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            return Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        return 0;
    }

I understand we can pass parameters directly without assigning to a local variable... My question is more about the method definition and the way it is handled.

A: 

According to your example, option 2 is the way to go. Option 1 knows about your form and how to display data on it, which violates the SRP.

alexn
Yes but should a buttons event interact with a textbox? i think maybe adding a property that would in turn set the textbox might be called for. then again why not implement mvc?
griegs
Well, in my opinion, controls are (and can also be) thigtly coupled in a simple, single, winforms application. In the end, it boils down to how complex the application is and what separations you need.
alexn
An therein lies the problem in simple winforms but the OP doesn't mention winforms and the event looks webforms to me
griegs
These are private methods, both of which presumably live on the form.
Jon Skeet
But if need to make the same action happen (like populating the amount field) from elsewhere (in some other event), I need to again gather the parameter and pass it to the method right.. But in option 1, I can just call the method and it will take care of its need
The King
+1  A: 

here is what i like to to if i don't implement mvc. and i'm assuming web here.

I'd do option 2 first but instead of having the buttons code set the text id create a property to set the text boxs value.

I do this because if something else sets the textbox value then you are going to duplicate code. bad if you change a name or control type.

griegs
Do you want me to create 15 Properties in my Form 1 for each TextBox. What will be the case for other controls?
The King
I'm just saying that I would never actually have a button even update a textbox. And yeah, i probably might add 15 properties. especially if i were massaging the data before posting
griegs
+6  A: 

If you're subscribing to the event automatically, I don't think it's particularly bad to have a method with the event handler signature which just delegates to a method which has the "real" signature you need (in this case, no parameters).

If you're subscribing manually, you can use a lambda expression instead:

postUpdateButton.Click += (sender, args) => PostUpdate();

and then do the work in PostUpdate. Whether you then split up the PostUpdate into two methods, one to deal with the UI interaction and one to deal with the BLL interaction is up to you. In this case I don't think it matters too much.

How you structure UI logic to make it testable is a whole different matter though. I've recently become a fan of the MVVM pattern, but I don't know how applicable that would be to your particular scenario (it's really designed around Silverlight and WPF).

A couple of other comments though:

  • Conventionally, parameters should be camelCased, not PascalCased
  • Do you genuinely believe you're getting benefit from prefixing local variables with l_? Isn't it obvious that they're local? Personally I'm not keen on most of the variable names shown here - consider naming variables after their meaning rather than their type.
  • Using a DataTable to return information is a somewhat error-prone way of doing things. Why can the BLL not return an int? to indicate the value (or a lack of value)?
Jon Skeet
The King