Home All Groups Group Topic Archive Search About

How to handle this exception?

Author
16 Mar 2007 2:53 PM
Andrew

Hello, friends,

In the following C# code, I tried to get the values for each property in an
object:

public void WriteObjectValue(object obj)
{
            Type objectType = obj.GetType();
            PropertyInfo[] properties = objectType.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                 System.Object propertyValue = property.GetValue(obj, null);
                 Console.WriteLine(propertyValue.ToString());
            }
}

This worked fine, except in the following situation:

If the passed object obj was defined like the follows

Public class IntObjClass
{
    int? deptId;
    int? sectionId;

    public int DeptId {set {deptId = value;}}
    public int SectioinId {set {sectionId = value;}}

}

and only sectionId was assigned a value, say 100, while deptId was left null.

At this situation, the foreach loop will throw an exception at
                  System.Object propertyValue = property.GetValue(),
complaining deptId was null.

Is there a way to get around this? (I don't want to use try{} catch {} to
handle this exception)

Thanks a lot for your help.
Author
16 Mar 2007 3:26 PM
Henning Krause [MVP - Exchange]
Hello,

Have you tried this?

if (propertyValue == null) {
  Console.WriteLine("<null>");
}
else
{
Console.WriteLine(propertyValue.ToString());
}

Best regards,
Henning Krause

Show quoteHide quote
"Andrew" <And***@discussions.microsoft.com> wrote in message
news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
> Hello, friends,
>
> In the following C# code, I tried to get the values for each property in
> an
> object:
>
> public void WriteObjectValue(object obj)
> {
>            Type objectType = obj.GetType();
>            PropertyInfo[] properties = objectType.GetProperties();
>
>            foreach (PropertyInfo property in properties)
>            {
>                 System.Object propertyValue = property.GetValue(obj,
> null);
>                 Console.WriteLine(propertyValue.ToString());
>            }
> }
>
> This worked fine, except in the following situation:
>
> If the passed object obj was defined like the follows
>
> Public class IntObjClass
> {
>    int? deptId;
>    int? sectionId;
>
>    public int DeptId {set {deptId = value;}}
>    public int SectioinId {set {sectionId = value;}}
>
> }
>
> and only sectionId was assigned a value, say 100, while deptId was left
> null.
>
> At this situation, the foreach loop will throw an exception at
>                  System.Object propertyValue = property.GetValue(),
> complaining deptId was null.
>
> Is there a way to get around this? (I don't want to use try{} catch {} to
> handle this exception)
>
> Thanks a lot for your help.
Are all your drivers up to date? click for free checkup

Author
16 Mar 2007 4:13 PM
Andrew
It was the

    propertyValue = property.GetValue(obj, null);

throw the exception (Nullable object must have a value) before I had a
chance to check if properValue was null or not.

Any other ideas? Thanks.

Show quoteHide quote
"Henning Krause [MVP - Exchange]" wrote:

> Hello,
>
> Have you tried this?
>
> if (propertyValue == null) {
>   Console.WriteLine("<null>");
> }
> else
> {
>  Console.WriteLine(propertyValue.ToString());
> }
>
> Best regards,
> Henning Krause
>
> "Andrew" <And***@discussions.microsoft.com> wrote in message
> news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
> > Hello, friends,
> >
> > In the following C# code, I tried to get the values for each property in
> > an
> > object:
> >
> > public void WriteObjectValue(object obj)
> > {
> >            Type objectType = obj.GetType();
> >            PropertyInfo[] properties = objectType.GetProperties();
> >
> >            foreach (PropertyInfo property in properties)
> >            {
> >                 System.Object propertyValue = property.GetValue(obj,
> > null);
> >                 Console.WriteLine(propertyValue.ToString());
> >            }
> > }
> >
> > This worked fine, except in the following situation:
> >
> > If the passed object obj was defined like the follows
> >
> > Public class IntObjClass
> > {
> >    int? deptId;
> >    int? sectionId;
> >
> >    public int DeptId {set {deptId = value;}}
> >    public int SectioinId {set {sectionId = value;}}
> >
> > }
> >
> > and only sectionId was assigned a value, say 100, while deptId was left
> > null.
> >
> > At this situation, the foreach loop will throw an exception at
> >                  System.Object propertyValue = property.GetValue(),
> > complaining deptId was null.
> >
> > Is there a way to get around this? (I don't want to use try{} catch {} to
> > handle this exception)
> >
> > Thanks a lot for your help.
>
>
Author
16 Mar 2007 5:08 PM
VJ
more like

