|
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class A
{
}
class B:A
{
}
class c
{
static void Main(string[] args)
{
B obj1 = new A();
}
}
}
will it execute ?? and if not can anyone give me the exact answer ??? i am new to C#..!!
|
|
|
|
|
No. It won't compile - it will complain that you cannot implicitly cast a Class A to Class B (you can explicitly, but that won't execute either.)
Think about it: if you create an instance of A and try to use it as a B what happens? Any additional elements in B do not exist in your instance of A. For example, if B has a public string called "Text" that is not in A, what happens in your code if you say
static void Main(string[] args)
{
B obj1 = new A();
Message.Show(obj1.Text);
} Where is the Text to come form - it hasn't been allocated in A!
It's like buying a loaf of bread and trying to treat it as a banana!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
|
Is Google broken? Don't have access to a book store?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Looks like an interview question.
Please format all code snippets appropriately
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Looks like an interview question.
Really?
|
|
|
|
|
I know shocking to think anyone would post such and obvious message
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Since you deleted your question, I'll reply here. (Please don't delete questions, it leaves the forum with half a conversation - that doesn't help anyone else who has a similar problem)
arindam2010 wrote: Can you please tell me little bit more about casting..??
the code works if i write like this
B obj1 = (B)new A();
what is happening actually here ??
First off, it won't work if you write it like that - it will compile, because you are using an explicit cast. But when you try to run it, you will get a run-time exception along the lines of "unable to cast from type A to type B" and your program will stop. Why? Because it would need to invent information, and it doesn't know how to do that.
When you cast from an integer to a float, it knows what to do: add "0.0" and call it a float! When you cast from a float to an integer, it knows what to do: Throw away any digits to the right of the decimal point.
When you cast from one of your classes to another, it doesn't know what is happening. It knows they are different, but it doesn't know how to make them the same.
Think of it this way: If you have a Ford Ka, and fit a Ferrari badge instead of the blue oval, will it out accelerate a Porche? No - it is still a Ford, but it looks a bit different - and won't fool anyone.
You can down cast from a derived class to the class it derived from (because everything in the source class is still there in the derived class), and you can cast that back to the derived class (because it started out as the derived class and in real terms, it still is - you just changed the name).
This is a bit complex to explain without face to face and maybe some diagrams - I would suggest you discuss this with your tutor as he/she will have explained this many times before!
[edit] Left? Right? My right hands side doesn't know whet my left hand side is doing... [/edit]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
modified on Thursday, September 16, 2010 2:50 PM
|
|
|
|
|
OriginalGriff wrote: When you cast from a float to an integer, it knows what to do: Throw away any digits to the left of the decimal point. ???
|
|
|
|
|
Can I blame Arabic numbers?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
OriginalGriff wrote: Can I blame Arabic numbers
You can blame them. It doesn't mean we necessarily believe you.
|
|
|
|
|
Pete O'Hanlon wrote: It doesn't mean we necessarily believe you
Damn. And it sounded like such a good excuse...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
You got an answer the first time you posted it in questions/answers.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hi,
Please guide me to create XML file using XSD.
Thanks in advance.
|
|
|
|
|
Here[^] you go. What does this have to do with C#?
|
|
|
|
|
Thanks for your reply. This was done manually from XSD to XML vice-versa. But I would like to convert XSD to XML file, programatically in C#.Net(not manually).
Hope you understood my problem
|
|
|
|
|
Ah. You want to autogenerate an XSD from an XML file. I see. One way to do this is to manually trigger the xsd.exe process in .NET using the xml file as an input to it.
|
|
|
|
|
Sorry for communicating wrongly. I would like to create XML from XSD. Could you let me know whats the command line in xsd.exe.
|
|
|
|
|
Do you want to create classes derived from an XSD file, and then use those classes for reading and writing XML files? Then you can have a look at my article Bernie’s Trackviewer[^] where I describe that with the example of the GPX schema.
|
|
|
|
|
Thanks for your reply.
Can you just give clarity on below things
1. Do we need to convert XSD to class files and class files to XML in the process of XSD->XML?
2. Is there any direct way to convert XSD->XML?
So far I followed Point1 and created class files from XSD using XSD.exe. I've seen your article and downloaded the source. Very nice article. Thanks for sharing.
Please let me know which class contains the code to create xml files from class files (generated from XSD). Please help as I'm new to XSD concept. Thank you..
|
|
|
|
|
XSD is a schema: it defines how the data look like. XML files contain data which fulfill the definition in the corresponding xsd file.
Consequently, I do not see the point of "converting" from xsd to xml.
You have some data and write them into an XML file, and the structure of the data is described in the XSD file.
As for the example project, have a look at the GPX class, the XML file is written by the ToFile method.
|
|
|
|
|
this is my code
private void button1_Click(object sender, EventArgs e)
{
if (newformtime.Visible == true)
{
timeToolStripMenuItem.Checked = false;
newformtime.Visible = false;
}
else if (newformtime.Visible == false)
{
timeToolStripMenuItem.Checked = true;
newformtime.Visible = true;
}
}
and
Time newformtime = new Time();
And for some reason, the window just wont open
|
|
|
|
|
Assuming newformtime is a Form you would also need to call newformtime.Show() or newformtime.ShowDialog() . Simply creating the object won't show it.
|
|
|
|
|
so
newformtime.Visible = true; doesn't work?
|
|
|
|
|
systemerror121 wrote: so
newformtime.Visible = true;
doesn't work?
On second thought.. that should work
|
|
|
|