OOP Headache - type confusion

BlitzMax Forums/BlitzMax Beginners Area/OOP Headache - type confusion

Imphenzia(Posted 2010) [#1]
Consider the code below and imagine:

* TManager and TBase is found a Blitzmax mod
* TTop is found in a code that has imported the mod

I want to achieve what the outcome of the current example is, BUT, I want to have lines such as row 20 (top.manager = manager) to exist in TBase instead to hide it as I don't want the user of the mod to have that in their code.

How would I best do this? I can't throw it into the New() method of TBase because I can't pass variables and it is unaware of the manager parameter in TTop. I also can't have a duplicate Create function in the TBase as far as I know.

SuperStrict

' FROM A BLITZMAX MOD ------------------------------------------------------
Type TManager
	Field afield:String = "manager"
End Type

Type TBase
	Field manager:TManager
	Field x:Int
	Field y:Int
End Type

' FROM A PROGRAM USING THE MOD ---------------------------------------------
Type TTop Extends TBase
	Field name:String
	Function Create:TTop(name:String, manager:TManager)
		Local top:TTop = New TTop
		top.name = name
		top.manager = manager  ' <--- I want code such as this in TBase
		Return top
	End Function
End Type

Local m:TManager = New TManager
Local o:TTop = TTop.Create("name",m)

Print o.manager.afield



matibee(Posted 2010) [#2]
Add an "Initialise" function to TBase that takes a TManager argument. In a debug build you can assert that field:manager is valid in every TBase method that uses it, to ensure "Initialise" has been called..

Type TBase
	Field manager:TManager
	Field x:Int
	Field y:Int

        Method Initialise ( man:Tmanager )
             manager = man
        End Method

        Method DoSomethingThatUsesManager ()
             Assert manager <> Null Else "TBase not initialised!"


        End Method

End Type



Imphenzia(Posted 2010) [#3]
Thanks matibee.

I'll give that a try. I was hoping to avoid a second step/call as I want to keep it as simple as possible outside the mod but I suppose a second call is required.


Czar Flavius(Posted 2010) [#4]
Have a look at this post: http://blitzbasic.com/Community/posts.php?topic=92618

By defining a series of constructors, for each type, and calling the constructor of the parent class, you can chain them together in a flexible way and make sure each class type sets itself up properly.

It's ok to put in your mod's documentation that a particular type should be created using a specific function or method; then it's up to the user to follow your instructions. If the user can't be bothered to use your mod properly, then he will mess up anyway, won't he?

Don't waste too much time worrying about these things and certainly don't waste clock cycles on checking it. At the end of the day, there is nothing to stop a user from just creating a type with New on its own and not setting anything up. It's a limitation of Blitz but not the end of the world.