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;
        }

Recommended Answers

All 2 Replies

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){}

Thanks alot AleMontriro, i knew it was somehting stupid that i missed. ;-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.