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. Access between classes?

Access between classes?

Scheduled Pinned Locked Moved C#
questionhelptutorial
6 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.
  • T Offline
    T Offline
    tantiboh
    wrote on last edited by
    #1

    I have two classes, ClassA and ClassB: public class ClassA { //Variable definition private int intProperty; //Property definition public int Property { get { return intProperty; } set { intProperty = value; } } //Constructor public ClassA() { ClassBTest = new ClassB(); } } My question is, how do I access and modify ClassA.Property from ClassB? For example (understanding that this doesn't actually work): public class ClassB() { private int Function() { int Temp = ClassA.Property; Temp++ ClassA.Property = Temp; } } There's got to be a fundamental way to do this, I'm just missing that piece of information. Thanks for your help.

    H 1 Reply Last reply
    0
    • T tantiboh

      I have two classes, ClassA and ClassB: public class ClassA { //Variable definition private int intProperty; //Property definition public int Property { get { return intProperty; } set { intProperty = value; } } //Constructor public ClassA() { ClassBTest = new ClassB(); } } My question is, how do I access and modify ClassA.Property from ClassB? For example (understanding that this doesn't actually work): public class ClassB() { private int Function() { int Temp = ClassA.Property; Temp++ ClassA.Property = Temp; } } There's got to be a fundamental way to do this, I'm just missing that piece of information. Thanks for your help.

      H Offline
      H Offline
      hooray
      wrote on last edited by
      #2

      what use wrote in your code was the use of a static property. if you use ClassA.Property the property definition must be: public static int Proprty {...} But you can also use is this way you code it. But then you must create an instance of the class to use the property: ClassA c = new ClassA(); int Temp = c.Property;

      T 1 Reply Last reply
      0
      • H hooray

        what use wrote in your code was the use of a static property. if you use ClassA.Property the property definition must be: public static int Proprty {...} But you can also use is this way you code it. But then you must create an instance of the class to use the property: ClassA c = new ClassA(); int Temp = c.Property;

        T Offline
        T Offline
        tantiboh
        wrote on last edited by
        #3

        That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.

        H T 2 Replies Last reply
        0
        • T tantiboh

          That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.

          H Offline
          H Offline
          hooray
          wrote on last edited by
          #4

          then you must use it as static like this: class ClassA { private static int i; public static int Property { get { return i; } set { i = value; } } } then you can use it via int i = ClassA.Property;

          T 1 Reply Last reply
          0
          • H hooray

            then you must use it as static like this: class ClassA { private static int i; public static int Property { get { return i; } set { i = value; } } } then you can use it via int i = ClassA.Property;

            T Offline
            T Offline
            tantiboh
            wrote on last edited by
            #5

            Works great, thanks. Isn't it amazing how it's always the simple stuff you end up researching for eight hours? I appreciate your help.

            1 Reply Last reply
            0
            • T tantiboh

              That's my dilemma: I need a solution more advanced than that. I cannot create the new class; that defeats the entire purpose of my program. I have to interact with the existing class. I'm playing with static members; that may give me what I need. Thanks.

              T Offline
              T Offline
              turbochimp
              wrote on last edited by
              #6

              Just as an aside - doing what the first reply suggested would not lead to creating a new class. It would lead to creating a new instance of an existing class. Either way (static or instance), you're taking up space on the heap. The difference is that with a static member, all clients in the AppDomain shares a single instance of the static member, and with non-static (instantiation), each call gets a new copy of the object.

              The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

              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