[Gambas-user] Using Character constants in Gambas

Caveat Gambas at ...1950...
Tue Mar 8 10:13:23 CET 2011


I would just create a Module called something like DialogFactory...

PUBLIC ErrorDialog AS Integer = 0
PUBLIC WarningDialog AS Integer = 1
PUBLIC InformationDialog AS Integer = 2
PRIVATE DefaultDialogType AS Integer = ErrorDialog
PRIVATE AllowedTypes AS Integer[] = [ErrorDialog, WarningDialog,
InformationDialog]

PUBLIC FUNCTION getDialog(dialogType AS Integer) AS VikramDialog
  
  DIM vDialog AS NEW VikramDialog
  DIM type AS Integer
  FOR EACH type IN AllowedTypes
    IF type = dialogType
      vDialog.dialogType = dialogType
      RETURN vDialog
    END IF
  NEXT
  PRINT "ERROR!  Unknown dialog type: " & dialogType
  vDialog.dialogType = DefaultDialogType
  RETURN vDialog
END


Using this code simply becomes:

  DIM aDialog AS VikramDialog
  aDialog = DialogFactory.getDialog(DialogFactory.InformationDialog)
  PRINT aDialog.dialogType
  aDialog = DialogFactory.getDialog(DialogFactory.WarningDialog)
  PRINT aDialog.dialogType
  aDialog = DialogFactory.getDialog(5)
  PRINT aDialog.dialogType


Of course, you can add other stuff to the getDialog function, like the
dialog name, title etc. etc...

What do you think?

Regards,
Caveat

On Mon, 2011-03-07 at 23:09 -0800, vikram wrote:
> Hi,
> 
> I am working on an application in which I reuse the same dialog for performing different tasks. I have a PUBLIC INTEGER variable(named dialogType) in the dialog's .class file. When creating instances of this dialog I set the DialogInstance.dialogType to the value(0,1,2,...) which indicates the purpose of the dialog. The value of DialogInstance.dialogType is used within the dialog to decide what to do with the user input.
> 
> I am using numeric values to indicate the dialog type. This isn't very elegant. I am looking for the Gambas equivalent of doing a #define CHARCONST value in C.
> 
> I have tried using a Collection.
> 
> DIM DialogType AS NEW Collection
> DialogType["SomeType0"] = 0
> DialogType["SomeType1"] = 1
> DialogType["SomeType2"] = 2
> DialogType["SomeType3"] = 3
> DialogType["SomeType4"] = 4
> 
> I am thinking of placing the collection's definition in a Module and then using it to replace the numeric constants:
> DIM DialogInstance as DialogName
> DialogInstance = NEW DialogName
> DialogInstance.dialogType = ModuleName.DialogType["SomeType0"]
> 'current code
> 'DialogInstance.dialogType = 0
> 
> Is there a better way of using character constants in Gambas?
> 
> Gambas 2.21/Debian 6
> 
> Thanks,
> Vikram Nair
> 
> 
> 
>       
> ------------------------------------------------------------------------------
> What You Don't Know About Data Connectivity CAN Hurt You
> This paper provides an overview of data connectivity, details
> its effect on application quality, and explores various alternative
> solutions. http://p.sf.net/sfu/progress-d2d
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user






More information about the User mailing list