Extention method return types

  • Thread starter Yankee Imperialist Dog
  • Start date
Y

Yankee Imperialist Dog

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!!!
 
J

Jon Skeet [C# MVP]

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
 
Y

Yankee Imperialist Dog

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?
 
J

Jon Skeet [C# MVP]

Yankee Imperialist Dog said:
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.
 
Y

Yankee Imperialist Dog

noted as for the text, i wanted to keep this example simple for an answer.
Thanks you , you answered my question.
Very appreciated
 

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

Top