0

Im passing a long string to a method that will return the first 20 words, however when i return the value the old value is still in the string. any ideas? My code is as follows:

string textContent = txtDiaryText.Text.ToString().Trim();
int NumberOfWords = 20;

 getLimitedWords(textContent, NumberOfWords);

 {
    //do stuff with returned value;
 }

 public string getLimitedWords(string textContent, int NumberOfWords)
        {
            string[] Words = textContent.Split(' ');
            string _return = string.Empty;

            if (Words.Length <= NumberOfWords)
            {
                _return = textContent;
            }
            else
            {
                for (int i = 0; i < NumberOfWords; i++)
                {
                    _return += Words.GetValue(i).ToString() + " ";
                }
            }
            textContent = _return.ToString(); 
           return textContent;
        }

Edited by barriegrant1

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by barriegrant1
0

You are not setting the string to the return of the method. It should be:

textContent = getLimitedWords(textContent, NumberOfWords);

Or you could use a reference parameter, like this:

getLimitedWords(ref textContent, 20);

public string getLimitedWords(ref string textContent, int NumberOfWords){}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.