Separate a mixed Type
BlitzMax Forums/BlitzMax Beginners Area/Separate a mixed Type
| ||
Mycode.bmx file getting large I been trying to separate MyAppMixType to an separate file but because MyAppMixType is controlling MyApp I can't Can I somehow move MyAppMixType to a separate file? File1/File2.bmx only have Type's that don't call MFrame/MyApp or MyAppMixType MyApp.bmx: Import "File1.bmx" Import "File2.bmx" Type MyApp Extends wxFrame Method onInit() MFrame = Self ... End Method Method MFunction1() ... End Method Method MFunction2() ... End Method Method MFunction3() ... End Method End Type Type MyAppMixType Method Function1() MFrame.MFunction2() ... End Method Method Function2() MFrame.MFunction3() ... End Method Method Function3() MFrame.MFunction1() ... End Method End Type Secoundary Question: Is it possible to separate Method/Function to another file within a Type and include those Method/Function that is located in another file? Type MyApp Extends wxFrame Method onInit() MFrame = Self ... End Method Method MFunction1() ... End Method include File3.bmx ' remove MFunction2 and MFunction3 to another file End Type File3.bmx Method MFunction2() ... End Method Method MFunction3() ... End Method |
| ||
With NG you could use interfaces, with vanilla you need to do work with "type x extends xbase" The basic idea is to use a basic type, defining functions which only know about what is defined there (or in imports done in that file). In your main application, you then extend this type and override the functions to fill them with the actions requiring knowledge of structures not defined in the file of the "xbase". Ok, here is some pseudo code: file1.bmx Import wx-modules Import other-things-needed Type MyAppBase extends wxFrame Global _instance:MyAppBase Method onInit() MFrame = Self ... _instance = self End Method Method MFunction1() 'do something not requiring knowledge of structures in "main.bmx" End Method Method MFunction2() abstract 'another possibility End Type file2.bmx Import file1.bmx 'now able to access MyAppBase-instance.MFunction1() Type MyAppMixType Method Function1() MyAppBase._instance.MFunction1() ... End Method End Type main.bmx Import file1.bmx 'know it knows about "MyAppBase" Import file2.bmx 'can now access MyAppMixType Type MyApp extends MyAppBase 'override Method onInit() 'now MyAppBase._instance is holding a "MyApp" type, no longer 'a potentially old MyAppBase _instance = self End Method 'override Method MFunction1() Super.MFunction1() 'call the method defined in "type MyAppBase" 'do something which is only known in the main-file End Method End Type Again - the basic approach is: - file1 contains "base implementation" of a type ("TMyTypeBase") - file2 contains "import file1" and is able to access the methods and fields defined in "TMyTypeBase" - main file contains "import file1" and "import file2" and contains the real implementation ("TMyType extends TMyTypeBase") the extending type inherits the functions/methods/properties of the base class. If you then create a "mytype:TMyType" you can pass this to a function waiting for a param "param:TMyTypeBase". When this function now calls a method (must be defined in "TMyTypeBase") it actually calls that method/function which is defined in "TMyType" (if there is no overriding method defined there, it uses the one of the parent/super class: TMyTypeBase. bye Ron |
| ||
Thanks Derron! I realize when make a new try separate the Types that: MyAppMixType is created in MyApp, and I can't do that! (they controlling each other) I don't really grasp my problem, but I think I need to start separate all function in MyApp that control MyAppMixType to separate MyAppMixType I get tired only thinking of it! We see when I am strong enough to make another try! |
| ||
I do not see where is your problem. Like I tried to explain: - create a "TMyTypeBase" which has "Method Myfunction()" (empty, not doing something) - store it in a "mytypebase.bmx" - all files wanting to call "TMyType.Myfunction()" now import "mytypebase.bmx" (so they could call "TMyTypeBase.Myfunction()" - either give the instance as function param (TOtherClass: Function UseIt(mytype:TMyTypeBase)) - or store it somewhere (a global in "mytypebase.bmx" or a field property in "TOtherClass) in your application - import "mytypebase.bmx" - create a "TMyType extends TMyTypeBase" - local mytype:TMyType = new TMyType - call functions awaiting "type:TMyTypeBase": Totherclass.UseIt(mytype) Just try it ... the reason that it works is the "extends" keyword. If you pass an instance of "TMyType" (extended from "TMyTypeBas") it will call the methods/functions defined in "TMyType" rather than "TMyTypeBase". bye Ron |