anyone got a minute, need some help with extention methods

  • Thread starter Thread starter Richard U
  • Start date Start date
R

Richard U

hey guys... was wondering if anyone got a min trying to work this thing out(less then 100lines) http://pastebin.com/Jm8YQNXg ... i am trying to add aextension method which is a filter to League object to allow filtering of fighters in the League , p.s. ive included the errors in the pastebin
 
hey guys... was wondering if anyone got a min trying to work this thing out (less then 100lines)http://pastebin.com/Jm8YQNXg... i am trying to add aextension method which is a filter to League object to allow filtering of fighters in the League , p.s. ive included the errors in the pastebin

I don't see what you are trying to do: where is the error, is there
an error? Then again I'm not that experienced and have done one
extension method in my life on my own, as a programming exercise. Why
do you want an extension method? Because you cannot change the
existing code otherwise or something?

Thanks for the pastebin link though, now I know about them*.

RL

*
What is Pastebin.com all about?
Pastebin.com is a website where you can store text for a certain
period of time. The website is mainly used by programmers to store
pieces of sources code or configuration information, but anyone is
more than welcome to paste any type of text. The idea behind the site
is to make it more convenient for people to share large amounts of
text online.
 
hey guys... was wondering if anyone got a min trying to work this thing out (less then 100lines) http://pastebin.com/Jm8YQNXg ... i am trying to add a extension method
which is a filter to League object to allow filtering of fighters in the League , p.s. ive included the errors in the pastebin

A League object contains a List<Fighter> but in the Filter method the
code doesn't reflect that relationship. Instead of
foreach (Fighter f in League)
try
foreach (Fighter f in League.Fighters)

regards
A.G.
 
Now, as far as the question goes, it seems to me that the primary issue
is that the type for the "this" parameter of your extension method
doesn't make any sense.  If you want to enumerate "Fighter" instances
from a collection, that collection should be "IEnumerable<Fighter>", not
"IEnumerable<League>".

That said, note that where you actually try to use the extension method,
the type is of neither "IEnumerable<Fighter>" or "IEnumerable<League>".
  You are only implementing the non-generic "IEnumerable" type, which
won't match the parameter type for the extension method.  So even with
the extension method fixed, your code won't compile.

You either need to make the extension method's "this" parameter
non-generic, or implement the generic "IEnumerable<Fighter>" interface
in your "League" type.

Some other things to point out about the code (in no particular order):

How do you know all this stuff just by eyeballing some confusing
code? You making this stuff up as you go along? Probably.

BTW, I cannot understand why Generic types <T> even exist, except for
the obvious 'you catch type safe mistakes on compile' but that's such
a weak rationale for using such a confusing looking nomenclature.

RL
 
- good stuff snipped -

How do you know all this stuff just by eyeballing some confusing
code? You making this stuff up as you go along? Probably.

Being able to read and understand someone else's code, tortured or
not, is a valuable asset. Pete took the time to read the code,
interpret what the code was intended to do, and then post what he
found. You should be much more appreciative of his efforts. With some
effort on your part you might learn something.
BTW, I cannot understand why Generic types <T> even exist, except for
the obvious 'you catch type safe mistakes on compile' but that's such
a weak rationale for using such a confusing looking nomenclature.
It's the OO paradigm about not having to re-invent the wheel over and
over again. The OP could have written a FighterList type as a
type-safe for Fighter wrapper around a List type. Then the OP could
have written a LeagueList type as a type-safe for League wrapper
around a List type. Both types provide limited re-usability.

Generic types provide the same type-safe functionality in an eminently
re-usable form. A major benefit of type-safety is not having to cast
and then verify the cast for every reference to a collection member.

regards
A.G.
 
- good stuff snipped -





Being able to read and understand someone else's code, tortured or
not, is a valuable asset. Pete took the time to read the code,
interpret what the code was intended to do, and then post what he
found. You should be much more appreciative of his efforts. With some
effort on your part you might learn something.

Doubtful Pete did anything useful. Let's wait to hear from the user.
Anybody can critique code--even beginners like you.
It's the OO paradigm about not having to re-invent the wheel over and
over again. The OP could have written a FighterList type as a
type-safe for Fighter wrapper around a List type. Then the OP could
have written a  LeagueList type as a type-safe for League wrapper
around a List type. Both types provide limited re-usability.

Generic types provide the same type-safe functionality in an eminently
re-usable form. A major benefit of type-safety is not having to cast
and then verify the cast for every reference to a collection member.

Yes you are taking about Generic Lists, which I use all the time. But
generic types in general are a waste IMO.

RL
 
RayLopez99 said:
Doubtful Pete did anything useful. Let's wait to hear from the user.
Anybody can critique code--even beginners like you.

Hmm, it seems like you have already admitted that YOU couldn't do it.
 
Doubtful Pete did anything useful. Let's wait to hear from the user.
Anybody can critique code--even beginners like you.
Peter answered the OP's question in detail and provided some
informative comments about the OP's code. Your evaluation is far from
universal and does not negate the value of Pete's explanations. He
certainly wasn't, as you've previously suggested, making things up.

I didn't critique any code, in a response to the OP I simply pointed
the obvious error.
Yes you are taking about Generic Lists, which I use all the time. But
generic types in general are a waste IMO.
Huh? Given the most common use of generics is to create collection
classes, the two sentences regarding generics appear to be
contradictory. You make me laugh.

regards
A.G.
 
Huh? Given the most common use of generics is to create collection
classes, the two sentences regarding generics appear to be
contradictory. You make me laugh.

Jokes on you. You just admitted you've never used a Genetic Type
outside of a collection class. Well you really should not. But you
can, and you don't even know how you can.

RL
 
Yes you are taking about Generic Lists, which I use all the time. But
generic types in general are a waste IMO.

From the practical perspective it is difficult to support generic
collections without supporting generics.

But besides that generics can also be useful outside
collections.

General reading and writing methods is one example.

Usage outside of collections may not justify the extra complexity,
but having the feature for collections makes it cost free for other
usage.

Arne
 
Usage outside of collections may not justify the extra complexity,
but having the feature for collections makes it cost free for other
usage.

Yes, that was my point. I use Generic Types in Lists all the time, but
outside of lists? No.

On another but related theme--I'm going through the "Gang of Four"
book, and it's fun to see these different patterns, mostly dealing
with inheritance, but as a practical matter I doubt most people use
them (of course they are used in libraries), except of course for the
"composite" pattern, which is used by everybody doing OO. I might
start another thread on this.

RL
 
Jokes on you. You just admitted you've never used a Genetic Type
outside of a collection class.

Re-read what was written and find where any mention is made concerning
my use of generics. Your continuing unfounded speculation is amusing.

regards
A.G.
 
Yes, that was my point. I use Generic Types in Lists all the time, but
outside of lists? No.

You really should consider using generics outside collections sometimes.

Serialization style read/write, algorithms etc. are often good
candidates for being generic.

Arne
 
Back
Top