Help with Generic Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all, below at the bottom are 2 nearly identical overloads for the same
method. As you can see the algorithm is exactly the same. However try as I
may I cannot seem too create a usefull Generic method, i.e. one that compiles.

What I would like to be able to do is something like the following when I
call it.
this.UpdateAnswer<TextBox>(question, answer);

Or better yet.
this.UpdateAnswer<typeof(SomeUiElement.GetType())>(question, answer);

Is this type of thing at all possible with generics or am I headed down the
wrong path?

private void UpdateAnswer(PaginationPageQuestion question, TextBox answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Trim().Equals(string.Empty) ?
answer.Text.Trim() :
null
;
}
}

private void UpdateAnswer(PaginationPageQuestion question, ComboBox
answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Trim().Equals(string.Empty) ?
answer.Text.Trim() :
null
;
}
}
 
Hi all, below at the bottom are 2 nearly identical overloads for the same
method. As you can see the algorithm is exactly the same. However try as I
may I cannot seem too create a usefull Generic method, i.e. one that compiles.

What I would like to be able to do is something like the following when I
call it.
this.UpdateAnswer<TextBox>(question, answer);

Or better yet.
this.UpdateAnswer<typeof(SomeUiElement.GetType())>(question, answer);

Is this type of thing at all possible with generics or am I headed down the
wrong path?

private void UpdateAnswer(PaginationPageQuestion question, TextBox answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Trim().Equals(string.Empty) ?
answer.Text.Trim() :
null
;
}
}

private void UpdateAnswer(PaginationPageQuestion question, ComboBox
answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Trim().Equals(string.Empty) ?
answer.Text.Trim() :
null
;
}
}

Dear John,

Your answer question must have a text property?

I would suggest using the strategy Pattern to answer the questions.
Sort of a delegate that will handle the return value of the answer.

Something like:
UpdateAnswer(question, answer, predicate);

To use generics you will have to find common properties for the types
that will be used..

Hope this helps.

Moty.
 
No need for generics, just use the common base class as the type for
your second argument:

private void UpdateAnswer(PaginatingPageQuestion question, Control
answer) {
...
}

This is not always possible, but in your particular case both classes
you're concerned with share a common base which declares the property
you want to check (Text).

HTH,

Sam
 
No need for generics, just use the common base class as the type for
your second argument:

private void UpdateAnswer(PaginatingPageQuestion question, Control
answer) {
...

}

This is not always possible, but in your particular case both classes
you're concerned with share a common base which declares the property
you want to check (Text).

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Yes,
Samuel is right in case of a Control.

If there will be cases in which the *answer* object type doesn't have
the text property (say Age property), it will not work..

Polymorphic calls are preferred in your case.

Moty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top