Peter said:
Well, it would have made sense if you'd shown us the lines of code
causing it. Instead, you showed us a line that most likely compiles
just fine, given your follow-up post.
I believe the problem you're having is with these two:
With the first, you can fix the error by casting correctly:
((TextBox)message).Text += text;
With the second, there is no correct line of code that is equivalent,
as strings are immutable.
It _looks_ like you are trying to accomplish something along the
lines of a TraceListener, TextWriter, etc. If so, you should
implement concrete versions of those classes that encapsulate the
underlying behavior you want. Then you would call the appropriate
methods on those objects, which would then already know exactly how
to modify the underlying object that should receive the new text.
Without knowing more about exactly what you're trying to do, it's
hard to suggest something specific.
What I am doing is creating a Log class to allow me to write to different
type of objects (TextBoxes, Labels, Strings etc). As another type comes up
I would just add it in.
In my example, I have one function (Write) that just adds a variable number
of strings to some object. The first parameter will always be the object I
am writing to.
********************************************
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
namespace RepositoryServiceApp.BusinessLayer
{
public class Log
{
public static void Write(params object[] names)
{
object destination = null;
foreach (object name in names)
{
if (destination == null)
{
destination = name;
}
else
{
if (name is string)
{
if (destination is TextBox)
{
((TextBox)destination).Text += (string)name;
}
if (destination is Label)
{
((Label)destination).Text += (string)name;
}
if (destination is string)
{
destination += (string)name;
}
}
}
}
}
public static void Write(object messageOut,string text)
{
}
}
}
********************************************
Then I could call it like so, where Status could be a TextBox or a string):
Log.Write(Status, parameter.ParameterName,"
",parameter.SqlDbType.ToString()," ",stemp,Environment.NewLine);
This seems to work pretty well.
I could also set up set a variable at the top of the program:
static object statusOutput = null;
Then later in my code I could set statusOutput to what ever I wanted:
Status(TextBox), stemp (string), etc and call it like so:
Log.Write(statusOutput, stext1," ",stext2,"
",stemp,Environment.NewLine);
This would allow me to use the same code to write status information to a
Multiline TextBox or a strint, or something else by just changing the
statusOutput at the top of the program.
Thanks,
Tom