Extention method return types

  • Thread starter Thread starter Yankee Imperialist Dog
  • Start date 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!!!
 
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
 
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?
 
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.
 
noted as for the text, i wanted to keep this example simple for an answer.
Thanks you , you answered my question.
Very appreciated
 
Back
Top