Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have some integer properties in my class, i am instantiate the class and set some property to some value and I am not setting some property but its taking default value to 0,
so I don't want that not setted properties,
so how to check property is set or not?
can any one suggest me?

thanks in advance
sushil

What I have tried:

class Class1
{
public int SomePropery1{get; set}
public int SomeMoreProp{get; set;}
}

class1 c1 = new class1()
c1.SomePropery1 = 1;
Posted
Updated 3-Sep-16 19:34pm
v2

 
Share this answer
 
Comments
Member 11859517 2-Sep-16 6:52am    
do you know nullable added in which .net framework?
Mehdi Gholam 2-Sep-16 6:56am    
Since .net v2.0
Member 11859517 2-Sep-16 7:01am    
thanks Mehdi,

but i have more than 50 prop. is there any generic way to do this?
Mehdi Gholam 2-Sep-16 7:15am    
Find and replace with a ? at the end.
Member 11859517 6-Sep-16 2:08am    
hi mehdi,
as u told use nullable
i have more propery, int,bool,string, long, ...
and i anm doing somthing like this

Dictionary<string, object=""> dic = new Dictionary<string, object="">();
foreach (PropertyInfo pi in myObject.GetType().GetProperties())
{
if (pi.PropertyType == typeof(string))
{
....here i want to check property is set or not ....How?
string value = (string)pi.GetValue(myObject, null);
if (!string.IsNullOrEmpty(value))
{
dic.Add(pi.Name, value);
}
}
if (pi.PropertyType == typeof(int?))
{
....here i want to check property is set or not ....how?
int value = (int)pi.GetValue(myObject, null);
if (value != null)
dic.Add(pi.Name, value);
}
}
in my class more properties, I am setting only 2 5 prop. T want to check property is set or no,,,
If these Integer Properties have some consistent regular feature, and you don't want to use the Nullable Int Type, as Mehdi suggested, you could do something like this
C#
public class YourClass
{
    public const Int32 INTNOTSET = Int32.MinValue;

    public int Int1 { set; get; }

    public YourClass()
    {
        Int1 = INTNOTSET;
    }
}
Then you would test if the property is initialized like this:
YourClass yourClass = new YourClass();

if (yourClass.Int1 == YourClass.INTNOTSET)
{
   // Int1 uninitialized 
}
else
{
   // Int1 value set  
}
Note that defining INTNOTSET as a Constant in 'YourClass results in it being a static Field of the Class which must be accessed via the reference to the Class itself.

Without knowing more about what your App is and what it does, and the shared aspects of your Int variables, and their uses (if any), evaluating the relevance of any technique is crystal-ball gazing :) However, if your Int Properties are there to hold values returned from querying databases, then, you probably do want to use Nullable Int.

Use of Nullable Types does not cause boxing and un-boxing, but does add some overhead, but so would the technique shown here, or any other technique.

Depending on the context, in one scenario using a Dictionary<string,int> might be useful, and so forth.
 
Share this answer
 
v3
Comments
Member 11859517 6-Sep-16 0:46am    
thanks BillWoodruff
Member 11859517 6-Sep-16 1:01am    
i have more propery, int,bool,string, long, ...
and i anm doing somthing like this

Dictionary<string, object=""> dic = new Dictionary<string, object="">();
foreach (PropertyInfo pi in myObject.GetType().GetProperties())
{
if (pi.PropertyType == typeof(string))
{
....here i want to check property is set or not ....
string value = (string)pi.GetValue(myObject, null);
if (!string.IsNullOrEmpty(value))
{
dic.Add(pi.Name, value);
}
}
if (pi.PropertyType == typeof(int?))
{
....here i want to check property is set or not ....
int value = (int)pi.GetValue(myObject, null);
if (value != null)
dic.Add(pi.Name, value);
}
}
in my class more properties, I am setting only 2 5 prop. T want to check property is set or no,,,

plz have some idea..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900