Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do you cast an object to a class? This does not work?

C#
object[] objs1 = (object[])(e.AddedItems);
           //send event with data arg to other module
           Class[] objs = (class[])(objs1);
Posted
Comments
Bernhard Hiller 19-Apr-13 2:58am    
Better show us the types you actually use instead of object[] and Class[]/class[]. Also be careful that names are case-sensitive - Myclass is not MyClass!
Also indicate the version of the .net framework.

This code is total gibberish. Nothing can help you unless you start learning from the very beginning.

You need to know that the keyword "object" is the alias of System.Object, which is the type, but keyword class does not denote any certain type.

The whole question "how to cast object to class" makes no sense, because you casting means representing some object of one type as it was the object of another type. The result of the cast is the same object, not class, but the result can be assigned to a variable (member) of the target type. It looks like you know the syntax, but the expression in '()' should be a name of some certain type. The case can be successful or not, so you may want to use dynamic cast instead, read on operator as.

Sorry, but I cannot help you to learn programming from the very beginning, and I cannot answer your question which does not make sense. I only can mention what's involved.

—SA
 
Share this answer
 
myClassA X = new myClassA();
myClassB Y = (myClassB) X;
 
Share this answer
 
v2

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