-1

My class has a bunch of nullable double properties. At run time some of them have 0 value and i am going to set them null before sending action. I know that we can use a foreach statement to iterate through a collection which has been placed inside a class so i hope use the same technique for this problem. As i said in this case i am not working with a collection so Implementing the IEnumerable is a kind of meaningless idea. Is there any way to move among the members of class ?

I have tried this

Class1 c=new Class1(){Age = 12,Family = "JR",Name = "MAX"};
foreach (string member in c)
{
    Console.WriteLine(member);
}
Console.ReadKey();

Implementing the IEnumerable

public IEnumerator GetEnumerator()
{
    // ?!
}
Yoav
  • 3,326
  • 3
  • 32
  • 73
Mahmood Shahrokni
  • 226
  • 1
  • 3
  • 12
  • 1
    The code you've demonstrated doesn't seem to reflect the situation you've described - there are no double properties at all. It would help if you could give a more concrete example... in particular, it's odd to want to iterate over a set of properties which *don't* logically form a collection. You *may* want to either use reflection, or write a property which simply returns each of those other properties in turn, but it's hard to say with so little information. – Jon Skeet Feb 02 '16 at 07:03
  • Dear @JonSkeet the code which you have seen is an simple example – Mahmood Shahrokni Feb 02 '16 at 07:09
  • It's a simple example which doesn't demonstrate the scenario you've described, and which therefore isn't particularly useful. Why would you describe one thing and then give an example of something different? Please read http://tinyurl.com/stack-hints – Jon Skeet Feb 02 '16 at 07:15

3 Answers3

2

You have to use Reflection, please look at

How to get the list of properties of a class?

I added a new double? property at your class.

    class Class1
    {
        public int Age { get; set; }
        public string Family { get; set; }
        public string Name { get; set; }

        public double? d { get; set; }
    }

    [Test]
    public void MyTest()
    {
            Class1 c = new Class1() { Age = 12, Family = "JR", Name = "MAX" };
            foreach (var prop in c.GetType().GetProperties().Where(x => x.PropertyType == typeof(double?)))
            {
                Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(c));
                prop.SetValue(c, (double?)null); // set null as you wanted
            }
    }
Community
  • 1
  • 1
Alex Vazhev
  • 1,363
  • 1
  • 18
  • 17
2

You can use Reflection and Linq for this

using System.Reflection;
...

private static void ApplyNullsForZeroes(Object value) {
  if (null == value)
    return; // Or throw exception 

  var props = value.GetType()
    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
    .Where(p => p.CanRead && p.CanWrite)
    .Where(p => p.PropertyType == typeof(Nullable<Double>));

  foreach (var p in props)
    if (Object.Equals(p.GetValue(value), 0.0))
      p.SetValue(value, null);
}

Test

public class MyClass {
  public MyClass() {
    Value = 0.0;
  }

  public Double? Value {
    get;
    set;
  }
}

...

MyClass test = new MyClass();

ApplyNullsForZeroes(test);

if (test.Value == null)
  Console.Write("It's null now");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

As you said you have properties, so why not use them as a Property?

Use condition in get. Return null while getting the value of property:

get
{
    return Age == 0 ? null : Age; //supposing "Age" is double in case to show an example
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • No I am not. Does it have to do anything with my answer? You didn't explain any scenario where you would need `0`. You just explained the `null` scenario. Please add more information to the question to help you better. – Shaharyar Feb 02 '16 at 07:10
  • @T.shaped: Why are you being beligerent to someone who is trying to help you, but finding it difficult because you haven't given a clear enough example *or* described clearly enough what you're trying to achieve? Hint: if people are misunderstanding what you're trying to do, that's more likely to be a reflection on your question than on the location of the people... – Jon Skeet Feb 02 '16 at 07:17