is this a cast? What is this one line of code doing?

  • Thread starter Thread starter hazz
  • Start date Start date
H

hazz

I have inherited code but don't know exactly what this one line of code is
doing? What it is the purpose of (ClassFileName)ClassFileName.........


ClassFileName vs2 = (ClassFileName )ClassFileName
..CreateInstance(typeof(ClassFileName ),textBox1.Text);



Is it a cast? If it is, why is it? What are the mechanisms going on here?
thanks, -hazz

where ClassFileName looks like this;

[XmlRootAttribute("ClassFileName ",Namespace="http://www.xxx.com/1.0")]
public class ClassFileName : V.V.Internal.ClassFileNameBaseXmlSerializable
{
private Method m_oMethod;

public ClassFileName ()
{
m_oMethod = new Method();
}

public Method Method
{
get
{
return m_oMethod;
}
set
{
m_oMethod = value;
}
}
}
 
Hazz

yes that is a cast. it is casting to ClassFileName, and the cast operation
is being performed on whatever is being returned by the CreateInstance
method likely something of type object.

regards
roy fine


hazz said:
I have inherited code but don't know exactly what this one line of code is
doing? What it is the purpose of (ClassFileName)ClassFileName.........


ClassFileName vs2 = (ClassFileName )ClassFileName
.CreateInstance(typeof(ClassFileName ),textBox1.Text);



Is it a cast? If it is, why is it? What are the mechanisms going on here?
thanks, -hazz

where ClassFileName looks like this;

[XmlRootAttribute("ClassFileName ",Namespace="http://www.xxx.com/1.0")]
public class ClassFileName : V.V.Internal.ClassFileNameBaseXmlSerializable
{
private Method m_oMethod;

public ClassFileName ()
{
m_oMethod = new Method();
}

public Method Method
{
get
{
return m_oMethod;
}
set
{
m_oMethod = value;
}
}
}
 
Thank you Roy. Now I understand why a double would be cast to an int but why
cast it to the class (or object)? Sad to say its too subtle for me. I just
don't get it. I will try to look at it sideways and see if that
helps....-hazz

Roy Fine said:
Hazz

yes that is a cast. it is casting to ClassFileName, and the cast operation
is being performed on whatever is being returned by the CreateInstance
method likely something of type object.

regards
roy fine


hazz said:
I have inherited code but don't know exactly what this one line of code is
doing? What it is the purpose of (ClassFileName)ClassFileName.........


ClassFileName vs2 = (ClassFileName )ClassFileName
.CreateInstance(typeof(ClassFileName ),textBox1.Text);



Is it a cast? If it is, why is it? What are the mechanisms going on here?
thanks, -hazz

where ClassFileName looks like this;

[XmlRootAttribute("ClassFileName ",Namespace="http://www.xxx.com/1.0")]
public class ClassFileName : V.V.Internal.ClassFileNameBaseXmlSerializable
{
private Method m_oMethod;

public ClassFileName ()
{
m_oMethod = new Method();
}

public Method Method
{
get
{
return m_oMethod;
}
set
{
m_oMethod = value;
}
}
}
 
hazz said:
Thank you Roy. Now I understand why a double would be cast to an int but why
cast it to the class (or object)? Sad to say its too subtle for me. I just
don't get it. I will try to look at it sideways and see if that

If something is declared to return "object" but you want to use it as
something more specific, you have to cast it to tell the compiler that
you know more about it. For instance:

ArrayList al = new ArrayList();
al.Add ("hello");
object o = al[0]; // Fine
string s = al[0]; // Not fine - the compiler doesn't know that al[0] is
// a string

string s = (string)al[0]; // Fine - the compiler will now insert a
// runtime check that al[0] really is a
// string, and then the value of the
// reference can safely be assigned to s
 
why cast it to the class (or object)?

My guess is that the return type of the CreateInstance method is not
ClassFileName, right?

So the types are different and we need to do a type conversion.

Some conversions happen automatically (implicit conversions) and some we
have to ask for with a cast (explicit conversions).

In this case, the method probably returns type object and we need to convert
to ClassFileName. This conversion is moving down the inheritance hierarchy
from object down to ClassFileName. These "down" conversions are explicit, i.e
they require the cast. The reason is that they might fail at runtime since an
object reference is very flexible and might refer to anything. To make sure
we are aware of this risk of runtime failure, the compiler requires that we
use an explicit cast.

Keep in mind that the type of the "thing" does not change when you cast.
Here we are just converting the type of the reference. The instance is a
ClassFileName and will always be a ClassFileName. The method likely returns
an object reference to a ClassFileName instance and you need to cast to get
the specific reference, i.e. a ClassFileName reference to a ClassFileName
instance.
 
Back
Top