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. how to use variable form other class? C#

how to use variable form other class? C#

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
7 Posts 5 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.
  • P Offline
    P Offline
    ping_jacob
    wrote on last edited by
    #1

    if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...

    K C P S 4 Replies Last reply
    0
    • P ping_jacob

      if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      class B
      {
      public void SomeMethod()
      {
      if (A.a = "aaa")
      {
      DoSomeStuff();
      }
      }
      }

      The A class needs to be public for this to work if the classes have different namespaces. The A class can also be internal if the namespace of the two classes is the same.

      Kristian Sixhoej sixhoej.net - forums.sixhoej.net

      modified on Thursday, November 27, 2008 1:50 PM

      1 Reply Last reply
      0
      • P ping_jacob

        if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        This is so rudimentary that you really need to buy a book and work through it, before you try to write any code that's not a book example.

        Christian Graus Driven to the arms of OSX by Vista.

        1 Reply Last reply
        0
        • P ping_jacob

          if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...

          P Offline
          P Offline
          Pedram Behroozi
          wrote on last edited by
          #4

          Hi, You need to create an instance from class A so you can use its public members. In this case you should write:

          public class A
          {
          public string a = "aaa";
          }

          public class B
          {
          A Ainstance = new A();
          if(Ainstance.a = "aaa") {}
          if (Ainstance.a == "aaa") { ;}
          }

          If variable a (or any member in your class) is static, you can use it from class B without creating an instance. Just like:

          public class A
          {
          public static string a = "aaa";
          }

          class B
          {
          if(A.a = "aaa") {}
          if(A.a == "aaa") { ;}
          }

          Hope can help :)

          I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

          modified on Thursday, November 27, 2008 4:22 PM

          P 1 Reply Last reply
          0
          • P Pedram Behroozi

            Hi, You need to create an instance from class A so you can use its public members. In this case you should write:

            public class A
            {
            public string a = "aaa";
            }

            public class B
            {
            A Ainstance = new A();
            if(Ainstance.a = "aaa") {}
            if (Ainstance.a == "aaa") { ;}
            }

            If variable a (or any member in your class) is static, you can use it from class B without creating an instance. Just like:

            public class A
            {
            public static string a = "aaa";
            }

            class B
            {
            if(A.a = "aaa") {}
            if(A.a == "aaa") { ;}
            }

            Hope can help :)

            I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

            modified on Thursday, November 27, 2008 4:22 PM

            P Offline
            P Offline
            ping_jacob
            wrote on last edited by
            #5

            your tip can help me thank so much...

            P 1 Reply Last reply
            0
            • P ping_jacob

              your tip can help me thank so much...

              P Offline
              P Offline
              Pedram Behroozi
              wrote on last edited by
              #6

              You're welcome :)

              I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

              1 Reply Last reply
              0
              • P ping_jacob

                if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...

                S Offline
                S Offline
                SelvaSR
                wrote on last edited by
                #7

                use internal variable to access it from same namespace.

                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