Hi there,
both ways works without any error, but only this way shows the property value: Console.WriteLine("wight: " + ((Porsche)A).wight);
and this with ',' instead of '+' does not:
Console.WriteLine("wight: ", ((Porsche)A).wight);
I made a C# console app with VS.NET 2012.
It has three clases.
Class Auto is the base class.
Class Porsche the subclase.
So, within those program part of this console app, I just have like to see some proofs, about changing of objectreferences.
Only Porsche has a property 'wight', it will set by construktor.
Then there is a class called AnyObject, which has just one method and this method excpect a 'Auto' type reference.
The methode will make a proof what type is it, and if it is Porsche then there shall be written 'wight = 10'.
But this happend only if I put it like that:
Console.WriteLine("wight: " + ((Porsche)A).wight);
So if I take look to those writeline methode, what its excpect while using ',' then there is "object arg0".
Why is it loosing the value of the property?
here the whole code:
// derivade clase Porsche
// this clase just take any reference of Auto types
// here we have program part of this app, where the class AnyObject is used
both ways works without any error, but only this way shows the property value: Console.WriteLine("wight: " + ((Porsche)A).wight);
and this with ',' instead of '+' does not:
Console.WriteLine("wight: ", ((Porsche)A).wight);
I made a C# console app with VS.NET 2012.
It has three clases.
Class Auto is the base class.
Class Porsche the subclase.
So, within those program part of this console app, I just have like to see some proofs, about changing of objectreferences.
Only Porsche has a property 'wight', it will set by construktor.
Then there is a class called AnyObject, which has just one method and this method excpect a 'Auto' type reference.
The methode will make a proof what type is it, and if it is Porsche then there shall be written 'wight = 10'.
But this happend only if I put it like that:
Console.WriteLine("wight: " + ((Porsche)A).wight);
So if I take look to those writeline methode, what its excpect while using ',' then there is "object arg0".
Why is it loosing the value of the property?
here the whole code:
Code:
[COLOR=Green]// Base clase Auto [/COLOR]
public class Auto
{ }
// derivade clase Porsche
Code:
public class Porsche: Auto
{
public int wight { get; set; }
public Porsche()[COLOR=Green] //Construktor [/COLOR]
{ this.wight = 10; }
}
// this clase just take any reference of Auto types
Code:
public class AnyObject
{
public void anyTask(Auto A)
{
if (A != null)
{
if (A is Auto)
{
Console.WriteLine("wight: " + ((Porsche)A).wight);
}
else
Console.WriteLine("unknown type.");
}
}
}
// here we have program part of this app, where the class AnyObject is used
Code:
class Program
{
static void Main(string[] args)
{
Porsche p = new Porsche();
Auto a = p;
[COLOR=Green] // base clase gets a referenc of a subclase[/COLOR]
AnyObject myOb = new AnyObject();
myOb.anyTask(p);
Console.ReadLine();
}
}
Last edited: