March 28, 2024, 01:03:22 PM

News:

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


Optional Parameters

Started by Steven Picard, August 16, 2007, 09:45:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steven Picard

August 16, 2007, 09:45:11 AM Last Edit: August 16, 2007, 10:07:51 AM by Steven Picard
I'm trying to write some libraries of code in Aurora to make my development for work easier.  I am working on a file handling class that mimics the FileSystemObject found in the scrrun.dll. I am testing out some optional parameters but I must be doing something wrong because it keeps crashing when I call the function without the parameter.  Here's some example code to illustrate what I mean:

class TestClass
{
//the class constructor
declare TestClass();
//the class destructor
declare _TestClass();

// Print Message with optional parameter
declare PrintMessage(opt string Message);
// Waits for Key Press
declare PressKey();

// Get Property
declare getMessage(), string;
// Set Property
declare setMessage(string Msg);

// Member variable for GET and SET Message property
string m_defaultMessage;
}

// Constructor
TestClass::TestClass()
{
m_defaultMessage = "Display Default Message";
}

// Destructor
TestClass::_TestClass()
{
}

// Property GET for Message.
TestClass::getMessage(), string
{
return m_defaultMessage;
}

// Property SET for Message.
TestClass::setMessage(string Msg)
{
m_defaultMessage = Msg;
}

// Output Message to Console.
TestClass::PrintMessage(opt string Message)
{
if(Message=null) // <- I've tried different tests here but without success.
Message = getMessage();

writeln(Message);
writeln("\n");
}

// Wait for key press.
TestClass::PressKey()
{
writeln("Press any key close\n");
while GetKey() = "";
}

// Application entry point.
sub main()
{
// Instantiate Class.
TestClass test;
// Print a message passed through optional parameter.
test.PrintMessage("My Test");
// Now try default parameter set in Constructor.
test.PrintMessage();
// Now use the set property to set default message.
test.setMessage("My Other Test");
// Print message using now updated default message.
test.PrintMessage("");
// Wait for key press.
test.PressKey();
}


I know I must be over looking something obvious.  :P

Ionic Wind Support Team

Two choices.  Give it a default value, or check for NULL with a pointer.   When you specify an optional string parameter and don't supply the parameter,or a default, then the compiler simply inserts a NULL into the parameter list.

Paul
Ionic Wind Support Team

Steven Picard

Thanks. Paul.

I just realized I should have set it to pointer.  It was worthwhile asking anyways since I didn't know I could assigned default values.  Stupid, I know, but it's the first time working with optional parameters in Aurora.

Ionic Wind Support Team

Yup just set it in the delcare

declare PrintMessage(opt string Message = "NOMSG");
Ionic Wind Support Team

Steven Picard

August 16, 2007, 10:48:35 AM #4 Last Edit: August 16, 2007, 10:54:42 AM by Steven Picard
For those interested here is the updated class:

class TestClass
{
//the class constructor
declare TestClass();
//the class destructor
declare _TestClass();

// Print Message
declare PrintMessage(opt string *Message);
// Waits for Key Press
declare PressKey();

// Get Property
declare getMessage(), string;
// Set Property
declare setMessage(string Msg);

// Member variable for GET and SET Message property
string m_defaultMessage;
}

// Constructor
TestClass::TestClass()
{
m_defaultMessage = "Display Default Message";
}

// Destructor
TestClass::_TestClass()
{
}

// Property GET for Message.
TestClass::getMessage(), string
{
return m_defaultMessage;
}

// Property SET for Message.
TestClass::setMessage(string Msg)
{
m_defaultMessage = Msg;
}

// Output Message to Console.
TestClass::PrintMessage(opt string *Message)
{
string output;

// Is the message missing?
// Yes. Use what ever is set in the default message.
if(Message=null) { output = getMessage(); }
else if(Message) { output = *(string)Message; } // No. Output the passed parameter.

writeln(output);
writeln("\n");
}

// Wait for key press.
TestClass::PressKey()
{
writeln("Press any key close\n");
while GetKey() = "";
}

// Application entry point.
sub main()
{
// Instantiate Class.
TestClass test;
// Print a message passed through optional parameter.
test.PrintMessage("My Test");
// Now try default parameter set in Constructor.
test.PrintMessage();
// Now use the set property to set default message.
test.setMessage("My Other Test");
// Print message using now updated default message.
test.PrintMessage();
// Print a message passed through optional parameter.
test.PrintMessage(test.getMessage());
// Wait for key press.
test.PressKey();
}


I changed it to use pointers so I can check for NULLs properly.  If I need to figure something out in Aurora I usually use a simple class like this for testing.

Of primary interest are these lines:
declare PrintMessage(opt string *Message);

and
// Output Message to Console.
TestClass::PrintMessage(opt string *Message)
{
   string output;
   
   // Is the message missing?
   // Yes. Use what ever is set in the default message.
   if(Message=null) { output = getMessage(); }   
   else if(Message) { output = *(string)Message; } // No. Output the passed parameter.

   writeln(output);
   writeln("\n");
}


You may have noticed I type cast the parameter Message like this *(string)Message.  That's not necessary in this case but I left it in.