Passing object as parameter such as RichTextBox

  • Thread starter Thread starter MarkusR
  • Start date Start date
M

MarkusR

Good day,

I am basically writing a generic namespace with various helper classes.
I don't want to add the calling form to the referrences.

Example of what I am trying to do. Please let me know if i am going in
the wrong direction. I am rather new to C# and .NET.

I have a form in another namespace that on a button click I call
DoProcessing and pass the RichTextBox to log to.

namespace Utils
{
class MiscUtils
{
RichTextBox LogRichTextBox = null;

public static void DoProcessing(RichTextBox aRichTextBox)
{
LogRichTextBox = aRichTextBox;
PrintOutput("A Message");
}

public static void PrintOutput(string aMessage)
{
if (LogRichTextBox != null)
{
LogRichTextBox.AppendText(aMessage);
{
}
}
}
 
Markus,

That's fine, you wouldn't need to have a reference to your form, you
just have to pass the RichTextBox in (which would require a reference to
System.Windows.Forms, but I think you are ok with that).

Hope this helps.
 
I figured it out. Sorry, I am a newbie at c#.

This is how I did it:

In the form:

private void button1_Click(object sender, EventArgs e)
{
MiscUtils myUtil = new ProcessLotUtils(mLog);
myUtil.DoProcessing();
}

In my utility namespace:

namespace Utils
{
class MiscUtils
{
RichTextBox LogRichTextBox = null;

// Default constructor:
public MiscUtils()
{
LogRichTextBox = null;
}

// Constructor:
public MiscUtils(RichTextBox aRichTextBox)
{
LogRichTextBox = aRichTextBox;
}

public void DoProcessing()
{
PrintOutput("A Message");
}

public static void PrintOutput(string aMessage)
{
if (LogRichTextBox != null)
{
LogRichTextBox.AppendText(aMessage);
{
}
}
}

Hopefully there aren't any types. Just typed up the example.

Thanks Nicholas for your help.

-Markus
 
Back
Top