473,511 Members | 16,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Generic Method

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
;
}
}

--
Thank you,
John
May 18 '07 #1
3 1097
On May 18, 11:35 pm, John A <i-code4f...@newsgroups.nospamwrote:
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
;
}
}

--
Thank you,
John
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.

May 18 '07 #2

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.

On Fri, 18 May 2007 13:35:00 -0700, John A
<i-*********@newsgroups.nospamwrote:
>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);
May 18 '07 #3
On May 18, 11:52 pm, Samuel R. Neff <samueln...@nomail.comwrote:
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.

On Fri, 18 May 2007 13:35:00 -0700, John A

<i-code4f...@newsgroups.nospamwrote:
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);
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

May 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3744
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
3
2877
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
10
2037
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
3
31434
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
0
2217
by: BigAl.NZ | last post by:
Hi Guys, I am trying to write/copy some code that uses events with a GPS. Everytime the GPS position updates the event fires. The GPS code is from a SDK Library that I got called GPS Tools...
9
5830
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
7
2034
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
26
3591
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
2328
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
2
9999
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.