473,387 Members | 1,493 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,387 software developers and data experts.

Whats wrong with this code

Hi all,

Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     class I
  4.     {
  5.         public void PrintIt()
  6.         {
  7.             PrintOne();
  8.  
  9.             ((I)this).PrintOne();
  10.         }
  11.  
  12.         public virtual void PrintOne()
  13.         {
  14.             Console.WriteLine("I am from base.\n");
  15.         }
  16.     }
  17.  
  18.     class G:I
  19.     {
  20.         public override void PrintOne()
  21.         {
  22.             Console.WriteLine("I am from sub.\n");
  23.         }
  24.     }
  25.  
  26.     class Program
  27.     {
  28.         static void Main(string[] args)
  29.         {
  30.             G g = new G();
  31.             g.PrintIt();
  32.             Console.ReadLine();
  33.         }
  34.     }
  35. }
  36.  
I am expecting the output to be

"I am from sub"

"I am from base"

but its not coming.... where am i making mistake?

I know I could achieve this by calling base.PrintOne in subclass... but i dont want it that way.

I want it to be called from base itself.

Please comment.

Thanks in advance.
Zacksoniar
Apr 12 '12 #1

✓ answered by Monomachus

It is not possible. What I meant is that after casting your I object is still instantiated as a G, so he'll get the PrintOne() method from the G because the I one is virtual, meaning what is said here http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

"When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member."

So your G even casted will still point to a G and when the I virtual method is invoked, the run-time type of your object is G. And so it will call PrintOne() from G.

The bottom line is when you instantiate your object as a G, using new G(); it won't change its run-time type.

Hope this helps.

7 1992
Monomachus
127 Expert 100+
That is the way polymorphism works. You override the method. And you declare the
Expand|Select|Wrap|Line Numbers
  1. G g = new G();
so you basically after cast you will still have something like
Expand|Select|Wrap|Line Numbers
  1. I i = new G();
More answers here http://stackoverflow.com/questions/4...n-method-c-net
Apr 13 '12 #2
thanks Monomachus...

but even in this case.. I am not getting what i want i.e.
"I am from sub"
"I am from base"


I know I should have called base.PrintOne() from overridden method.
But I dont want to do that way. I want to achieve calling of base class method & sub class method from base class function only.

My code compiles .. doesn't it mean it is possible?

with ur approach too .. i get
"I am from sub"
"I am from sub"


It is just to understand polymorphism & upcasting..

Thnaks
Apr 13 '12 #3
Monomachus
127 Expert 100+
It is not possible. What I meant is that after casting your I object is still instantiated as a G, so he'll get the PrintOne() method from the G because the I one is virtual, meaning what is said here http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

"When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member."

So your G even casted will still point to a G and when the I virtual method is invoked, the run-time type of your object is G. And so it will call PrintOne() from G.

The bottom line is when you instantiate your object as a G, using new G(); it won't change its run-time type.

Hope this helps.
Apr 13 '12 #4
then what is the significance of this line
Expand|Select|Wrap|Line Numbers
  1. I i = new G();
  2.  
does it mean nothing in C#, cause in java above statement mean a lot of things..

thanks in advance
Apr 14 '12 #5
class I contains the virtual void PrintOne()

then you override the virtual void PrintOne() in class G

you need to call the class that contains your virtual void PrintOne() then assign the class that contains override..

I i = new G();
i.PrintOne();
//you get the override value of the virtual method

I i = new I();
i.PrintOne();
//you get the value of the virtual method

G g = new G();
g.PrintOne();
//you get nothing, because you wish to call for override without any virtual method.
Apr 18 '12 #6
let say in real life..

.

class I, virtual void PrintOne() - is a delivery man that has delivery box.

.

class G, override void PrintOne() - is another delivery delivery box.

.

delivery man can deliver the delivery box.

.

delivery man can deliver another delivery box.

.

but another delivery box cant deliver itself, it needs a delivery man. :))
Apr 18 '12 #7
Thanks everybody .. :-)
Apr 21 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
1
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application....
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
1
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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,...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.