471,317 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Extention method return types

I'm do meore and more with extention methods, totally cool. They are making
life much easier for IU developers. I'm trying to add a return type of struct
to a asp.net DropDownList of type struct. The idea is that if the conversion
to say Int32 failed and a default value was used i can also return a
Pass/Fail bool, int code to let the developer know that it failed or the
reason for the failure.

Where should the struse be defined for the return type? It's a simple
question, but i really don't know the answer.

Has anyone else done this and do you know and artical or have an example.

thanks!!!
--
Share The Knowledge. I need all the help I can get and so do you!
Aug 1 '08 #1
4 1182
On Aug 1, 3:12*pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
I'm do meore and more with extention methods, totally cool. They are making
life much easier for IU developers. I'm trying to add a return type of struct
to a asp.net DropDownList of type struct. The idea is that if the conversion
to say Int32 failed and a default value was used i can also return a
Pass/Fail bool, int code to let the developer know that it failed or the
reason for the failure.

Where should the struse be defined for the return type? It's a simple
question, but i really don't know the answer.

Has anyone else done this and do you know and artical or have an example.
A few questions:
1) Why do you want it to be a struct?
2) Why do you want to use a code for the failure instead of a message?

As for where the type is defined - anywhere, basically. An extension
method is just a normal static method with some extra syntactic sugar,
so the normal rules apply.

Jon
Aug 1 '08 #2
I appreciate the very prompt reply!
1. I would like two values returned, would a class be a better choice?
1.a I can call two seprate extention methods for valueexists then ToInt32,
but i may also want to retrun the reason like it's empty, it's null, it's
just not there.

2. Then in the class file that defines the methods for the name space would
make sense?
--
Share The Knowledge. I need all the help I can get and so do you!
"Jon Skeet [C# MVP]" wrote:
On Aug 1, 3:12 pm, Yankee Imperialist Dog
<YankeeImperialist...@discussions.microsoft.comwro te:
I'm do meore and more with extention methods, totally cool. They are making
life much easier for IU developers. I'm trying to add a return type of struct
to a asp.net DropDownList of type struct. The idea is that if the conversion
to say Int32 failed and a default value was used i can also return a
Pass/Fail bool, int code to let the developer know that it failed or the
reason for the failure.

Where should the struse be defined for the return type? It's a simple
question, but i really don't know the answer.

Has anyone else done this and do you know and artical or have an example.

A few questions:
1) Why do you want it to be a struct?
2) Why do you want to use a code for the failure instead of a message?

As for where the type is defined - anywhere, basically. An extension
method is just a normal static method with some extra syntactic sugar,
so the normal rules apply.

Jon
Aug 1 '08 #3
Yankee Imperialist Dog <Ya******************@discussions.microsoft.com>
wrote:
I appreciate the very prompt reply!
1. I would like two values returned, would a class be a better choice?
Either would be okay, but class is a better "default choice". Unles
you've got a reason to make it a value type, I'd stick with a class.
1.a I can call two seprate extention methods for valueexists then ToInt32,
but i may also want to retrun the reason like it's empty, it's null, it's
just not there.
Returning a reason is fine, but a string tends to be easier to
understand than an integer.
2. Then in the class file that defines the methods for the name space would
make sense?
No, the class which defines the extension methods has to be a static
class, but you could do it in the same namespace.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 1 '08 #4
noted as for the text, i wanted to keep this example simple for an answer.
Thanks you , you answered my question.
Very appreciated
--
Share The Knowledge. I need all the help I can get and so do you!
"Jon Skeet [C# MVP]" wrote:
Yankee Imperialist Dog <Ya******************@discussions.microsoft.com>
wrote:
I appreciate the very prompt reply!
1. I would like two values returned, would a class be a better choice?

Either would be okay, but class is a better "default choice". Unles
you've got a reason to make it a value type, I'd stick with a class.
1.a I can call two seprate extention methods for valueexists then ToInt32,
but i may also want to retrun the reason like it's empty, it's null, it's
just not there.

Returning a reason is fine, but a string tends to be easier to
understand than an integer.
2. Then in the class file that defines the methods for the name space would
make sense?

No, the class which defines the extension methods has to be a static
class, but you could do it in the same namespace.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Chris S. | last post: by
1 post views Thread by Carl Ogawa | last post: by
3 posts views Thread by Tuvas | last post: by
18 posts views Thread by Daniel Gustafsson | last post: by
3 posts views Thread by Paul Michaud | last post: by
5 posts views Thread by Krustov | last post: by
9 posts views Thread by VK | last post: by
reply views Thread by rosydwin | last post: by

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.