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.UpdateAnsw er<TextBox>(que stion, answer);
Or better yet.
this.UpdateAnsw er<typeof(SomeU iElement.GetTyp e())>(question, answer);
Is this type of thing at all possible with generics or am I headed down the
wrong path?
private void UpdateAnswer(Pa ginationPageQue stion question, TextBox answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Tr im().Equals(str ing.Empty) ?
answer.Text.Tri m() :
null
;
}
}
private void UpdateAnswer(Pa ginationPageQue stion question, ComboBox
answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Tr im().Equals(str ing.Empty) ?
answer.Text.Tri m() :
null
;
}
}
--
Thank you,
John 3 1101
On May 18, 11:35 pm, John A <i-code4f...@newsg roups.nospamwro te:
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.UpdateAnsw er<TextBox>(que stion, answer);
Or better yet.
this.UpdateAnsw er<typeof(SomeU iElement.GetTyp e())>(question, answer);
Is this type of thing at all possible with generics or am I headed down the
wrong path?
private void UpdateAnswer(Pa ginationPageQue stion question, TextBox answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Tr im().Equals(str ing.Empty) ?
answer.Text.Tri m() :
null
;
}
}
private void UpdateAnswer(Pa ginationPageQue stion question, ComboBox
answer)
{
if (answer != null)
{
question.Answer = !answer.Text.Tr im().Equals(str ing.Empty) ?
answer.Text.Tri m() :
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(qu estion, 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(Pa ginatingPageQue stion 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-*********@newsg roups.nospamwro te:
>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.UpdateAns wer<TextBox>(qu estion, answer);
On May 18, 11:52 pm, Samuel R. Neff <samueln...@nom ail.comwrote:
No need for generics, just use the common base class as the type for
your second argument:
private void UpdateAnswer(Pa ginatingPageQue stion 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...@newsg roups.nospamwro te:
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.UpdateAnsw er<TextBox>(que stion, 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 properties. Something similar to a
java hash table. One can add new properties at runtime and remove them
as well.
I have a really fancy third party property inspector available to me
that I would very much like to reuse. This inspector uses...
|
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 some very
important ones seem to be missing.
E.g., the multiple class inheritance is great,
but there are no generic functions (at least that
i can find). If i have classes X, Y, and Z,
and subclasses X_sub, Y_sub, and Z_sub respectively.
|
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
|
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 understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined...
|
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 from
www.franson.com
For some reason my code below doesnt work - the GpsFixEvent never seems
to "fire" as it were.
| |
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 the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
|
by: Dave |
last post by:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegate;
instantiation:
displayMsgDelegate = DisplayStatusMessage;
implementation:
public void DisplayStatusMessage(string statusMessage)
|
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 delegate
type expression in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking.
Note that since 'boxing' and 'unboxing' is involved (I think), you
don't get what you...
|
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
GetMethods()
MethodInfo methods = t.GetMethods();
|
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 is used to access and read the value stored in it
C.A variable is allocated or deallocated in memory during runtime
D.A variable can be initialized at the time of its creation or later
2. The.……types feature facilitates the definition of classes...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |