Public and Private on Types

BlitzMax Forums/BlitzMax Beginners Area/Public and Private on Types

vinians(Posted 2010) [#1]
Hi guys!
I was reading BlitzMax manual but I cannot figured out how to use Public and Private on my type. I got an error! See:
Type Test
Private
Field _x%, _y%
Public
Method set_x(__x%)
_x = __x
EndMethod
Method set_y(__y%)
_y = __y
EndMethod
EndType
Whats wrong?


_Skully(Posted 2010) [#2]
Its like this...

Private
' code in between is now private... but why do this!
Type Test
  field _x%,_y%
end type
Public



therevills(Posted 2010) [#3]
I think vinians wants to do proper encapsulation, which you cant do in BlitzMax.

The public/private keywords are for the source file:

Private makes a Constant, Global variable or Function only accessible from within the current source file


Public makes a Constant, Global variable or Function accessible from outside the current source file (Default)


You cant really make fields and methods private like in C++ or Java.

Just pretend that they are:

Type TSprite

  Field x#, y# ' private fields

  Method setX(x#) ' public
    Self.x = x
  End Method

  Method getX#() ' public
    Return x
  End Method

End Type

Local s:TSprite = New TSprite

s.setX(100)

Print s.getX()



vinians(Posted 2010) [#4]
Exactly therevills!!!
But ok then :D I Was just wondering, maybe on the future we will get private, protected and Public methods and Fields and a free upgrade lol


Czar Flavius(Posted 2010) [#5]
This has been requested for some time but it doesn't look likey to happen. I'm not sure why, it would be very easy to add. It doesn't change any compilation, you just do a syntax check that private stuff isn't accessed out of the type declaration.

It's become a kind of standard to prefix a private method or variable with an underscore, such as _ to denote it shouldn't be used outside the type.


Kryzon(Posted 2010) [#6]
It's become a kind of standard to prefix a private method or variable with an underscore, such as _ to denote it shouldn't be used outside the type.

You just saved me starting an embarassing thread to ask that.


Bobysait(Posted 2015) [#7]
Pretty old topic but here is a way to do it :



Now the privateValue is only accessible via getter and setter from the extended type.

Of course, it only works on separated files as the public/private does not affect the code within the same page.