Announcement

Collapse
No announcement yet.

Public class variables?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Walt, i think you have not understood the question... wat i want is to have a variable inside class that is reachabe from outside of the class, but not a global variable, because then all the classes will share the same global variable, rendering the individuality of each class useless.

    What i need is a variable that each individual class instance can contain separately, and that it is reachable from the outside. For example:

    Code:
    MyClass1.MyVariable = 10
    And a separate instance of the same class:

    Code:
    MyClass2.MyVariable = 20
    And that when i use each variable, the values differ individually to each class instance. Kind of like an UDT. I thought it was clear from the original question, but if it was not, im sorry about the confusion.
    www.pluribasic.com

    Comment


    • #22
      Walt, i think you have not understood the question... wat i want is to have a variable inside class that is reachabe from outside of the class, but not a global variable, because then all the classes will share the same global variable, rendering the individuality of each class useless. ...and that when i use each variable, the values differ individually to each class instance.
      Eric Pearson answered this.... in Post #2.... one hour ten minutes after you asked, at what was very early in the AM for him.

      This variable value is a PROPERTY!

      MCM
      Michael Mattias
      Tal Systems Inc.
      Racine WI USA
      mmattias@talsystems.com
      http://www.talsystems.com

      Comment


      • #23
        Code:
        #COMPILE EXE
        #DIM ALL
        
        CLASS MyClass
        
         INSTANCE MyVar AS LONG
        
         INTERFACE MyInterface
        
           INHERIT IUNKNOWN
        
           PROPERTY GET MyVar () AS LONG
              PROPERTY = MyVar
           END PROPERTY
        
           PROPERTY SET MyVar (BYVAL value AS LONG)
              MyVar = value
           END PROPERTY
        
         END INTERFACE
        
        END CLASS
        
        FUNCTION PBMAIN () AS LONG
        
           DIM pObj AS MyInterface
           pObj = CLASS "MyClass"
           pObj.MyVar = 123
           ? STR$(pObj.MyVar)
        
           DIM pObj2 AS MyInterface
           pObj2 = CLASS "MyClass"
           pObj2.MyVar = 567
           ? STR$(pObj2.MyVar)
        
        END FUNCTION
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #24
          Originally posted by Michael Mattias View Post
          Eric Pearson answered this.... in Post #2.... one hour ten minutes after you asked, at what was very early in the AM for him. This variable value is a PROPERTY!
          Thanks Michael, it was also early when i replied in Post #6.

          Originally posted by José Roca View Post
          Code:
          #COMPILE EXE
          #DIM ALL
          [...]
          Thanks Jose, i think that is how it is going to have to be. There is an undefined number of instance variables, and i wanted the class declaration to be simpler but, this is the best solution possible.
          www.pluribasic.com

          Comment


          • #25
            Thanks Michael, it was also early when i replied in Post #6.
            In said post you say..
            Yeah but i would like a simpler form of variable that doesnt require sanitizing and reclaring a property get or set for every single variable...
            Which I will read as, "Thank you for your answer, Eric; but too bad I didn't really know what I meant when I asked.. or maybe I just didn't know how to ask. "

            Except, you DID know what you wanted you wanted a separate value for each INSTANCE of the class which clearly means "santiizing" or (DECLAREing) every external use of the variable.

            And for Jose..
            Thanks Jose, i think that is how it is going to have to be
            ... That is just the basics of using PROPERTYs .. as suggested by Eric (and others).



            Michael Mattias
            Tal Systems Inc.
            Racine WI USA
            mmattias@talsystems.com
            http://www.talsystems.com

            Comment


            • #26
              > Eric Pearson answered this.

              Michael, like you, I have limited experience with objects, and have demonstrated no particular expertise about them. Unlike you, I asked a question so that I could learn from people like José and Brian, and I have gained knowledge from this discussion.

              Churchill didn't say it, but I'm sure he'd concur that expertise in one area does not imply expertise in all areas. The same principle could be applied to Hutch's current thread about asm, and your response therein.

              "Not my circus, not my monkeys."

              Comment


              • #27
                Originally posted by Michael Mattias View Post
                Which I will read as...
                Depends on who reads it Michael. I thought erick understood perfectly. What i meant to say was, "Thanks! is there a less verbose method to do it?" And that is what i tried to explain in my reply.

                Eric, thanks for your poiliteness, my english is not perfect, im sorry if my question (or reply) was not clear at first.

                About Public variables. I went ahead and added my own for PowerBASIC.

                Code:
                PUBLIC SomeVar AS WHATEVER
                Thanks guys!




                www.pluribasic.com

                Comment


                • #28
                  Originally posted by José Roca View Post
                  Other kind of classes can have public, private and/or protected variables, but PB classes don't.
                  I hear ya ...

                  In the last couple of month, for the first time I'm using PB classes extensively. And by trying to port over some libraries from VB Classic, which is notoriously lacking in the OOP department compared to other languages, I noticed just how ... erhm ... "limited" PB's OOPish implementation is.

                  It leaves a lot to be desired, even in comparison to a decade long dead language like VB Classic and considering how much later (=could've peaked how things are done elsewhere) PB has introduced OO. And the things that are implemented are often terribly awkward to use/make no sense the way they're implemented.

                  <rant />

                  Comment


                  • #29
                    This must be because PB has not implemented OOP, but COM. They're different animals. Your frustration comes from trying to use COM to do OOP.
                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                    Comment


                    • #30
                      In PB this is not possible. Maybe in PB x64 it would be an idea to have such a switch. As its with GOTO, the real day programming may be wite different from the pure paper-definition.
                      For example actually we can not currently put a Callback-Sub into a COM Object.
                      --Theo Gottwald
                      ------------------------------------------------
                      76706 Dettenheim * Germany * info@it-berater.org
                      ------------------------------------------------
                      Joses Forum * Theo's Link Site * IT-Berater.org

                      Comment


                      • #31
                        Originally posted by José Roca View Post
                        This must be because PB has not implemented OOP, but COM. They're different animals. Your frustration comes from trying to use COM to do OOP.
                        Exactly. And having a COM class named a CLASS does not help much. Most "foreigners" will come expecting to see the same familiar faces... and then BAM! some "weird" thing with two faces.

                        I think COM classes can (at some extent) be used as the classes other languages have, but it takes a little tweaking.

                        www.pluribasic.com

                        Comment


                        • #32
                          José, Brian,

                          you're both right.

                          But than again for implemnting COM, good old structural programming would have suffice. Instead PB dedicated a whole section in the help file to Objects.

                          BTW, I can code around around all the superflous limitations posed on OO programming by the PB compilers. My posts are more intended to draw Drake Software's attention to the areas they might want to improve upon. And this is seperated from any 32/64 bit compiler decisions.

                          Comment


                          • #33
                            I am a Java programmer nowadays and have worked with C# for quite some time.

                            While Java supports having mutable public instance variables in a class, it is considered a very poor design choice and it is always recommended to have public getters/setters (called properties) for mutable instance variables and keep the variables themselves private. This has mainly to do with programming to contracts: how the class internally stores property values is considered an implementation detail that can change later and is not the business of their clients (the code that uses the class). Another bonus could be that a setter can potentially choose to make it thread safe (for example by locking the method while it is called for other threads), but locking is expensive and, AFAIK, even considered old-fashioned in the JVM world.

                            Nowadays a programming paradigm called "functional programming" is gaining more and more attraction. With this style of programming, variables are no longer mutable. Instead, objects are supposed to create and return a new instance of themselves (with the change applied) when one or more values changes. The current instance always stays the same, guaranteed. The good languages/platforms offer data structures that keep references to the old (unchanged) data, so that not too much memory is being wasted. This makes concurrent computing much easier, changes never mess up other threads that are processing the same instance(s). Rather expensive CPU locks on threads are generally not needed anymore as well. With properties those kind of rules are way easier to enforce. There's a lot more to say about FP, see https://en.wikipedia.org/wiki/Functional_programming for more information.

                            FP is probably most important for modern web-services and web applications that need to handle a huge amount of traffic and/or data concurrently. Whether FP will be important for desktop applications and the BASIC language in particular remain to be seen IMHO. Still, I think PowerBASIC is flexible enough for many aspects of FP..

                            Still, even without FP, using properties, while verbose indeed, is in my opinion, a good choice for a clean internal API design.
                            Last edited by Vincent van der Leun; 14 Feb 2018, 06:51 PM.

                            Comment


                            • #34
                              Vince, i am a Java programmer too, and i understand why having mutable public variables can be considered bad practice, but under some circumstances, it is the more logic option. For example when having boolean variables. Since the boolean variables can have only 2 states, it makes little sense having to dedicate 2 whole functions just to change them.

                              Most class input will require sanitizing, but i personally believe trying to impose coding styles to programmers is a little pretentious.... like those lanaguages that do not have GOTO because its "dangerous". Yes, as dangerous as a knife in the hands of a baby. Using it correctly should not have any problems and makes better code. In the same way, public variables can be "dangerous" and may lead to problems if not used correctly, but thats an IF.

                              About "FP"... i can do in powerbasic any web Application i can do in PHP (a language specific for web apps), and sometimes with not many differences in syntax. OOP is a style of coding, nothing else, many things achievable with it are achievable with old fashioned code, but thats a trend we developers are in need of following.
                              www.pluribasic.com

                              Comment

                              Working...
                              X