if (property == null) {

}
else
{

}

should work

VJ

Show quoteHide quote
"Andrew" <And***@discussions.microsoft.com> wrote in message
news:200E4804-4C1E-4D2E-93F1-216840BFD36C@microsoft.com...
> It was the
>
>    propertyValue = property.GetValue(obj, null);
>
> throw the exception (Nullable object must have a value) before I had a
> chance to check if properValue was null or not.
>
> Any other ideas? Thanks.
>
> "Henning Krause [MVP - Exchange]" wrote:
>
>> Hello,
>>
>> Have you tried this?
>>
>> if (propertyValue == null) {
>>   Console.WriteLine("<null>");
>> }
>> else
>> {
>>  Console.WriteLine(propertyValue.ToString());
>> }
>>
>> Best regards,
>> Henning Krause
>>
>> "Andrew" <And***@discussions.microsoft.com> wrote in message
>> news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
>> > Hello, friends,
>> >
>> > In the following C# code, I tried to get the values for each property
>> > in
>> > an
>> > object:
>> >
>> > public void WriteObjectValue(object obj)
>> > {
>> >            Type objectType = obj.GetType();
>> >            PropertyInfo[] properties = objectType.GetProperties();
>> >
>> >            foreach (PropertyInfo property in properties)
>> >            {
>> >                 System.Object propertyValue = property.GetValue(obj,
>> > null);
>> >                 Console.WriteLine(propertyValue.ToString());
>> >            }
>> > }
>> >
>> > This worked fine, except in the following situation:
>> >
>> > If the passed object obj was defined like the follows
>> >
>> > Public class IntObjClass
>> > {
>> >    int? deptId;
>> >    int? sectionId;
>> >
>> >    public int DeptId {set {deptId = value;}}
>> >    public int SectioinId {set {sectionId = value;}}
>> >
>> > }
>> >
>> > and only sectionId was assigned a value, say 100, while deptId was left
>> > null.
>> >
>> > At this situation, the foreach loop will throw an exception at
>> >                  System.Object propertyValue = property.GetValue(),
>> > complaining deptId was null.
>> >
>> > Is there a way to get around this? (I don't want to use try{} catch {}
>> > to
>> > handle this exception)
>> >
>> > Thanks a lot for your help.
>>
>>
Author
16 Mar 2007 6:01 PM
Andrew
property is not null. It is a valid instantiated object.

Show quoteHide quote
"VJ" wrote:

> more like
>
> if (property == null) {
>
> }
> else
> {
>
> }
>
> should work
>
> VJ
>
> "Andrew" <And***@discussions.microsoft.com> wrote in message
> news:200E4804-4C1E-4D2E-93F1-216840BFD36C@microsoft.com...
> > It was the
> >
> >    propertyValue = property.GetValue(obj, null);
> >
> > throw the exception (Nullable object must have a value) before I had a
> > chance to check if properValue was null or not.
> >
> > Any other ideas? Thanks.
> >
> > "Henning Krause [MVP - Exchange]" wrote:
> >
> >> Hello,
> >>
> >> Have you tried this?
> >>
> >> if (propertyValue == null) {
> >>   Console.WriteLine("<null>");
> >> }
> >> else
> >> {
> >>  Console.WriteLine(propertyValue.ToString());
> >> }
> >>
> >> Best regards,
> >> Henning Krause
> >>
> >> "Andrew" <And***@discussions.microsoft.com> wrote in message
> >> news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
> >> > Hello, friends,
> >> >
> >> > In the following C# code, I tried to get the values for each property
> >> > in
> >> > an
> >> > object:
> >> >
> >> > public void WriteObjectValue(object obj)
> >> > {
> >> >            Type objectType = obj.GetType();
> >> >            PropertyInfo[] properties = objectType.GetProperties();
> >> >
> >> >            foreach (PropertyInfo property in properties)
> >> >            {
> >> >                 System.Object propertyValue = property.GetValue(obj,
> >> > null);
> >> >                 Console.WriteLine(propertyValue.ToString());
> >> >            }
> >> > }
> >> >
> >> > This worked fine, except in the following situation:
> >> >
> >> > If the passed object obj was defined like the follows
> >> >
> >> > Public class IntObjClass
> >> > {
> >> >    int? deptId;
> >> >    int? sectionId;
> >> >
> >> >    public int DeptId {set {deptId = value;}}
> >> >    public int SectioinId {set {sectionId = value;}}
> >> >
> >> > }
> >> >
> >> > and only sectionId was assigned a value, say 100, while deptId was left
> >> > null.
> >> >
> >> > At this situation, the foreach loop will throw an exception at
> >> >                  System.Object propertyValue = property.GetValue(),
> >> > complaining deptId was null.
> >> >
> >> > Is there a way to get around this? (I don't want to use try{} catch {}
> >> > to
> >> > handle this exception)
> >> >
> >> > Thanks a lot for your help.
> >>
> >>
>
>
>
Author
16 Mar 2007 6:57 PM
VJ
Ok Andrew, I think I know the problem.. sorry about confusion. You cannot do
getValue on a property with Set only attributes. It wont work. How you will
know a property is read or write is like below

