Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What this code is doing

What this code is doing

Scheduled Pinned Locked Moved C#
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hum Dum
    wrote on last edited by
    #1

    I want to know what is happening in these lines of code: a)

    public class cAppBarDisplay:AppBar
    {
    cMessageProcessing CMS;
    //some code here
    public cAppBarDisplay(cMessageProcessing cMP)
    {
    CMS = cMP;//exactly what is happening here
    //cMessageProcessing is another class,
    }

    }

    ->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.

    A A 2 Replies Last reply
    0
    • H Hum Dum

      I want to know what is happening in these lines of code: a)

      public class cAppBarDisplay:AppBar
      {
      cMessageProcessing CMS;
      //some code here
      public cAppBarDisplay(cMessageProcessing cMP)
      {
      CMS = cMP;//exactly what is happening here
      //cMessageProcessing is another class,
      }

      }

      ->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.

      A Offline
      A Offline
      AlwiNus
      wrote on last edited by
      #2

      humdumof wrote:

      ->cMessageProcessing CMS; By this mean are we creating a refrence?

      You created a field called CMS, which you can set to reference an instance of cMessageProcessing. This happens when you do CMS = cMP for example.

      humdumof wrote:

      CMS = cMP;//exactly what is happening here

      You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).

      H 1 Reply Last reply
      0
      • H Hum Dum

        I want to know what is happening in these lines of code: a)

        public class cAppBarDisplay:AppBar
        {
        cMessageProcessing CMS;
        //some code here
        public cAppBarDisplay(cMessageProcessing cMP)
        {
        CMS = cMP;//exactly what is happening here
        //cMessageProcessing is another class,
        }

        }

        ->cMessageProcessing CMS; By this mean are we creating a refrence? ->and what the constructer is actually doing? CMS & cMP both are refrence of same class:confused: Thanks in advance, plz describe in a bit detail.

        A Offline
        A Offline
        ag46677
        wrote on last edited by
        #3

        buddy, U have a typical class code with u. Just look in the code its just a constructor with one parameter which is initiallising the value passed by the parameter to the local class variable.

        1 Reply Last reply
        0
        • A AlwiNus

          humdumof wrote:

          ->cMessageProcessing CMS; By this mean are we creating a refrence?

          You created a field called CMS, which you can set to reference an instance of cMessageProcessing. This happens when you do CMS = cMP for example.

          humdumof wrote:

          CMS = cMP;//exactly what is happening here

          You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).

          H Offline
          H Offline
          Hum Dum
          wrote on last edited by
          #4

          AlwiNus wrote:

          You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).

          Not a refrence to the class itself? By this what u mean?:confused: also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?

          A 1 Reply Last reply
          0
          • H Hum Dum

            AlwiNus wrote:

            You assign the parameter cMP to the field CMS. They now hold the same reference to an instance of the cMessageProcessing class (not a reference to the class itself).

            Not a refrence to the class itself? By this what u mean?:confused: also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            humdumof wrote:

            Not a refrence to the class itself? By this what u mean?

            When you use a class you create an instance of it. Kinda like if i want a bowl of cereal, i have to get myself a bowl and some cereal, and probably some milk. Then my bowl of cereal that i can eat, would be an instance of a bowl of cereal... if you follow me. When you pass the instance of your class to the constructor your passing a reference (pointer) to it.

            humdumof wrote:

            also CMS=cMP; Does it mean we can access all the methods of cMessageProcessing thru CMS? & what is use of doing all this in constructor?

            Yes, you can access all the methods of the class through CMS. The use of this, is that you can make use of the same instance of a class, in as many different classes as you like. So you can make 1 instance of your class, and pass it to 2 other classes for example. Then both classes will work on the same instance. EDIT: As for why you should actually do this in the constructor: If the new class depends on having an instance of cMessageProcessing (ie it uses it) and it does not get passed a reference or create its own instance, then it will probably crash. By passing it to the constructor, you can guarantee that the new class will always have an instance of cMessageProcessing, it would be impossible to instatiate the class (ie myClass = new MyClass()) without passing the correct parameters.

            My current favourite word is: Bauble!

            -SK Genius

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups