June 16, 2024, 07:10:16 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Declaring variables

Started by LarryMc, January 07, 2006, 06:27:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

Have the following snipets( I left out stuff that has no bearing on problem):

Quoteclass tlmWindow : window
{
   declare OnMenuPick(int nID),INT;
}
Quoteglobal sub main()
{
   tlmWindow winMain,winIMAGE,winEDIT,winHIDE ;
   winMain.Create(0,0,640,600,.................................,NULL)
   winIMAGE.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winIMAGE.ShowWindow(SWHIDE);
   winEDIT.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winEDIT.ShowWindow(SWHIDE);
   winHIDE.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winHIDE.ShowWindow(SWHIDE);
   do
   {
      wait();
   }
   until winMain.run = 0;
   return;
}

QuotetlmWindow::OnMenuPick(int nID),int
{
ÂÃ,  ÂÃ,  switch nID
ÂÃ,  ÂÃ,  {
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  case 1:
   winIMAGE.ShowWindow(SWRESTORE);
   winEDIT.ShowWindow(SWHIDE);
   winHIDE.ShowWindow(SWHIDE);
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  case 2:
   winIMAGE.ShowWindow(SWHIDE);
   winEDIT.ShowWindow(SWRESTORE);
   winHIDE.ShowWindow(SWHIDE);
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  case 3:
   winIMAGE.ShowWindow(SWHIDE);
   winEDIT.ShowWindow(SWHIDE);
   winHIDE.ShowWindow(SWRESTORE);

   //quit the program
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, case 5:
   Destroy();
ÂÃ,  ÂÃ,  return false;
}

It won't compile.ÂÃ, 
I get an "unknown variable" for each of the winXXX in the Case statements and an "unknown class" for each variable error.

The showwindow stements work correctly in the main sub.

I know the problem is how/where I declaring the 3 windows but I don't know how to fix it.

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

You're declaring them in the main() subroutine, so they aren't visible to other subroutines. Move them outside of any subroutine and it'll work.

LarryMc

Parker
I moved the line to just above the main sub

QuoteÂÃ,  tlmWindow winMain,winIMAGE,winEDIT,winHIDE ;
global sub main()
{

ÂÃ,  ÂÃ, winMain.Create(0,0,640,600,.................................,NULL)
ÂÃ,  ÂÃ, winIMAGE.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winIMAGE.ShowWindow(SWHIDE);
ÂÃ,  ÂÃ, winEDIT.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winEDIT.ShowWindow(SWHIDE);
ÂÃ,  ÂÃ, winHIDE.Create(20,40,600,480,...........................,winMain)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  winHIDE.ShowWindow(SWHIDE);
ÂÃ,  ÂÃ, do
ÂÃ,  ÂÃ, {
ÂÃ,  ÂÃ,  ÂÃ,  wait();
ÂÃ,  ÂÃ, }
ÂÃ,  ÂÃ, until winMain.run = 0;
ÂÃ,  ÂÃ, return;
}

It compiles now but I get one of those "Tell Uncle Bill Gates your problem" windows and the program doesn't run.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

You may have to override some other subroutines, like OnCreate and OnDestroy.

LarryMc

Here's the complete code:

QuoteDeclare Import, GetSysColor(index as int),int;

class tlmWindow : window
{
   // Overridden functions
   declare OnClose(),ÂÃ,  int;
   declare OnCreate(), int;
   declare OnMenuPick(int nID),INT;
   // Variables
   def run as int;

}
   tlmWindow winMain,winIMAGE,winEDIT,winHIDE ;
tlmWindow::OnClose(), int
{
   Destroy();
   run = 0;
   Return 0;
}

tlmWindow::OnCreate(), int
{
   run = 1;
   CenterWindow();
   Return True;
}


global sub main()
{
   winMain.Create(0,0,640,600,
      AWS_CAPTION|AWS_VISIBLE|AWS_BORDER|AWS_SYSMENU|AWS_AUTODRAW,
      0,"Second Site Image Editor",NULL);
   winMain.SetWindowColor(GetSysColor(15));
   menu m;
   //create the file menu   
   m.BeginMenu();
      m.MenuTitle( "&File");
      m.MenuItem( "&Load Image\tCtrl+L",0,1);
      m.Separator();
      m.MenuItem( "&Del Image\tCtrl+D",0,2);
      m.Separator();
      m.MenuItem( "Def Path &IN\tCtrl+I",0,3);
      m.MenuItem( "Def Path &Out\tCtrl+O",0,4);
      m.Separator();
      m.MenuItem( "&Quit\tCtrl+Q",0,5);
      m.MenuTitle( "&Config");
   m.EndMenu();
   winMain.SetMenu(m.Detach());
   //add our keyboard accelerators
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("L"),1);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("D"),2);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("I"),3);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("O"),4);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("Q"),5);

   winIMAGE.Create(20,40,600,480,AWS_VISIBLE|AWS_BORDER| AWS_CHILD,0,"",winMain);
   winIMAGE.SetWindowColor(GetSysColor(14));
   winIMAGE.ShowWindow(SWHIDE);

   winEDIT.Create(20,40,600,480,AWS_VISIBLE|AWS_BORDER| AWS_CHILD,0,"",winMain);
   winEDIT.SetWindowColor(GetSysColor(13));
   winEDIT.ShowWindow(SWHIDE);

   winHIDE.Create(20,40,600,480,AWS_VISIBLE|AWS_BORDER| AWS_CHILD,0,"",winMain);
   winHIDE.SetWindowColor(GetSysColor(12));
   winHIDE.ShowWindow(SWHIDE);

   do
   {
      wait();
   }
   until winMain.run = 0;
   return;
}

