Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy

    Why does this inherited method work?

    Say I have a class:

    Code:
     class MachoMan
        {
            public int x;
    
            public virtual void changex() { x += 10; }
                
        }
    And class:

    Code:
    class MiniMan : MachoMan
        {
            public int y;
            public override void changex()
            {
                base.changex();
                
                y += 10;
                Console.WriteLine(y);
            }
        }
    Now I do this:

    Code:
     MachoMan ComboMan = new MiniMan();
    Alright. If I do
    Code:
    ComboMan.y
    it doesn't work. I can understand that, it's presumably because when I made ComboMan type MachoMan, it only had the fields and methods of MiniMan. And since I made it a new MiniMan , it has the fields and methods of MachoMan, but what MiniMan has filled in those field & methods. So basically, I thought it's not suppost to have Y becuase MachoMan doesn't have that. So calling ComboMan.changex() shouldn't work because in the MiniMan class I made a new variable that MachoMan doesn't have, and manipulated it in the changex() method.

    But, when I do this
    Code:
    ComboMan.changex();
    It works fine????? Wtf? Why? If that works then that means that ComboMan still has all of the fields of MachoMan. But how can that be when I made it type MachoMan??

    ----------------------------------------------------------------------------------------------------------------------
    I made the names different. If i'm being confusing , my question is this. So miniman inherits from machoman, and miniman has a y field which machoman does not have.
    When I make comboman of type machoman but set it equal to miniman, it will have this:

    Code:
    public override void changex()
    {
    base.changex();
    
    y += 10;
    Console.WriteLine(y);
    }
    code inside the changex method instead of this

    Code:
    public virtual void changex() { x += 10; }
    PLUS I am not able to do ComboMan.Y becaue it doesn't recognise it! THAT leads me to believe that ComboMan does not have all the fields and methods of MiniMan even if it is set to MiniMan. Instead it has all the fields and methods of MachoMan but they're filled in with what's in MiniMan. So instead of having the changex do the original Machoman's changex, it does what the MiniMan has filled in for it ( which is increment y with 10).

    So my question is, if ComboMan doesn't have the fields and methods of MiniMan, but instead has the fields and methods of MachoMan with what MiniMan has filled in for them, then why does the changex method work? It increments y with 10, but y is a field exclusive to MiniMan. So since ComboMan doesn't have the fields and methods of MiniMan, therefore doesn't have y, it shouldn't work. So why does it work? I hope I've made myself comprehensible.
    Last edited by Laslod; 02-28-2013 at 06:33 PM.

  2. #2
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    "...If that works then that means that inherites still has all of the fields of inherits. But how can that be when I made it type Inherit??"

    Because you didn't make it of type 'inherit', you made the variable of type 'inherits'
    Code:
    Inherit inherites = new Inherits(); <--I see both classes..
    Unless I mis-read. ?

    But I'm really not the one to explain why, sry.
    Last edited by abuckau907; 02-28-2013 at 04:03 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  3. #3
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Huh? I thought when I did

    Inherit inherites = new Inherits();

    I made an inherites of type Inherit and set it equal to Inherits

  4. #4
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    You declare it one type, but can set it = another related type.

    So, which data type is the variable 'inherites'...?

    Declared as inherit, but set = to inhertis. //Is it an 'inherit' or 'inherits' object ?
    Last edited by abuckau907; 02-28-2013 at 04:34 PM. Reason: edit5000
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  5. #5
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Quote Originally Posted by abuckau907 View Post
    You declare it one type, but can set it = another related type.

    So, which data type is the variable 'inherites'...?

    Declared as inherit, but set = to inhertis. //Is it an 'inherit' or 'inherits' object ?
    It's an inherit object declared as inherits.

  6. #6
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    I know, I said that :P So what's the question ?

    "If that works then that means that inherites still has all of the fields of inherits."
    Well, you did set it =new inherits object. I'm not sure what you're *trying* to do.
    Last edited by abuckau907; 02-28-2013 at 04:47 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  7. #7
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    You can start by having names a little different.

  8. #8
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Agreed. Naming is supert important.
    @Laslod - maybe re-make the 2 classes: try something semi-obvious and familiar. I suggest a 'dog' class, and maybe a 'super cop dog' class that inherits from it.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  9. #9
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    @Virtual Void @abuckau907

    I made the names different. If i'm being confusing , my question is this. So miniman inherits from machoman, and miniman has a y field which machoman does not have.
    When I make comboman of type machoman but set it equal to miniman, it will have this:

    public override void changex()
    {
    base.changex();

    y += 10;
    Console.WriteLine(y);
    }
    code inside the changex method instead of this

    public virtual void changex() { x += 10; }

    PLUS I am not able to do ComboMan.Y becaue it doesn't recognise it! THAT leads me to believe that ComboMan does not have all the fields and methods of MiniMan even if it is set to MiniMan. Instead it has all the fields and methods of MachoMan but they're filled in with what's in MiniMan. So instead of having the changex do the original Machoman's changex, it does what the MiniMan has filled in for it ( which is increment y with 10).

    So my question is, if ComboMan doesn't have the fields and methods of MiniMan, but instead has the fields and methods of MachoMan with what MiniMan has filled in for them, then why does the changex method work? It increments y with 10, but y is a field exclusive to MiniMan. So since ComboMan doesn't have the fields and methods of MiniMan, therefore doesn't have y, it shouldn't work. So why does it work? I hope I've made myself comprehensible.

  10. #10
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    post all the new code.. Need to see how you declare the variables etc. I can assume, but having 100% of your code in front of me makes both our lives easier.

    Your class "relationships" still aren't very good. It's not "clear" that a 'miniman' is a 'machoman', or vice versa? You should have a base class of "man" and then 2 derived classes of "miniman" and "machoman" maybe. ? Really really doesn't matter, but naming is everything, and having something named "changex" doesn't exactly make the problem more clear.

    (obviously not real code, but something similar to)

    class dog

    void func Bark()
    {
    << "A basic dog bark"
    }


    clas SuperCopDog : dog
    {
    overrides func Bark()
    <<" SUPER LOUD cop dog bark"
    }


    --create some objects and see which method actually gets called.

    The whole point of polymorphism is to allow you to use 1 class in place of another, but ofc some of the functions won't be accessible(depending on the TYPE of your variable). I can't explain very well, sorry.

    If you can/can't access an object indsie your class....the problem is how you defined your classes and their relationships, so I'm not sure what you're asking when you say (for example) "well I don't see anything after typing in myObj. (see the dot)" because...that's how it's supposed to work. It it does/doesn't allow you to access some variable, it's not an error, that's just how c# does inheritance / how you defined your classes. ? Even though I'm not sure what to say, let's try one more time. Paste all ur code and we'll go from there.

    [b]edit:[b/] you edited your original post. I guess that works, but not what I meant.? Please paste below this msg.
    Last edited by abuckau907; 02-28-2013 at 06:20 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  11. #11
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Quote Originally Posted by abuckau907 View Post
    post all the new code.. Need to see how you declare the variables etc. I can assume, but having 100% of your code in front of me makes both our lives easier.

    Your class "relationships" still aren't very good. It's not "clear" that a 'miniman' is a 'machoman', or vice versa? You should have a base class of "man" and then 2 derived classes of "miniman" and "machoman" maybe. ? Really really doesn't matter, but naming is everything, and having something named "changex" doesn't exactly make the problem more clear.

    (obviously not real code, but something similar to)

    class dog

    void func Bark()
    {
    << "A basic dog bark"
    }


    clas SuperCopDog : dog
    {
    overrides func Bark()
    <<" SUPER LOUD cop dog bark"
    }


    --create some objects and see which method actually gets called.

    The whole point of polymorphism is to allow you to use 1 class in place of another, but ofc some of the functions won't be accessible(depending on the TYPE of your variable). I can't explain very well, sorry.

    If you can/can't access an object indsie your class....the problem is how you defined your classes and their relationships, so I'm not sure what you're asking when you say (for example) "well I don't see anything after typing in myObj. (see the dot)" because...that's how it's supposed to work. It it does/doesn't allow you to access some variable, it's not an error, that's just how c# does inheritance / how you defined your classes. ? Even though I'm not sure what to say, let's try one more time. Paste all ur code and we'll go from there.

    [b]edit:[b/] you edited your original post. I guess that works, but not what I meant.? Please paste below this msg.
    Huh? I don't get what you're saying. There's no new code I just edited the main one and made my message clearer because you guys told me to change the names.
    I just want to know the why. I know how it works but I want to know why it works. I'll put my clarification attempt on the original post so it's better visible I gguess.

  12. #12
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    why: Because that's how C# implements inheritance. I'm not sure what you mean.

    Your object was declared as the "base" class, but set = "derived" class.


    You want to know why it can/can't access certain members of both classes?
    Idk 'why', sry.
    Last edited by abuckau907; 02-28-2013 at 06:41 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  13. #13
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Quote Originally Posted by abuckau907 View Post
    why: Because that's how C# implements inheritance. I'm not sure what you mean.

    Your object was declared as the "base" class, but set = "derived" class.


    You want to know why it can/can't access certain members of both classes?
    Idk 'why', sry.
    Yes I want to know why ComboMan seemingly doesn't have the Y field but is able to change it with the changex() method without generating an error.

  14. #14
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    public virtual void changex


    Virtual Functions cause this behavior - by design.


    I really can't explain anymore, sry. If you have skype, feel free too add me. I'm obviously failing at explaining, I'm sorry.


    -Pick 2 different classes, please. It will be "more obvious" if you choose the proper classes.

    Classes should exhibit the "is a" relationship. "miniman" is a "machoman" seems a little weird, but doesn't *really* matter.
    Last edited by abuckau907; 02-28-2013 at 07:12 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  15. #15
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    You need to read up on how inheritance works. Although you constructed a MiniMan instance, you stored it in a variable of type MachoMan. This is perfectly valid, however because you're now using a MachoMan variable, you no longer have access to additional methods/variables that the MiniMan class made available. You do, however, have access to the methods made available in the MachoMan base class (changex). This is where inheritance can get tricky to wrap your head around, so I recommend doing some proper reading on the subject. When you call changex(), you're actually going to call the MiniMan implementation of changex(), not the original MachoMan implementation, because your variable is an instance of the MachoMan derived class, MiniMan.

    Take a look at this article for a more in-depth explanation of how inheritance and polymorphism work. It can take some time to wrap your head around it, but it's well worth learning.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

Page 1 of 2 12 LastLast

Similar Threads

  1. [Help] Why does this dont work?
    By skulhead in forum WarRock Hack Source Code
    Replies: 10
    Last Post: 04-01-2011, 08:47 AM
  2. [Help] Why does this dont work?
    By skulhead in forum WarRock Hack Source Code
    Replies: 11
    Last Post: 02-26-2011, 12:24 AM
  3. Why does this not work?
    By extremehack in forum Combat Arms Hack Coding / Programming / Source Code
    Replies: 8
    Last Post: 08-15-2010, 09:47 AM
  4. Why does this site run so god damn slow?
    By shazbork in forum Flaming & Rage
    Replies: 24
    Last Post: 09-13-2008, 10:49 PM
  5. Why does 2.0 bypass work for me but not 3.5?
    By Mattsta in forum Combat Arms Hacks & Cheats
    Replies: 12
    Last Post: 08-17-2008, 02:55 PM