473,563 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

..CreateInstanc e(typeof(ClassF ileName ),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;

[XmlRootAttribut e("ClassFileNam e ",Namespace="ht tp://www.xxx.com/1.0")]
public class ClassFileName : V.V.Internal.Cl assFileNameBase XmlSerializable
{
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 1738
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******** ******@TK2MSFTN GP11.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(ClassFi leName ),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;

[XmlRootAttribut e("ClassFileNam e ",Namespace="ht tp://www.xxx.com/1.0")]
public class ClassFileName :

V.V.Internal.Cl assFileNameBase XmlSerializable {
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.obf uscate.net> wrote in message
news:OV******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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(ClassFi leName ),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;

[XmlRootAttribut e("ClassFileNam e ",Namespace="ht tp://www.xxx.com/1.0")]
public class ClassFileName :

V.V.Internal.Cl assFileNameBase XmlSerializable
{
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.co m>
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
1777
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 the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Line 176. Source Error:
5
3405
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to...
8
1787
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 along the lines of did you check this, have you did this, or this is the wrong way of doing this. I can access the CColPositions and the CPosition...
3
2270
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 DataSet ' Create connection Dim cn As New OleDbConnection With cn .connectionstring =
1
4861
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 specifiying my code: The Code used in Visual Studio: Function GetRank(ByVal ID As Integer, ByVal Comp As String, ByVal Sec As String, ByVal...
11
3872
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
3541
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 I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then...
18
2192
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 something into somthing else'. Example: double d=9.99; long l=(double)d; On the other hand 'casting' means: 'Treating something as something else...
5
1887
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. 1". Question 28 says:
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.