tlmWindow::OnMenuPick(int nID),int
{
   switch nID
   {
      case 1:
            //winIMAGE.ShowWindow(SWRESTORE);
            //winEDIT.ShowWindow(SWHIDE);
            //winHIDE.ShowWindow(SWHIDE);
      case 2:
            //winIMAGE.ShowWindow(SWHIDE);
            //winEDIT.ShowWindow(SWRESTORE);
            //winHIDE.ShowWindow(SWHIDE);
      case 3:
            //winIMAGE.ShowWindow(SWHIDE);
            //winEDIT.ShowWindow(SWHIDE);
            //winHIDE.ShowWindow(SWRESTORE);
      case 4:

      //quit the program
      case 5:
         Destroy();

      //close the current child
      case 200:

      //add a new child window;
      case 100:

   }
   //we return false to let the client handle other menu items from children.
   return false;
}

If I put the line I moved back inside the main sub the "Gates error" goes away and the program runs fine(I have the showwindows down in the case statement remarked out)
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

You may want to try changing the window variables to pointers, the only change that would require is using the -> operator instead of the . one. And make sure you use NEW and DELETE on them so that they are allocated and the constructors and destructors get called.

Other than that, I can't think of anything. But it seems to me that the constructors not getting called is the problem.

Ionic Wind Support Team

Classes in global scope do not work, and will not get there constructors/destructors called.

In your main window class have variables for each of the children your creating:

class tlmWindow : window
{
   // Overridden functions
   declare OnClose(),  int;
   declare OnCreate(), int;
   declare OnMenuPick(int nID),INT;
   // Variables
   def run as int;
tlmWindow *winIMAGE,*winEDIT,*winHIDE ;
}

Then create each one with NEW followed by Create.

Remember to delete them before your program ends.
Ionic Wind Support Team

LarryMc

Sorry Paul but this is all new to me.

You said:
QuoteThen create each one with NEW followed by Create.
Does create with NEW mean the following like in the middemo:
QuotewinIMAGE=new(tlmWindow,1);
   winEDIT=new(tlmWindow,1);
   winHIDE=new(tlmWindow,1);
If so, where does it go?

And does
Quotefollowed by Create
mean:
QuotewinIMAGE->Create(20,40,600,480,AWS_VISIBLE|AWS_BORDER| AWS_CHILD,0,"",winMain);
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

Yep, that's the right idea. The -> operator dereferences a pointer and also acts like the . operator.

The new() statements should go right at the beginning of the main() subroutine.

LarryMc

when I put the NEW statement at the beginning of the main sub(inside) I get compile errors saying
Quoteundefined variable, unable to autodefine
for each of the 3 statements
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

Did you declare the variables as pointers?
tlmWindow *winMain, *winEDIT, ...;

LarryMc

