Photo by nasrulekram
Are you not taking advantage of all the refactoring features available to you in WX? Are you wasting time maintaining code in more than one place? Today we are going to use conditional target-code to refactor code between WinDev and WebDev so the Open Source Default Manager Class can be shared between both environments and avoid having two copies to maintain.
The default manager was originally created for WinDev because that was my immediate need. However last week I ran into a need to use the default manager in a WebDev project. Since the entire WX product line shares a common code base, you have the ability to use the same code between the environments. However the last thing you want to do is start copying code from one environment to the another and end up having 2 or even 3 versions of your same wizbang feature to maintain. That is where code refactoring and the ability to share classes via the SCM comes in handy.
So the first step is to share the class in your WebDev Project via the SCM. So in the Project Explorer right click on classes and chose import from the SCM.
This brings up the SCM administartor and allows you to navigate to the WinDev Open Source project that you have probably already added to the SCM. Of course if you are not using the SCM or don’t have the Open Source project in your SCM these steps would be different, but don’t worry, the real point of this article is conditional target-code, which will come in handy regardless of how you decide to share your code between projects.
After importing the class into your WebDev project you will find that there is an compile error in the GetWindowName method.
That is because Internal Windows are not available in WebDev. But don’t fret target conditional target-code will come to your rescue. Conditional target-code is code that is only included in the project based on the type of product. Many programming environments have a similar feature, general called something along the lines of Compiler Directives.
So in WebDev you don’t want the Window Name you want the page name and since there are no internal windows you don’t want that code included at all. This is where you might be temped to just make a quick change to the code and keep a separate copy for WebDev. Sounds like a good idea until 2 years from now when you have forgotten all about it, and you make some enhancements to the WinDev Default Manager class only to find out that they are not reflected in WebDev and now you have to go make the changes in the WebDev version as well and before long you have random bugs in one or the other.
So instead setup conditional target-code by choosing, Conditional Target-Code->New Area from the Code menu.
The next screen ask which enviroment that code is for, select WinDev.
This creates a separate section of code for the target copy and paste the WinDev specific code into this section.
Notice how the heading for this section shows all the different possible targets for a WinDev project. You can create conditional code specific to a particular target, such as Linux or 32 bit windows, by clicking on the Advanced button on the Describing a Conditional Target-Code window and select the specific targets on the next screen.
The specific targets can also be tweaked by clicking on the edit icon in the code header and selecting the targets from the popup menu.
For this purpose you don’t need to get more specific than WinDev and WebDeb so there is no need to go through the extra step but it sure is good to know it is there for future reference.
The next step is to create the WebDev specific code, so again use the code menu to insert conditional target-code but this time select WebDev and place the following line of code in the created section.
WindowName = MyPage..Name
When you are finished the GetWindowName method should look like this
And that is all there is to it, you now have a class that you can share between WinDev and WebDev and only have one copy to maintain!
Be sure to go over to wxLive.us and watch the Uncle Pete’s corner, webinar that is the companion to this article. Along with show the above, at the end is some bonus material on some other refactoring tips and tricks.
Uncle Pete’s Corner is weekly webinar on all things WX every Friday 7:15 AM CST (5:15 PST), to watch the recorded version of this webinar, many other WX related webinars or to watch future ones live go to wxLive.us