tags:

views:

298

answers:

3

I am having asp.net page where i have combo box . I am highly confused that how to fill that combo because i am having two approach

  1. Fetch combobox data : by creating object of my database class. and call function for combobox data.

  2. Fetch combobox data : using static function

When should we create static function and how can we decide whether function should be static or not.

Suppose i need to fill No of people living in city based upon city Id and there is another condition of filter like business group, service group, students.

What is better approach of filling combobox.

+1  A: 

Function should be static if it's supposed to be stateless. Simple as that.

Arnis L.
It's not true. You can have static methods which uses static states. And you can have instance method, which does not uses any state. Both ways are correct.
TcKs
@TcKs There's a lot of things that 'you can do/have'. Question was - *when* to use static functions. Isn't statelessness a sign that you should use static functions?
Arnis L.
@No, "stateless" is not aign you should use static function. With static functions is using polymorphism much more complex than with instance methods.
TcKs
@TcKs Usage of static functions does not demand usage of complex static objects.
Arnis L.
A: 

You can have a lot of scenario how to fill your combobox. For example:

  • You can derive from ComboBox and you can fill they on on Load event (or on anyone else event, if you want)
  • You can have classes for combobox fill with same interface (for example: UserConboBoxFiller, InvoiceComboBoxFiller, ArticleComboBoxFiller, etc...)
  • You can have static methods for combobox fill - as you wrote. It's not wrong, in simple scenarios.

If you have several filter conditions for filling comboboxes, I recomend use the classes for filling:

public interface IComboBoxFiller {
    void Fill( ComboBox cbo );
}

public class UsersComboBoxFiller : IComboBoxFiller {
    public bool OnlyOnlineUsers {get;set;}

    public void Fill( ComboBox cbo ) {
        // there is logic for combobox filling
        // you can dynamicly generate where condition
        // by the "OnlyOnlineUsers"
    }
}
TcKs
A: 

You make your functions static if they do not need to work on class instances and access that instance state.

Static classes and functions are of common use in web applications because these applications are mostly stateless working over stateless HTTP. Or at least they mimic statefulness by using some tricks like sessions, cookies or injecting some helper content into HTML. But even so, there is almost no state in PC memory as such - objects are created to serve the request and deleted after the response has been sent. So, classes and functions are mostly there to pack user data and sent it to the database and in reverse direction. Mostly, just dataflow processing.

User