January 07, 2006, 10:20:34 PM #11 Last Edit: January 07, 2006, 10:22:15 PM by tlmccaughn
yes, in the class part at the beginni9ng of the program where Paul told me to.
(see Paul's last post above)
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

What do the offending lines say? I've never seen that error before, but it sounds like you're trying to use a variable that doesn't exist in some way.

Ionic Wind Support Team

In your Main subroutine after the main window is created.   Create them each after the 'new'.  Delete them after the main window is closed, or in the OnClose handler of the main window.

One of the principle methodologies of oop is that classes should manage their own interfaces, or child classes.  In other words your main window is a class, it should handle the creation or destruction of any child windows it uses. 

As a simple discussion of this idea think about the ubiquitous 'about' box.  It an about box an object of the application?  Not really it is usually created as the child of the main window. 

A class is, in essence, a UDT or struct as we call them.  Variables use only by that class should be member variables.  Resist the urge to use global or local variables if a class needs to access them.

As a general template for an applications main window lay it out like this:


class MainWindow:window
{
//Construction and destruction
   MainWindow();
   _MainWindow();
//Virtual overrides
   declare OnClose(),int;
   declare OnCreate(),int;
   ...etc
//Member variables
   AboutDialog *m_pAbout;
}

//the class contstructor
MainWindow::MainWindow()
{
     m_pAbout = NULL;
}

//the class destructor
MainWindow::_MainWindow()
{
    if(m_pAbout <> NULL)
        Delete m_pAbout;
}

//Handlers
MainWindow::OnCreate()
{
     m_pAbout = new(AboutDialog,1);
     m_pAbout->Create(.....,this);
     m_pAbout->AddControl(...)
     ...etc
}


Since the MainWindow class manages the about dialog you no longer have to worry about getting a pointer to it.  Lets say you respond to a menu item in your main window


MainWindow::OnMenuPick(int nID),int
{
    if(nID = IDM_ABOUT)
         m_pAbout->DoModal();
}


Think on it, you'll get the hang of it and eventually follow along naturally.  It is only when you try and force OOP to be procedural that you end up coding yourself into a circle.
Ionic Wind Support Team

LarryMc

Each of these lines generates it's own error message:
Quote
global sub main()
{

   winIMAGE=new(tlmWindow,1);
   winEDIT=new(tlmWindow,1);
   winHIDE=new(tlmWindow,1);
.............
}


the error messages are:
Quoteundefined variable, unable to autodefine -winIMAGE
undefined variable, unable to autodefine -winEDIT
undefined variable, unable to autodefine -winHIDE

and this is my class section

Quoteclass tlmWindow : window
{
   // Overridden functions
   declare OnClose(), ÂÃ, int;
   declare OnCreate(), int;
   declare OnMenuPick(int nID),INT;
   // Variables
   def run as int;

   tlmWindow *winIMAGE,*winEDIT,*winHIDE ;

}
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

Because they are member variables of the class, not local variables of the main subroutine.  read my previous reply.  And if you must do it  in the main subroutine then you need to tell the compiler where those variables are

winMain.Create(...)
winMain.winIMAGE=new(tlmWindow,1);
winMain.winEDIT=new(tlmWindow,1);
winMain.winHIDE=new(tlmWindow,1);



Ionic Wind Support Team

Parker

Oh, aren't we getting far along ;D
Give me a minute and I'll correct it for you. The basic idea is since the variables are in the class, they're no longer global and the main subroutine can't access them. So you'll have to handle that in mainWindow::OnCreate().

I see Paul has beat me ;)

LarryMc

That got me over the hump Paul.

I'm trying to convert a fully functioning program in IBPro to Aurora without ever having done an C programming, no book, and no, written help.

I read your last post 10+ times, plus looking through all the example programs, and search the forum for various keywords(such as new, create, etc)

I understand it is an aggravation to you to have to stop what you are doing to answer such mundane questions from someone who is totally ignorant of some of the concepts.

I guess my best bet is to just dog it off until there is more information available that I will have access to without being such a bother.

Keep up the good work and I'll be back.

BTW, thanks for all your help Parker.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Parker

January 07, 2006, 10:41:36 PM #18 Last Edit: January 07, 2006, 10:47:49 PM by Parker
Alright, this works. I made a new child window class for you, since a child window doesn't need its own children of the same type.

Good luck with this OOP stuff, we're always here to help ;)

I'm not aggrivated, it's fun to answer these questions. And I hope Paul isn't either :)
Declare Import, GetSysColor(index as int),int;

class tlmWindow : window
{
   // Overridden functions
   declare OnClose(),  int;
   declare OnCreate(), int;
   declare OnMenuPick(int nID),INT;
   // Variables
   def run as int;
tlmwindow *winIMAGE, *winEDIT, *winHIDE;
}

class tlmChildWin : window
{
declare OnClose(),int;
// declare OnCreate(),int;
// declare OnMenuPick(int nID),int;
}

tlmWindow::OnClose(), int
{
   Destroy();
   run = 0;
   Return 0;
}

