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. Variable visibility in subclasses...

Variable visibility in subclasses...

Scheduled Pinned Locked Moved C#
question
7 Posts 3 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.
  • D Offline
    D Offline
    devzav
    wrote on last edited by
    #1

    Hi all! I have a class like this: class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } } Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. Luca

    A G 2 Replies Last reply
    0
    • D devzav

      Hi all! I have a class like this: class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } } Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. Luca

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

      You could... pass a reference to your strA to class B through its constructor. Or, you could probably do something with delegates. I must check up on delegates one day. :rolleyes:

      My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -The Undefeated

      D 1 Reply Last reply
      0
      • A Anthony Mushrow

        You could... pass a reference to your strA to class B through its constructor. Or, you could probably do something with delegates. I must check up on delegates one day. :rolleyes:

        My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -The Undefeated

        D Offline
        D Offline
        devzav
        wrote on last edited by
        #3

        Thank you! I thought to do that too. It seems to be the simplest way to obtain that I asked :)

        Hope that helps! Luca Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks!

        1 Reply Last reply
        0
        • D devzav

          Hi all! I have a class like this: class A { private string strA; private B mySubclass = new B(); ... class B { public B() { string strB = strA // <--- this } } } Is there a way to make strA visible only for class B and not for other classes? If not can you suggest a workaround to do that? Thank you. Luca

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          If it actually was a subclass, you could just make the string protected. As B is not a subclass at all, you can't do that. You have to pass a reference to the instance of the class.

          Experience is the sum of all the mistakes you have done.

          D 1 Reply Last reply
          0
          • G Guffa

            If it actually was a subclass, you could just make the string protected. As B is not a subclass at all, you can't do that. You have to pass a reference to the instance of the class.

            Experience is the sum of all the mistakes you have done.

            D Offline
            D Offline
            devzav
            wrote on last edited by
            #5

            Thank you for the answer! And how can I do to make B a "real" subclass?

            G 1 Reply Last reply
            0
            • D devzav

              Thank you for the answer! And how can I do to make B a "real" subclass?

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              devzav wrote:

              And how can I do to make B a "real" subclass?

              Inherit from the A class:

              class B : A {
              ...
              }

              Experience is the sum of all the mistakes you have done.

              D 1 Reply Last reply
              0
              • G Guffa

                devzav wrote:

                And how can I do to make B a "real" subclass?

                Inherit from the A class:

                class B : A {
                ...
                }

                Experience is the sum of all the mistakes you have done.

                D Offline
                D Offline
                devzav
                wrote on last edited by
                #7

                I need the "like subclass" :) I used in the example. So I'll pass a reference to B. Thank you very much for your answer!

                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