Reputation: 185
I have a function that is giving me a Console.Writeline output, but it's not writing the information to my richTextBox.
public static void FindFeatureSecurityEsc(string[] actualFontName, string displayFontName, string dragDropSourceData, string outputData)
{
//Create a list to hold all the fonts
var listOfDisplayFonts = new List<string>();
string[] arrayOfDisplayFonts = listOfDisplayFonts.ToArray();
//Display font name for each font that exists in the dragdrop rich text box.
if (dragDropSourceData.Contains(actualFontName[0]) || dragDropSourceData.Contains(actualFontName[1]) || dragDropSourceData.Contains(actualFontName[2]))
{
//If the font doesn't already exist in the output textbox.
if (!outputData.Contains(displayFontName))
{
//Add font to the output textbox.
listOfDisplayFonts.Add(displayFontName);
foreach (string s in listOfDisplayFonts)
{
//Add strings from list to text box.
outputData += string.Join(Environment.NewLine, s) + "\n";
}
//Clear fonts from the list (They've already been added to the textboxes).
listOfDisplayFonts.Clear();
}
}
Console.WriteLine(outputData);
}
In this example, outputData
is equal to richTextBox1.Text
(the last parameter). When I am using this function in my Main.cs, these are the parameters.
FontFinder.FindFeatureSecurityEsc(EscapeSequence.fontEsc12pt, EscapeSequence.fontDispName, dragDropRichTextBox1.Text, richTextBox1.Text);
Why is it that the Console.Writeline is giving me the proper output, but richTextBox1.Text remains empty? I know the function is proper, because if I call it directly from my Main.cs (and fill the actual parameter values into the function), it works fine. This is mostly an educational exercise to help me learn.
Upvotes: 0
Views: 503
Reputation: 94
Looks like you are only using the richTextBox1.Text to display the output, since you are passing it by value. Hence the Console.Writeline output is correct. You would be able to get the result in the richTextBox.Text if you pass it by ref. That way you don't even have to change the return type of your method and assign it back to the control's text.
Upvotes: 0
Reputation: 72854
You're passing the text string of richTextBox1
to the method. The statement outputData += string.Join(Environment.NewLine, s) + "\n";
would concatenate text and create a new string (a string is immutable) but the original text string that was passed remains the same.
You need to assign back the result to richTextBox1
:
richTextBox1.Text = FontFinder.FindFeatureSecurityEsc(EscapeSequence.fontEsc12pt,
EscapeSequence.fontDispName, dragDropRichTextBox1.Text, richTextBox1.Text);
Of course in this case you should change the method's return type from void
to string
and return outputData;
at the end.
Upvotes: 4
Reputation: 45135
Quick fix:
At the end of your function, do this (obviously changing the return type to string
from void
as well):
return outputData;
So your function becomes:
public static string FindFeatureSecurityEsc(string[] actualFontName, string displayFontName, string dragDropSourceData, string outputData)
{
// everything the same as before...
return outputData;
}
Then you can call it like this:
richTextBox1.Text = FontFinder.FindFeatureSecurityEsc(EscapeSequence.fontEsc12pt,
EscapeSequence.fontDispName, dragDropRichTextBox1.Text,
richTextBox1.Text);
Strings are immutable. That means whenever you concatenate or do any other kind of string manipulation you are actually creating a new string. Your control's .Text
property is still pointing to the old string so it won't "see" any of the changes.
The other approach here would be to change your function to take the control instead of the just the string:
public static void FindFeatureSecurityEsc(string[] actualFontName, string displayFontName, string dragDropSourceData, RichTextBox outputDataCtrl)
{
var outputData = outputDataCtrl.Text;
// rest of your function as before
outputDataCtrl.Text = outputData;
}
Which approach is better is up to you.
Upvotes: 2
Reputation: 150108
In this example, outputData is equal to richTextBox1.Text (the last parameter).
It might have the same string value when you start, but it is not the same variable.
You pass in outputData
.
FindFeatureSecurityEsc(string[] actualFontName, string displayFontName, string dragDropSourceData, string outputData)
Then you modify it a bunch, and finally write it to the console.
Console.WriteLine(outputData);
You never assign your new outputData
back to any control.
The outputData
that you passed in is a different one than the final one you write to the console, because strings are immutable. That's a fancy way of saying you don't modify the original string, but rather you create a new one.
To solve the problem, assign outputData
to the control. You can do that within the function you posted if it has access to the control, or it can return outputData
back instead of having a void
return type.
Upvotes: 3