473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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;
}
}
}
Nov 16 '05 #1
4 1724
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" <ha**@sonic.net> wrote in message
news:et**************@TK2MSFTNGP11.phx.gbl...
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;
}
}
}

Nov 16 '05 #2
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" <rl****@twt.obfuscate.net> wrote in message
news:OV**************@TK2MSFTNGP09.phx.gbl...
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" <ha**@sonic.net> wrote in message
news:et**************@TK2MSFTNGP11.phx.gbl...
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;
}
}
}


Nov 16 '05 #3
hazz <ha**@sonic.net> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
> 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.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Arjen | last post by:
Hello, Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
11
by: Roman Mashak | last post by:
Hello, All! I'm implementing function parsing config file, which look like this: # this is comment port=10000 path=/var/run/dump.pid .... Declared the type
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
18
by: Felix Kater | last post by:
I haven't been thinking about it for years but recently I've stumbled on the fact that 'casting' is actually doing (at least) two different things: On the one hand 'casting' means: 'Change...
5
by: johnbrown105 | last post by:
Hello All, I did try searching the archives, but the topics seemed to be mostly about base and derived classes. I have reached Chapter 8 in Bruce Eckel's "Thinking in C++ 2nd Edition Vol....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.