Results 1 to 2 of 2

Thread: Any way to write a method that accepts generic parameter and assign value?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Any way to write a method that accepts generic parameter and assign value?

    Hi!

    I am working on refactoring an application that is written as a Model View Presenter (winforms).

    I am stuck on a scenario where the view fires events to the presenter when certain properties are changed. For example, if the user is selecting data in a combobox, an "CountryValueSelectedChanged" event is fired as an Action<int id> where id is the value selected. Same for textboxes where the keyup-event fires an OnFirstNameChanged event which is an Action<string name>.

    Would it be possible to write a single OnPropertyChanged<T> method, since both the view and the presenter are aware of T (which is the domain entity) Then I just need a way to inform this generic event two things

    1) The new value
    2) Which property that was changed.

    How do I do this in a clever way? I have on the presenter a property of type T which is called "selectedEntity; In the event handler for OnPropertyChanged<T> I want to be able to do:

    "selectedEntity.X = newValue.X;"

    How can I define X?

    /Henrik

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Any way to write a method that accepts generic parameter and assign value?

    Quote Originally Posted by MrNorth View Post
    Hi!

    I am working on refactoring an application that is written as a Model View Presenter (winforms).

    I am stuck on a scenario where the view fires events to the presenter when certain properties are changed. For example, if the user is selecting data in a combobox, an "CountryValueSelectedChanged" event is fired as an Action<int id> where id is the value selected. Same for textboxes where the keyup-event fires an OnFirstNameChanged event which is an Action<string name>.

    Would it be possible to write a single OnPropertyChanged<T> method, since both the view and the presenter are aware of T (which is the domain entity) Then I just need a way to inform this generic event two things

    1) The new value
    2) Which property that was changed.

    How do I do this in a clever way? I have on the presenter a property of type T which is called "selectedEntity; In the event handler for OnPropertyChanged<T> I want to be able to do:

    "selectedEntity.X = newValue.X;"

    How can I define X?

    /Henrik

    Hmm yes if i am understanding what you want correctly then wouldn't Dependency Injection do the job?

    You would need to do some reading up on DI as its not the easiest thing to explain and its quite a large subject but ......

    Have a read of this - http://stackoverflow.com/questions/1...ency-injection

    Read the accepted answer it's a pretty good explanation!
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width