property.CanRead
or
property.CanWrite

HTH
VJ

Show quoteHide quote
"Andrew" <And***@discussions.microsoft.com> wrote in message
news:7BC599C5-02D5-484A-B7F0-F5B6FF624692@microsoft.com...
> property is not null. It is a valid instantiated object.
>
> "VJ" wrote:
>
>> more like
>>
>> if (property == null) {
>>
>> }
>> else
>> {
>>
>> }
>>
>> should work
>>
>> VJ
>>
>> "Andrew" <And***@discussions.microsoft.com> wrote in message
>> news:200E4804-4C1E-4D2E-93F1-216840BFD36C@microsoft.com...
>> > It was the
>> >
>> >    propertyValue = property.GetValue(obj, null);
>> >
>> > throw the exception (Nullable object must have a value) before I had a
>> > chance to check if properValue was null or not.
>> >
>> > Any other ideas? Thanks.
>> >
>> > "Henning Krause [MVP - Exchange]" wrote:
>> >
>> >> Hello,
>> >>
>> >> Have you tried this?
>> >>
>> >> if (propertyValue == null) {
>> >>   Console.WriteLine("<null>");
>> >> }
>> >> else
>> >> {
>> >>  Console.WriteLine(propertyValue.ToString());
>> >> }
>> >>
>> >> Best regards,
>> >> Henning Krause
>> >>
>> >> "Andrew" <And***@discussions.microsoft.com> wrote in message
>> >> news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
>> >> > Hello, friends,
>> >> >
>> >> > In the following C# code, I tried to get the values for each
>> >> > property
>> >> > in
>> >> > an
>> >> > object:
>> >> >
>> >> > public void WriteObjectValue(object obj)
>> >> > {
>> >> >            Type objectType = obj.GetType();
>> >> >            PropertyInfo[] properties = objectType.GetProperties();
>> >> >
>> >> >            foreach (PropertyInfo property in properties)
>> >> >            {
>> >> >                 System.Object propertyValue = property.GetValue(obj,
>> >> > null);
>> >> >                 Console.WriteLine(propertyValue.ToString());
>> >> >            }
>> >> > }
>> >> >
>> >> > This worked fine, except in the following situation:
>> >> >
>> >> > If the passed object obj was defined like the follows
>> >> >
>> >> > Public class IntObjClass
>> >> > {
>> >> >    int? deptId;
>> >> >    int? sectionId;
>> >> >
>> >> >    public int DeptId {set {deptId = value;}}
>> >> >    public int SectioinId {set {sectionId = value;}}
>> >> >
>> >> > }
>> >> >
>> >> > and only sectionId was assigned a value, say 100, while deptId was
>> >> > left
>> >> > null.
>> >> >
>> >> > At this situation, the foreach loop will throw an exception at
>> >> >                  System.Object propertyValue = property.GetValue(),
>> >> > complaining deptId was null.
>> >> >
>> >> > Is there a way to get around this? (I don't want to use try{} catch
>> >> > {}
>> >> > to
>> >> > handle this exception)
>> >> >
>> >> > Thanks a lot for your help.
>> >>
>> >>
>>
>>
>>
Author
16 Mar 2007 7:42 PM
Andrew
sorry, it actually had {get {return ****;}} definitions....

