TextWriter problem.

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

Hello,

I have the following:

TextWriter writter;
Action<TextWriter> action = new Action<TextWriter>(writter);

But I kept getting the error:

Error 1 'writter' is a 'variable' but is used like a 'method'

I am not sure what I am missing and why is says writter is being used
as a method ...

Anyone?

Thank You,
Miguel
 
First, I would spell it "writer", not "writter".

Second, you _are_ using it as a method, exactly as the error message
says.  The type "Action<TextWriter>" is a delegate type, and all
delegate type constructors require a method name, used to specify the
method to be called when the delegate is invoked.

At best, you could do something like this:

   void SomeMethod()
   {
     TextWriter writer = …;
     Action<TextWriter> action = MyTextWriterAction;

     action(writer);
   }

   void MyTextWriterAction(TextWriter writer)
   {
     // do something with 'writer'
   }

Pete

Yes, Writter with two t's was a mistake.

I am not sure I understand what you mean. Basically, I have the
following:

public static HelperResult Join(this HtmlHelper helper,
HelperResult result1, HelperResult result2) {
return new HelperResult(writer => writer.Write(result1 +
result2.ToString()));
} // HelperResult

Which I would like to turn into:

public static HelperResult Join(this HtmlHelper helper,
IList<HelperResult> results) {

TextWriter writer = new StreamWriter();
Action action = new Action<TextWriter>(writer);
foreach (HelperResult result in results)
writer.Write(result);

return new HelperResult(new Action<TextWriter>(writer));

} // HelperResult

HelperResult has only one constructor of type:

HelperResult(Action<TextWriter>(writer));

Well, I am not sure I need to do this. My problem would be solved if I
could use the IList<HelperResult> into:

return new HelperResult(writer => writer.Write(result1 +
result2.ToString()));

I think there is a way to, maybe using linq, to do:

result1inlist + result2inlist + result3inlist into:

writer => writer.Write(...Here ...)

But my trials do not work out ....
 
[...]
I am not sure I understand what you mean.

I mean your code to create a new instance of Action<TextWriter> makes no
sense.  You can't pass a reference to an actual TextWriter instance to a
constructor that expect a method name.
Basically, I have the following:
     public static HelperResult Join(this HtmlHelper helper,
HelperResult result1, HelperResult result2) {
       return new HelperResult(writer =>  writer.Write(result1 +
result2.ToString()));
     } // HelperResult

That seems pretty goofy to me.  Granted, you didn't bother to post the
declarations for HtmlHelper and HelperResult, so maybe there's some
super-awesome functionality they support and which require the above
kind of code.

But on the face of it, it's not at all clear why HelperResult requires a
delegate that will write a specific string to a TextWriter instance,
when you presumably could just store the concatenated string itself and
write it to any arbitrary TextWriter passed to it at any time.
Which I would like to turn into:
     public static HelperResult Join(this HtmlHelper helper,
IList<HelperResult>  results) {
       TextWriter writer = new StreamWriter();
       Action action = new Action<TextWriter>(writer);

This can't compile.  Not only does it have the same "variable used as a
method" problem you started with said:
       foreach (HelperResult result in results)
         writer.Write(result);

Your original "Join" method doesn't actually write to the TextWriter.
Why should the new version do so?  Furthermore, what is the point of
creating the "action" delegate instance when you never actually use it?
       return new HelperResult(new Action<TextWriter>(writer));
     } // HelperResult
HelperResult has only one constructor of type:
   HelperResult(Action<TextWriter>(writer));

Why does it have that one constructor?  What is the purpose of passing a
TextWriter instance to a HelperResult constructor?
Well, I am not sure I need to do this. My problem would be solved if I
could use the IList<HelperResult>  into:
   return new HelperResult(writer =>  writer.Write(result1 +
result2.ToString()));
I think there is a way to, maybe using linq, to do:
    result1inlist + result2inlist + result3inlist into:
writer =>  writer.Write(...Here ...)
But my trials do not work out ....

 From all that, it seems all you really want to do is extend your
original "Join" method so that it can join more than just two
HelperResults.  Why not just write it like this:

   public static HelperResult Join(this HtmlHelper helper, params
HelperResult[] results)
   {
     StringBuilder sb = new StringBuilder();

     foreach (HelperResult result in results)
     {
       sb.Append(result.ToString());
     }

     return new HelperResult(writer => writer.Write(sb.ToString()));
   }

The TextWriter part still looks to me to have dubious value.  But at
least the above ought to be what you seem to be trying to do.

(Note that I choose to support the variable parameter list syntax,
rather than your IList<HelperResult> approach.  There's no indication in
your post that you really need an IList<T>; at most, an IEnumerable<T>
should be sufficient.  And frankly, for the usage that it appears you're
trying to do, the "params" syntax is likely to be more convenient.  If
you start with an IList<T>, just use the ToArray() extension method, and
of course the caller can just list as many HelperResults in the method
call as they like, and the compiler will take care of converting to an
array for you).

Pete

Hello Pete,

Thank You Very Much. That was exactly what I was looking for.

And the change from IList to params is simply great!

I didn't post the other code because it is MSFT code and it is still
in Release Candidate and source was not release yet.

Thank You!
Miguel
 
Back
Top