Results 1 to 26 of 26

Thread: [RESOLVED] Method overridiing Issue

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Resolved [RESOLVED] Method overridiing Issue

    Can any body tell me ?why i am getting the following error.namespace does not directly contain members such as fields or methods.when i tried to run the following code.Kindly let me know the idea.
    Code:
    using System;
     class A
     {
     public void Driver()
         {
         Console.WriteLine("This is the Driver method of the Class A");
            }
        }
    
    Class B: A
    
       {
    
     New Public Void Driver()
       {
    Console.writeline("This is the Overridden Driver method of the Class B");
        }
       }
    
     Class Test
       {
    Public Static Void Main()
       {
    B objB=New B();
    oBJB.Driver();
        }
    }

  2. #2
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    hay firoz.raj,
    you can not use the new Key word in the definition of a method.


    please have a look
    http://www.akadia.com/services/dotnet_polymorphism.html
    http://www.codeproject.com/KB/cs/cs_methodoverride.aspx
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Method overridiing Issue

    1. Unlike VB, C# is a case-sensitive language. So public, Public, void, Void, static, Static are all different from each other. Usually the lowercase ones are valid keywords.
    2. new keyword is used to initialize objects. So they can't be used in method/property/class declarations.
    3. "override" keyword can be used to override base class members.

    c# Code:
    1. using System;
    2. class A
    3. {
    4.     public void Driver()
    5.     {
    6.         Console.WriteLine("This is the Driver method of the Class A");
    7.     }
    8. }
    9.  
    10. class B : A
    11. {
    12.  
    13.     public override void Driver()
    14.     {
    15.         Console.WriteLine("This is the Overridden Driver method of the Class B");
    16.     }
    17. }
    18.  
    19. class Test
    20. {
    21.     public static void Main()
    22.     {
    23.         B objB = new B();
    24.         objB.Driver();
    25.     }
    26. }
    Last edited by Pradeep1210; Jul 10th, 2010 at 05:03 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    Still Not Working .here is the following code what i have written.Kindly let me know the idea.Any help would be highly appreciated.
    Code:
    using System;
     class A
     {
     public void Driver()
         {
         Console.WriteLine("This is the Driver method of the Class A");
            }
        }
    
    class B: A
    
       {
    
     Public override Void Driver()
       {
    Console.writeline("This is the Overridden Driver method of the Class B");
        }
       }
    
     Class Test
       {
    Public Static Void Main()
       {
    B objB=New B();
    oBJB.Driver();
        }
    }

  5. #5
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    hay,

    Code:
     Public override Void Driver()
    must be
    Code:
     public override void Driver()
    it is case sensitive

    and in your test class
    Code:
    Public Static Void Main()
    it must be
    Code:
    public static void Main()
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    Still getting error .here is the following code .what i have written.
    Code:
    using System;
     class A
     {
     public void Driver()
         {
         console.WriteLine("This is the Driver method of the Class A");
            }
        }
    
    class B: A
    
       {
    
     public override void Driver()
       {
    console.writeline("This is the Overridden Driver method of the Class B");
        }
       }
    class Test
       {
    Public static void Main()
       {
    B objB=New B();
    oBJB.Driver();
        }
    }

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. Shadowing a member and overriding it are similar but not the same. Generally speaking, it's preferable to declare a base member 'overridable' and then declare a derived member of the same name 'override'. If the base member isn't declared 'overridable' then you must declare the derived member 'new'. If you were to try Pradeep's code you would get an error message telling you that Driver can't be overridden.

    The main difference between overriding and shadowing is that overriding follows the type of the object, while shadowing follows the type of the reference. To demonstrate this, try running this code:
    Code:
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                BaseClass b = new BaseClass();
    
                Console.WriteLine("BaseClass object via BaseClass reference:");
                b.OverridableMethod();
                b.NotOverridableMethod();
                Console.WriteLine();
    
                DerivedClass d = new DerivedClass();
    
                Console.WriteLine("DerivedClass object via DerivedClass reference:");
                d.OverridableMethod();
                d.NotOverridableMethod();
                Console.WriteLine();
    
                b = d;
    
                Console.WriteLine("DerivedClass object via BaseClass reference:");
                b.OverridableMethod();
                b.NotOverridableMethod();
    
                Console.ReadLine();
            }
        }
    
        public class BaseClass
        {
            public virtual void OverridableMethod()
            {
                Console.WriteLine("BaseClass.OverridableMethod");
            }
    
            public void NotOverridableMethod()
            {
                Console.WriteLine("BaseClass.NotOverridableMethod");
            }
        }
    
        public class DerivedClass : BaseClass
        {
            public override void OverridableMethod()
            {
                Console.WriteLine("DerivedClass.OverridableMethod");
            }
    
            public new void NotOverridableMethod()
            {
                Console.WriteLine("DerivedClass.NotOverridableMethod");
            }
        }
    }
    As you'll see form the output, the first two cases are exactly as you'd expect: a BaseClass object via a BaseClass reference reports BaseClass methods and a DerivedClass object via a DerivedClass reference reports DerivedClass methods. It's the third case that gets interesting. When accessing a DerivedClass object via a BaseClass reference, the overridden method reports a DerivedClass method, while the shadowed method reports a BaseClass method. So the overridden members will always be invoked on the type of the object itself, while shadowed members will be invoked on the type of the reference, even if the actual object is a more derived type. This is why overriding is preferable, but shadowing is all you can do if you're not the author of the base class and they didn't declare the member virtual.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    hay,

    this is code will work
    Code:
    using System;
        class Test
        {
            static void Main()
            {
                B objB = new B();
                objB.Driver();
            }
        }
    
        class A
        {
            public virtual void Driver()
            {
                Console.WriteLine("This is the Driver method of the Class A");
            }
        }
    
        class B : A
        {
    
            public override void Driver()
            {
                Console.WriteLine("This is the Overridden Driver method of the Class B");
            }
        }
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  9. #9
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    about the errors,

    Code:
    console.writeline
    as we said it is a c# is case sensitive,
    it must be
    Code:
    Console.WriteLine
    -----
    you declared
    Code:
    B objB=New B();
    oBJB.Driver();
    and in the name was not the same, this object oBJB not exist., its name is objB
    --------
    at last you have to use virtual key word in the A class
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Method overridiing Issue

    Quote Originally Posted by jmcilhinney View Post
    There's a bit of misinformation being provided here. The C# 'new' keyword IS valid on methods and property declarations. The VB equivalent is Shadows. ...
    Great!
    So I learnt a new use of new keyword today.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    hay jmcilhinney,
    can you provide example for using new Key word with method ?
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    Quote Originally Posted by avrail View Post
    hay jmcilhinney,
    can you provide example for using new Key word with method ?
    Perhaps you should read the information I've already provided before asking me to provide more.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    ok, thanks
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    hay jmcilhinney,
    can you provide example for using new Key word with method ?
    Hi,avrail
    see the following .you can use
    Code:
    public new void Driver()
    or
    Code:
    public virtual void Driver()
    or
    Code:
    new public void Driver()
    this also works.
    Code:
    using System;
     class A
     {
     public void Driver()
         {
         Console.WriteLine("This is the Driver method of the Class A");
         Console.Read();
            }
        }
    
    class B: A
    
       {
    
          public new void Driver()  //this will also work
     //   public virtual void Driver() //this will work
       {
    Console.WriteLine("This is the Overridden Driver method of the Class B");
    Console.Read();
          }
       }
    class Test
       {
    public static void Main()
       {
    B objB=new B();
    objB.Driver();
        }
    }
    Last edited by firoz.raj; Jul 10th, 2010 at 07:24 AM.

  15. #15
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Method overridiing Issue

    thanks firoz.raj
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    Quote Originally Posted by firoz.raj View Post
    you can use public new void Driver() instead of
    public virtual void Driver() also.
    That just goes to show that you've missed the point. There is one reason and one reason only to use the 'new' keyword: to shadow a member inherited from a base class. 'virtual' and 'new' are not interchangeable because they do two completely different things. 'virtual' is used in a base class to indicate that a member can be overridden. 'new' is used in a derived class to shadow an inherited member that has not been declared 'virtual'. Overriding an inherited member is always preferable to shadowing so you would only ever use 'new' if you needed to replace the base implementation of a member in a derived class but you couldn't use 'override' because the base member wasn't declared 'virtual'.

    Another point to note is that a shadowed member can return a different type to the base member while an overridden member cannot.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    Thank you.
    Last edited by firoz.raj; Jul 11th, 2010 at 12:34 AM.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    OK, that's twice now that someone has asked a question that I've already answered in post #7. If you want answers but can't make the effort to read them when they're provided then you're wasting my time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    can anyone tell me please.what is the difference between Method Overriding.
    and polymorphism???????.As per my knowledge.Method Overriding.Normally when methods are available in different version.compiler are identified these methods on the basis of Method parameter.then it is called Method overriding.it is as same as Polymorphism.let me clarify my doubt.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    Overriding and polymorphism have nothing really to do with each other. Overriding is as has already been demonstrated: replacing the implementation of an inherited member in a derived class.

    "Poly" means "many" and "morph" means "shape", so "polymorphism" is literally "the ability to have many shapes". Specifically, polymorphism in OOP means that you can declare something (variable, property, method parameter, etc) as one type and then you can assign objects of many types to it, as long as the object is of that type or a type that inherits that type.

    Take MDI applications for instance. When you want to display a form as an MDI child, you assign the parent form to its MdiParent property. The MdiParent property is declared type Form, but you can assign any form to it because every form is of a type that inherits the Form class.

    In .NET apps, polymorphism allows for any type that inherits a specific base type or implements a specific interface. An example of that is using a 'foreach' loop. The 'foreach' statement allows you to loop through any object that implements the IEnumerable interface, so when you use 'foreach' to loop through an array or List<T> or whatever, you're making use of polymorphism.

    I guess where polymorphism and overriding intersect a little is exactly how I've demonstrated in my code example in post #7. You can access an instance of a derived type via a variable of a base type and it will still be the derived implementation of the member that is invoked.
    Last edited by jmcilhinney; Jul 11th, 2010 at 01:49 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    can anybody tell me ?why saying invalid token.
    Code:
    using System;
     class baseIO
             {
            public string FileName;
                public int Delete()
                  {
                Console.Writeline("This is the delete Method of the BaseIO Class");
                return(0);
                   }
                  
                  class imageIO :baseIO 
                     {
                   public string imgformat;
                   public int alphaBlend()
                        {
                       Console.WriteLine ("This is the alpha Blending method of the imageIO Class");
                       return(0);
                         }
                      }
               class Test
                 {
               static void Main()
                 {
                imageIO obj=New imageIO();
                obj.FileName ="ffffffff";
                obj.Delete();
                       }
                      }
    Last edited by firoz.raj; Jul 11th, 2010 at 11:14 PM.

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    Why does your code have such bad formatting? It would be far easier to read for both you and us if it was formatted properly, and probably less prone to errors too. As for your question, could you perhaps point out exactly where this error occurs? It feels like you're trying to make this as hard for us as possible. You certainly aren't going out of your way to make it easier. If you'd like us to take the time and make the effort to help you, I suggest that you take the time and make the effort to help us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  23. #23

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Method overridiing Issue

    Can you tell me ?why i am gettiing error no suitable method found to override.
    let me know please.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace AdvanoopTest
    {
        class A
        {
            public  int methodA()
               {
               return (100);
                }
        }
        class B : A
        {
            public override int methodB()
            {
                return (399);
            }
        }
            class test
             {
             public static void Main()
                {
                B ObjB = new B();
                System.Console.WriteLine(ObjB.methodA());
                Console.Read();
                }
    
    
        }
    }
    Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    Have you read the documentation for the 'override' keyword? If you have then you already have your answer. If you haven't then I suggest that you get to it. It's about 20 seconds work to answer this question for yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  25. #25

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Question Re: Method overridiing Issue

    Resolved
    Last edited by firoz.raj; Jul 17th, 2010 at 03:29 AM.

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method overridiing Issue

    You're the one who asked the question. You tell us whether that answers your question or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width