|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to handle this exception?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. 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. 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. > > 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. >> >> 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. > >> > >> > > > 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. >> >> >> >> >> >> >> 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. > >> >> > >> >> > >> > >> > >> > > > Andrew <And***@discussions.microsoft.com> wrote:
> sorry, it actually had {get {return ****;}} definitions.... In that case, it's rather hard for us to know what's wrong.> > so, ReadOnly was not the reason 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
Other interesting topics
how to control UDP sending Speed?
BLOB problem warning in XAML Field Truncation in a DataTable System.Security.SecurityException: System.Security.Permissions.SecurityPermission Loading from GAC without all information IsolatedStorage and cleaning up No Admin access Small problem with storing connection strings what encoding does system.xml.xmldocument.save(string path) use to save the xml document if there is |
|||||||||||||||||||||||