so, ReadOnly was not the reason

Show quoteHide quote
"VJ" wrote:

> Ok Andrew, I think I know the problem.. sorry about confusion. You cannot do
> getValue on a property with Set only attributes. It wont work. How you will
> know a property is read or write is like below
>
> property.CanRead
> or
> property.CanWrite
>
> HTH
> VJ
>
> "Andrew" <And***@discussions.microsoft.com> wrote in message
> news:7BC599C5-02D5-484A-B7F0-F5B6FF624692@microsoft.com...
> > property is not null. It is a valid instantiated object.
> >
> > "VJ" wrote:
> >
> >> more like
> >>
> >> if (property == null) {
> >>
> >> }
> >> else
> >> {
> >>
> >> }
> >>
> >> should work
> >>
> >> VJ
> >>
> >> "Andrew" <And***@discussions.microsoft.com> wrote in message
> >> news:200E4804-4C1E-4D2E-93F1-216840BFD36C@microsoft.com...
> >> > It was the
> >> >
> >> >    propertyValue = property.GetValue(obj, null);
> >> >
> >> > throw the exception (Nullable object must have a value) before I had a
> >> > chance to check if properValue was null or not.
> >> >
> >> > Any other ideas? Thanks.
> >> >
> >> > "Henning Krause [MVP - Exchange]" wrote:
> >> >
> >> >> Hello,
> >> >>
> >> >> Have you tried this?
> >> >>
> >> >> if (propertyValue == null) {
> >> >>   Console.WriteLine("<null>");
> >> >> }
> >> >> else
> >> >> {
> >> >>  Console.WriteLine(propertyValue.ToString());
> >> >> }
> >> >>
> >> >> Best regards,
> >> >> Henning Krause
> >> >>
> >> >> "Andrew" <And***@discussions.microsoft.com> wrote in message
> >> >> news:28BFF904-952F-4194-8987-7C7D6D5DD246@microsoft.com...
> >> >> > Hello, friends,
> >> >> >
> >> >> > In the following C# code, I tried to get the values for each
> >> >> > property
> >> >> > in
> >> >> > an
> >> >> > object:
> >> >> >
> >> >> > public void WriteObjectValue(object obj)
> >> >> > {
> >> >> >            Type objectType = obj.GetType();
> >> >> >            PropertyInfo[] properties = objectType.GetProperties();
> >> >> >
> >> >> >            foreach (PropertyInfo property in properties)
> >> >> >            {
> >> >> >                 System.Object propertyValue = property.GetValue(obj,
> >> >> > null);
> >> >> >                 Console.WriteLine(propertyValue.ToString());
> >> >> >            }
> >> >> > }
> >> >> >
> >> >> > This worked fine, except in the following situation:
> >> >> >
> >> >> > If the passed object obj was defined like the follows
> >> >> >
> >> >> > Public class IntObjClass
> >> >> > {
> >> >> >    int? deptId;
> >> >> >    int? sectionId;
> >> >> >
> >> >> >    public int DeptId {set {deptId = value;}}
> >> >> >    public int SectioinId {set {sectionId = value;}}
> >> >> >
> >> >> > }
> >> >> >
> >> >> > and only sectionId was assigned a value, say 100, while deptId was
> >> >> > left
> >> >> > null.
> >> >> >
> >> >> > At this situation, the foreach loop will throw an exception at
> >> >> >                  System.Object propertyValue = property.GetValue(),
> >> >> > complaining deptId was null.
> >> >> >
> >> >> > Is there a way to get around this? (I don't want to use try{} catch
> >> >> > {}
> >> >> > to
> >> >> > handle this exception)
> >> >> >
> >> >> > Thanks a lot for your help.
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>
Author
16 Mar 2007 8:48 PM
Jon Skeet [C# MVP]
Andrew <And***@discussions.microsoft.com> wrote:
> sorry, it actually had {get {return ****;}} definitions....
>
> so, ReadOnly was not the reason

In that case, it's rather hard for us to know what's wrong.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bookmark and Share