Casting objects

Robert Byers asked on June 16, 2015 22:54

What is the proper way to cast a treenode object into an another object that inherits from a treenode object?

I have the following code but get a cast error at runtime:

public class PublicationWorkflowAction : DocumentWorkflowAction { public override void Execute() { Publication pub = (Publication) Node; }

... public partial class Publication : TreeNode ... protected TreeNode Node ... Exception type: System.InvalidCastException

Currently using Kentico 8.2.11. Thanks in advance.

Recent Answers


Roman Hutnyk answered on June 16, 2015 23:18

What is the reason you want to cast it? If this is to fetch data, I'd use node.GetValue("fieldname");

0 votesVote for this answer Mark as a Correct answer

Robert Byers answered on June 17, 2015 04:12

There are many columns on the object and I also have some methods I'd like to encapsulate in the Publication. Overall just want to do this for cleaner and easier code maintenance. Is it possible or is node.GetValue the cleanest route?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 17, 2015 13:33

@Robert take a look at this post on creating recurring events. This uses a tree node and creates a strongly typed EventDocument. Keep in mind you have to go through the effort and create the class to define the EventDocument.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.