# How to use PRIVATE and PUBLIC variables

- How to use PRIVATE and PUBLIC variables
  - The Program:
  - The Code:
    - Fmain.class
    - Ccar.class
  - The Source

Each object of a class should be master of its own data!
The variables that it uses should be kept private if at all possible.
It should have accessor methods, which can be used to read or write the variables.
It's a kind of standard to name them
setXYZ() to write and
getXYZ() to read.
Each method (sub or function) which is only used for internal stuff should also be private.
These practices give you greater control over which variables are used by which code, and will be very helpful as you begin writing code for use in multiple projects, such as Gambas libraries.

## The Program:

The program just instantiates two objects of a class and sets theis values.
If you click a button, the associated value is displayed.

## The Code:

### Fmain.class

    ferrari AS CCar
    porsche AS CCar

    STATIC PUBLIC SUB Main()
      hForm AS Fmain
      hForm = NEW Fmain
      hForm.show
    END

    PUBLIC SUB _new()
      ferrari = NEW CCar("ferrari", 430, 150000.56)

      porsche = NEW CCar
      porsche.setBrand("porsche")
      porsche.setPS_Power(300)
      porsche.setPrice(100345.72)

'the following does not work!
'porsche.brand = "porsche"

    END

    PUBLIC SUB Button1_Click()
      Label1.Text = ferrari.getBrand()
    END
    PUBLIC SUB Button2_Click()
      TextLabel1.Text = Str(ferrari.getPS_Power()) & " PS <br>" &
      Str(ferrari.getKW_Power()) & " KW"
    END
    PUBLIC SUB Button3_Click()
      TextLabel2.Text = Str(ferrari.getPrice()) & " &euro;"
    END
    '------------------------------------------
    PUBLIC SUB Button4_Click()
      Label4.Text = porsche.getBrand()
    END

    PUBLIC SUB Button5_Click()
      TextLabel3.Text = Str(porsche.getPS_Power()) & " PS <br>" &
      Str(porsche.getKW_Power()) & " KW"
    END

    PUBLIC SUB Button6_Click()
      TextLabel4.Text = Str(porsche.getPrice()) & " &euro;"
    END

### Ccar.class

## The Source

Download