Hi guys ,I have written a program which filter the text using replace function.What i want to do is to create another class and do all the filtering over there .How can i achieve this.I am pretty new to C# so any help would be appreciated.
Thanks

Why don't you use a method instead of a class?

Add this above public Form1() { ...

string FilterText(string text)
{
   // do all the filtering here
}

And you can use it like this:

string text = "Hello, World.";

text = FilterText(text);

Thanks

And if you really NEED to do it in another class, it'd just be an abstraction of what farooqaaa provided above but would require an appropriate constructor for the class object allowing pass-in of the string you're filtering.

The Filtertext fuction, how will i access that as i suppose this will be in another class.So should i create an instance of another class like

Class2 class = new Class2

The Filtertext fuction, how will i access that as i suppose this will be in another class.

If you are following farooqaaa's suggestion it doesn't need to be in another class, it can be a function within your main program and therefor doesn't require an instanciation of a new class to perform. Simply insert the function within your code outside of your other functions and call it as required in the way that farooqaaa indicated.

All *I* was saying is that it is POSSIBLE to create it as a function of an external class, not that it was better to do so. Unless you have a specific purpose or need for separating it to an external class it would be easier/better to simply include it in your main program area.

Edited 6 Years Ago by Lusiphur: n/a

Posted above is not a class, instead he has suggested you use a method which is called by simply:
text = FilterText(text);
(as it says in his post)

Note that a method comes inside a class, e.g.

Class Program
{
   static void Main()
   {
      string text = "text";
      text = FilterText(text);
   }

   static string FilterText(string text)
   {
      //Do something...
      return text;
   }
}

... could be the basic structure of your program

On a further note, a method can be accessed from another class if it public.
Having your method in another class is as simple as making a new class, copying & pasting the code across, and then calling the method with
ClassName.MethodName()

Edited 6 Years Ago by Medalgod: Further explanation

Thanks for the help guys but i do need to create another class and then create a method in that class and then access it in the main program.My next question is do i have to create set and get method for that.Is the foloowing approach ok:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextFiltering
{
    public class TextCleaning
    {
        private string text = "";
        private string[] formatsymbols = new string[] {" ","section_text=",""};
        private string[] specialsymbols = new string[] { "'" };
        public string Text
        {
            set { text = value; }
            get { return text; }
        }



    }
}

and if this is ok what am i suppose to do next?
thanks once again

Whilst that would work, it is not required. Get and set should only be used if you want additional checks on the data before recording or returning it. Or ofcoarse if you want it to be read only. You should just use a public variable for Text.

In order to make a new instance of an object from your program's code do:

TextCleaning txtClean = new TextCleaning();

and i'm sure you'll find the rest for yourself :)

This article has been dead for over six months. Start a new discussion instead.