tlmWindow::OnCreate(), int
{
   run = 1;
   CenterWindow();
winIMAGE = new(tlmChildWin,1);
   winIMAGE->Create(20,40,600,480,AWS_BORDER| AWS_CHILD,0,"",this);
   winIMAGE->SetWindowColor(GetSysColor(14));

winEDIT = new(tlmChildWin,1);
   winEDIT->Create(20,40,600,480,AWS_BORDER| AWS_CHILD,0,"",this);
   winEDIT->SetWindowColor(GetSysColor(13));

winHIDE = new(tlmChildWin,1);
   winHIDE->Create(20,40,600,480,AWS_BORDER| AWS_CHILD,0,"",this);
   winHIDE->SetWindowColor(GetSysColor(12));

   Return True;
}

tlmChildWin::OnClose(),int
{
Destroy();
return 0;
}


global sub main()
{
tlmWindow winMain;
   winMain.Create(0,0,640,600,
      AWS_CAPTION|AWS_VISIBLE|AWS_BORDER|AWS_SYSMENU|AWS_AUTODRAW,
      0,"Second Site Image Editor",NULL);
   winMain.SetWindowColor(GetSysColor(15));
   menu m;
   //create the file menu   
   m.BeginMenu();
      m.MenuTitle( "&File");
      m.MenuItem( "&Load Image\tCtrl+L",0,1);
      m.Separator();
      m.MenuItem( "&Del Image\tCtrl+D",0,2);
      m.Separator();
      m.MenuItem( "Def Path &IN\tCtrl+I",0,3);
      m.MenuItem( "Def Path &Out\tCtrl+O",0,4);
      m.Separator();
      m.MenuItem( "&Quit\tCtrl+Q",0,5);
      m.MenuTitle( "&Config");
   m.EndMenu();
   winMain.SetMenu(m.Detach());
   //add our keyboard accelerators
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("L"),1);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("D"),2);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("I"),3);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("O"),4);
   winMain.AddAccelerator(FCONTROL|FVIRTKEY,ASC("Q"),5);

   do
   {
      wait();
   }
   until winMain.run = 0;
   return;
}

tlmWindow::OnMenuPick(int nID),int
{
   switch nID
   {
      case 1:
            winIMAGE->ShowWindow(SWRESTORE);
            winEDIT->ShowWindow(SWHIDE);
            winHIDE->ShowWindow(SWHIDE);
      case 2:
            winIMAGE->ShowWindow(SWHIDE);
            winEDIT->ShowWindow(SWRESTORE);
            winHIDE->ShowWindow(SWHIDE);
      case 3:
            winIMAGE->ShowWindow(SWHIDE);
            winEDIT->ShowWindow(SWHIDE);
            winHIDE->ShowWindow(SWRESTORE);
      case 4:

      //quit the program
      case 5:
         *(tlmWindow)this.run = 0;

      //close the current child
      case 200:

      //add a new child window;
      case 100:

   }
   //we return false to let the client handle other menu items from children.
   return false;
}

Ionic Wind Support Team

Quote
I understand it is an aggravation to you to have to stop what you are doing to answer such mundane questions from someone who is totally ignorant of some of the concepts.

Not at all aggravating.  In fact I am sure my answers help others that might not be as far along as you ;)

Parker...

   winIMAGE->ShowWindow(SWHIDE);

Is not needed if you remove the AWS_VISIBLE style from the create statement.
Ionic Wind Support Team

Parker

QuoteParker...

   winIMAGE->ShowWindow(SWHIDE);

Is not needed if you remove the AWS_VISIBLE style from the create statement.

I didn't see that, I'll change the above code. Thanks.

LarryMc

Works like a champ Parker! :o

Thanks again! ;D
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

Change OnClose to this an you'll be all set:


tlmWindow::OnClose(), int
{
   Destroy();
   if(winImage <> NULL)
        delete winImage;
   if(winEdit <> NULL)
        delete winEdit;
   if(winHide <> NULL)
        delete winHide;
   run = 0;
   Return 0;

}


Don't want memory leaks ;)

I always test the pointer before deletion.  And you should have a tlmWindow constructor set them to NULL.  It's good programming practice to get into.  When the worst case scenerio of an application failure happens, because of lack of memory for example, you won't be deleting a NULL pointer.

Ionic Wind Support Team

Bruce Peaslee

Quote from: Ionic Wizard on January 07, 2006, 10:45:05 PM
Not at all aggravating.ÂÃ,  In fact I am sure my answers help others that might not be as far along as you ;)

Exactly. I study these problems carefully and print the ones I find useful now or think may be useful when my skill has advanced.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles