Determine what called a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • developing
    New Member
    • Mar 2007
    • 110

    Determine what called a function

    So whats the best way form myFunction() to know if it got called by classA or classB ?

    I don't want to pass state values since im convinced there has to be a better way
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well there is the Parent property of the object...you can use the TypeOf (or GetType) method to determine what type of object called it...

    Why do you need to know the calling class type?

    It sounds like you may be interested in looking into a Custom Event (with Custom EventArgs)....

    -Frinny

    Comment

    • developing
      New Member
      • Mar 2007
      • 110

      #3
      yeah GetType() and typeof work


      okay, now if I have a class Card with two instances running at the same time:

      Card creditCard = new Card();
      and
      Card debitCard = new Card();


      and they both call the function Card.Buy(), how would Buy() determine if it should take money from creditCard or debitCard?

      Comment

      • joedeene
        Contributor
        • Jul 2008
        • 579

        #4
        Put a parameter in your card.buy() function so that it has ByVal cardType as String or something so when you call the function you say card.buy("Debit ") if its debit and have an if else statement in the buy function to see if the cardType parameter is "Debit" or "Credit" something like that ?

        joedeene

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          As for your example, if I were you, .Buy() should not be a static member of Card.
          Perhaps a private static function like DeductAccount(d ouble amount, int64 AccountNumber) maybe, where the instance of Card as a member method of .Buy(double amount) and inside the function calls the DeductAccount() with that amount, and its internal instance of accountnumber.

          Example:
          [code=c#]
          public class Card
          {
          private UInt64 _accountnumber= 0;
          private static DeductAccount(d ouble Amount, UInt64 AccountNumber)
          {//...
          }
          public bool Buy(double Amount)
          {
          //..
          DeductAccount(A mount,this._acc ountnumber);
          //..
          }
          }
          [/code]


          As for not wanting to pass in references to things, you will notice that all of microsoft's events contain the "object sender" parameter, they do it, you could too.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by joedeene
            Put a parameter in your card.buy() function so that it has ByVal cardType as String or something so when you call the function you say card.buy("Debit ") if its debit and have an if else statement in the buy function to see if the cardType parameter is "Debit" or "Credit" something like that ?

            joedeene

            This still doesn't make sense.
            First of all, Plater's right, the method should not be static (or shared).

            It seems to me the "Buy" method is trying to do too much.
            You should reconsider the best place to put this implementation.

            -Frinny

            Comment

            • developing
              New Member
              • Mar 2007
              • 110

              #7
              Thanks for your replies.

              I am not programming anything right now. This is just something I have been wondering in my "free time". Don't think I know enough about security to get hired as a financial developer just yet.

              Also, whats wrong with Buy() being static? Sure, it can be called by anything, but if its return type is 'void' or 'bool', does it really matter? Good programming practice?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by developing
                Thanks for your replies.

                I am not programming anything right now. This is just something I have been wondering in my "free time". Don't think I know enough about security to get hired as a financial developer just yet.

                Also, whats wrong with Buy() being static? Sure, it can be called by anything, but if its return type is 'void' or 'bool', does it really matter? Good programming practice?
                It doesn't matter what the method returns...

                Static (or Shared in VB) methods are "shared" between all instances of a class.
                Say you have your 5 instances of your Card class, if you make your Buy() method static, this same method will be used by all 5 instances of your Card class.

                This means that static methods cannot contain any data that pertains to a specific instance of your class.

                So, say your Card class has a private Boolean "isDebitCar d" used to identify what type of card it is. This Boolean cannot be used in the static method "Buy()" because it is unique to that instance of the Card Class. Therefore the Buy() method cannot be static because it cannot be shared between all of the Card Objects.

                You should look into the topic of Scope.
                You should also look into Object Coupling and Cohesion.

                -Frinny

                Comment

                Working...