0

dVery confusing on how Umbraco did this, and I'm looking for some sort of explanation if possible. I'm using Umbraco 7. I have a custom property on a doc type that is a True/False (basically a check box).

I'm receiving an error when attempting to parse the GetPropertyValue, object not reference to an object, which is the True/False checkbox.

I do NOT get the error when parsing the 1 INTO an object.

Code:

var topics = topicDocType.Select(x => new ForumModel
             {
                   Topic = x.Name,
                   TopicId = x.Id,
/*no error*/       IsClosedForQuestions = x.GetPropertyValue("closedQuestions") == (object)1 ? true : false,
                   Questions = x.Descendants().Where(y => y.DocumentTypeAlias.Equals("Question")).Select(y => new Question
                   {
                        QuestionName = y.GetPropertyValue("question").ToString(),
                        QuestionId = y.Id,
                        QuestionDateTime = y.CreateDate,
                        AskedBy = y.GetPropertyValue("askedBy").ToString(),
   /*no error*/         IsClosedForPosts = y.GetPropertyValue("closedPosts") == (object)1 ? true : false,
                        Posts = y.Descendants().Where(z => z.DocumentTypeAlias.Equals("Post")).Select(z => new Post
                    .... more code here

This doesn't work, and I do not get why:

var topics = topicDocType.Select(x => new ForumModel
             {
                   Topic = x.Name,
                   TopicId = x.Id,
/*doesn't work*/   IsClosedForQuestions = int.Parse(x.GetPropertyValue("closedQuestions").ToString()) == 1 ? true : false,
                    .... more code here

This doesn't work either:

var topics = topicDocType.Select(x => new ForumModel
             {
                   Topic = x.Name,
                   TopicId = x.Id,
/*doesn't work*/   IsClosedForQuestions = x.GetPropertyValue("closedQuestions").ToString() == "1" ? true : false,
                    .... more code here

If I create a test variable

var test = new ContentService().GetById(2269).GetValue("closedQuestions");

The value of test is 0, b/c the check box isn't checked. If I check it, run it again, the value is 1.

This could just be a C# question that perhaps I knew nothing about, and nothing really to do w/ Umbraco, but I know if I .ToString() an object and attempt to check if the value is equal to another string or not, it works.

Appreciate it.


EDIT:

After reading what Umbraco saves true/false as an int. True/False built-in-prop

True/False is a simple checkbox which saves either 0 or 1, depending on the checkbox being checked or not.

I also looked up how to parse correctly from an object(int) Better way to cast object int

However, this is still not working

IsClosedForQuestions = (int)x.GetPropertyValue("closedQuestions") == 1 ? true : false,

EDIT 2:

This DOES work, with retrieving the correct value. Any answers as to why?

IsClosedForQuestions = x.GetPropertyValue<int>("closedQuestions") == 1 ? true : false,

To show you that it's not null:

enter image description here

After change to int.Parse(....).ToString()) == 1

enter image description here

Community
  • 1
  • 1
Rob Scott
  • 7,921
  • 5
  • 38
  • 63

2 Answers2

1

It seems that x.GetPropertyValue("closedQuestions") is returning null in some cases (or maybe an int? with a null value), which is why you're getting that error when trying to call ToString() on it. If x.GetPropertyValue<int>("closedQuestions") works for you then that's great, but I wanted to point out one thing:

You should not compare value-types (like int) by casting them to object and using ==. Casting to object will box the value type and use referential equality which will fail:

object o1 = 1;
object o2 = 1;
Console.WriteLine(o1 == o2);  // will print `false`

You could instead use object.Equals():

IsClosedForQuestions = object.Equals(x.GetPropertyValue("closedQuestions"),1)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

You can also parse it is x.GetPropertyValue<bool>("propName"), which might make it a bit easier for comparison and I've never had this method give me an issue.

Also, it's important to note that when you do getPropertyValue("propName"), it's an object with no typing, which can cause issues with comparisons. It's just best practice to type the prop out and avoid these issues.