Wolfish
Wolfish

Reputation: 970

Clearing data before reading a new line

I have a considerable number of strings in my application that need to be cleared each time I get new data from my source. I'd like to use something akin to string.Empty, but I am unsure of how to implement this. Ideally, I'd also like to do this only once, rather than for each separate string.

Pseudo-code:

foreach (string in application)
{
    this.empty
}

Am I thinking on the right track?

Some of my code is as follows:
classtoinstantiate

public string Str1;
private string str1 {get {return Str1;}}
public void DoStuff()
{
    doStuff();
}
private void doStuff()
{
    //dostuff
}

And Form1.cs

classtoinstantiate class1 = new classtoinstantiate();
class.DoStuff();
//I would like to then clear the *public* iteration of string Str1 here, 
                                                      //before I DoStuff() again.

Upvotes: 0

Views: 89

Answers (5)

Arie
Arie

Reputation: 5373

public class StringCollection
{
    public string StringProp1 { get; set; }
    public string StringProp2 { get; set; }
    public string StringProp3 { get; set; }
    public string StringProp4 { get; set; }

    // .... more properties here

    // this property won't be touched when clearing
    public int SomeOtherProperty{ get; set; }

    public void ClearStrings()
        {
            // returns all public properties
           foreach (var prop in this.GetType().GetProperties())
        {
            // "clear" only properties of type String and those that have a public setter
            if (prop.PropertyType == typeof(string) && prop.CanWrite) 
                prop.SetValue(this, string.Empty, null); // <- "clear" value of the property 
        }
    }

or, in a more general manner - use extension methods:

 public class StringCollection
    {
        public string StringProp1 { get; set; }
        public string StringProp2 { get; set; }
        public string StringProp3 { get; set; }
        public string StringProp4 { get; set; }

        public int SomeOtherProperty { get; set; }
    }


    public class SomeOtherClass
    {
        public string a1 { get; set; }
        public string a2 { get; set; }
        public string a3 { get; set; }

        public DateTime d1 { get; set; }
        public int SomeOtherProperty { get; set; }
    }

    public static class MyExtensions
    {
        public static void ClearStrings(this Object obj)
        {
            // returns all public properties
            foreach (var prop in obj.GetType().GetProperties())
            {
                // "clear" only properties of type String and those that have a public setter
                if (prop.PropertyType == typeof(string) && prop.CanWrite)
                    prop.SetValue(obj, string.Empty, null); // <- "clear" value of the property 
            }
        }
    } 

use the code:

        StringCollection scol2 = new StringCollection();
        // ... do soemthing
        scol2.ClearStrings();

        SomeOtherClass obj = new SomeOtherClass();
        // ... do something
        obj.ClearStrings();

Upvotes: 0

Sinatr
Sinatr

Reputation: 21998

That's the basic OOP concept: construct object when you need it, destroy at the end. Constructing part always deals with default values, which is exactly what you need.

For managed objects (string) simply create a new instance of a class holding all data what has to be reset (cleared):

class SomeDataStorage
{
    // default is null
    public string Data1 {get; set;}

    private string _data2 = "default value";
    public string Data2 { get {return _data2;} set {_data2 = value;}}
}

Then you construct this object when you need it

foreach (string in application)
{
    var data = new SomeDataStorage(); // default values
    ...
}

It will be automagically destroyed when going out of scope (leaving { } or exiting function).

For unmanaged objects, implement IDisposable and consider to use using() { } often to auto-dispose.

You can have application-wide instance of SomeDataStorage. Simply assign a new object (construct new instance) to reset values to default.

To make it even more clear:

class App
{
    public SomeDataStorage MyData;

    public App()
    {
        Reset();
    }

    // call this when you need to init for the first time or simply reset to default
    public void Reset()
    {
        MyData = new SomeDataStorage();
    }
}

Upvotes: 1

vallabha
vallabha

Reputation: 385

I suggest to put all your strings in to a class and dispose the object if you get new data

Upvotes: 0

flindeberg
flindeberg

Reputation: 5027

Given your comments I get the feeling that you only want to clear the strings, have a look at this C# like pseudo code:

public void ClearString(IEnumerable<object> stuffToClear)
{
  // go through all the objects to clear
  foreach (var item in stuffToClear)
  {
    // get the properties to clear
    var props = from prop in item.GetType().GetProperties()
                where prop.PropertyType == typeof(string) // or another type or filter
                select prop;
    for (var p in props)
    {
      // clear it
      p.SetValue(item, string.Empty);
    }
  }
}

Not that I'm writing this in freehand, all calls will surely not be correct.

Upvotes: 2

Amorphis
Amorphis

Reputation: 398

String.Empty represents a not null empty string. If you want to clear a large amount of data (string/non string) you can encapsulate all of the variables in one class and create a Clean() method that goes through all the variables and clears them or instantiate that class when you need a fresh copy when you set the default values in the constructor.

The use of class.Empty is from what I understand to have a well defined instance of what is an empty instance.

Upvotes: 2

Related Questions