Page 1 of 1

Sample demo code: Error messages in WinForms

PostPosted: Sat Jan 10, 2009 12:06 pm
by Csaba
Hi all,

In VB.NET using WinForms I usally try to catch a run time error with the information ASAP.
I. E almost all my methods has a try catch!

I have made a small demo program in Cobra showing the techniques. There are a number of issues in the program that I'm sure can be solved in a more elegant way. After a rewrite, I think it is a very useful demo program.

Which editor is best for Cobra today?

Regards
Csaba
Code: Select all
"""
Purpose: Demonstrates how to show a informative error message in the MessageBox
at runtime ASAP where unexpected error occured

Assumptions:
Compiled with cobra WinForms -r:System.Windows.Forms using Cobra v0.8

To improve:
    1 Problem to set up global constants
    2 Can't use the VB specific MsgBoxStyle.Exclamation, Cobra version is unknown
    3 Don't know if there are nicer Cobra reflection methods.
    4 Does Cobra have a meta method for the application title?
   
v 0.5 by Csaba Urbaniczky (CsabaU@HotMail.com)
"""

use System.Windows.Forms # does not work
class Demo
    def doFail
        try
            MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
            zero=0 # since Cobra is to clever 1/0 can't be used!
            MessageBox.show( "This will never be shown: [1/zero] ")
        catch e as Exception # Cobra errror message here
            MessageBox.show('EXCEPTION in [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]: \n [e.message] ', Program.appTitle, MessageBoxButtons.OK)

class DemoForm
    inherits Form

    def init
        .text = ' [Program.appTitle]  Click me!'
        listen .click, ref .handleClick # Cobra set up specific

    def handleClick(sender as Object, args as EventArgs)
        try
            # this will only be executed if no error above
            zero=0 # since Cobra is to clever 1/0 can't be used!
            MessageBox.show("This will never be shown: [1/zero] ")
        catch e as Exception # .NET error message here
            MessageBox.show('EXCEPTION in [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]: \n [e.message] ', Program.appTitle, MessageBoxButtons.OK)

class Program
    var _appTitle as String is shared #  = "WinFormsDemo"      I'm not able to initialize and set ' is shared'  in single line
   
    #get appTitle from _appTitle is shared  does not compile   # This since Cobra has no Const
   
    def appTitle as String is shared # work around for the non-working property
        return _appTitle
       
    def main is shared
        _appTitle  = "WinFormsDemo" # set here in the work around
        try
            MessageBox.show('Hello from [Program.appTitle].[System.Reflection.MethodBase.getCurrentMethod.name]', Program.appTitle,  MessageBoxButtons.OK ) # Missing: MsgBoxStyle.Exclamation
            Application.run(DemoForm())
            x as dynamic = Demo
            x.doFail # This object will show a Cobra specific run time error - not the .NET specific
        catch e as Exception # .NET error message here
            MessageBox.show('EXCEPTION in [Program.appTitle].[System.Reflection.MethodBase.getCurrentMethod.name]:\n [e.message]', Program.appTitle, MessageBoxButtons.OK ) # ,  MsgBoxStyle.Exclamation

Re: Sample demo code: Error messages in WinForms

PostPosted: Sat Jan 10, 2009 4:34 pm
by Charles
On Windows, I like UltraEdit, but note that I'm not necessarily saying it "the best". See also: EditorSupport

My approach to the problem you describe is a bit more intense. Check out these:

http://cobra-language.com/trac/cobra/browser/cobra/trunk/Source/ObjectExplorer-WinForms.cobra

http://cobra-language.com/trac/cobra/browser/cobra/trunk/Source/CobraMain-ObjectExplorer-WinForms.cobra

You can see them in your HowToInstallFromSource.

Combined with Cobra's -dst flag, you can browse every stack frame and for each stack frame you can inspect every argument and local variable. Furthermore, since the objects are still "live", you can drill down through their properties as deep as you want.

I use this when developing Cobra itself.

Btw I think you're looking for MessageBoxIcon.Exclamation which is defined by WinForms. I've use it in C# apps.

Re: Sample demo code: Error messages in WinForms

PostPosted: Sun Jan 11, 2009 8:50 am
by Csaba
Hi,

Thank you for the help. I have made a new version to show error information in MessageBox, see the code below.
In Cobra v0.8, it seems to be an issue that Cobra in Cobra objects shows the first error cause as: UnknownMemberException

It is nice that Cobra has correct scope for variables in Try-Catch blocks

Regards
Csaba

Code: Select all
"""
Purpose: Demonstrates how to show error information iin .NET MessageBox
  at runtime ASAP where unexpected error occured.

Shows also that a variable declared in the try block is valid in the catch block.

Assumptions:
Compiled with the command:
cobra WinFormsDemo -r:System.Windows.Forms
using Cobra v0.8

TODO, To improve:
    1 Cobra seems to skip the first catch block level in its own objects!
    2 Problem to set up global constants -> Cobra 1.0

Comment: Don't know if there are nicer specific Cobra meta (=reflection) methods.
   
v 0.7 by Csaba Urbaniczky (CsabaU@HotMail.com)
"""
use System.Windows.Forms
use System.Reflection

class ExInfo # help methods to show error messages in MessagaBox
   # The longer and more informative version
   def show(ex as Exception, typeName as String, methodName as String) is shared
      MessageBox.show('EXCEPTION catched in [typeName].[methodName]: \n [ex.toString] ', Path.getFileName(Application.executablePath), MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
   
   # The short form
   def short(ex as Exception, typeName as String, methodName as String) is shared
      MessageBox.show('EXCEPTION catched in [typeName].[methodName]: \n [ex.message] ', Path.getFileName(Application.executablePath), MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

class Demo
   def doFail
      try
         MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
         zero=0 # since Cobra is to clever 1/0 can't be used!
         MessageBox.show( "This will never be shown: [1/zero] ")
      catch ex as Exception # This ctach is not executed!
         ExInfo.show(ex, .getType.name, MethodBase.getCurrentMethod.name)

class MainForm
   inherits Form

   def init # =.ctor
      MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
      listen .load, ref .mainFormLoad # Cobra set up specific
      listen .click, ref .handleClick # Cobra set up specific
      
   def mainFormLoad(sender as System.Object, eArgs as System.EventArgs)
      try
         MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
         .text = ' [Program.appTitle]  Click me!'
      catch ex as Exception # This is never executed
         ExInfo.show(ex, .getType.name, MethodBase.getCurrentMethod.name)

   def handleClick(sender as Object, eArgs as EventArgs)
      try
         MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
         o as dynamic = Demo
         o.doFail # This object will fail
   
         #zero=0 # since Cobra is to clever 1/0 can't be used!
         #MessageBox.show("This will never be shown: [1/zero] ")
      catch ex as Exception # This is never executed
         ExInfo.show(ex, .getType.name, MethodBase.getCurrentMethod.name)

class Program # = Application.productName
   #get appTitle = Path.getFileName(Application.executablePath)  is shared   does not compile correctly in v0.8 ( This since Cobra has no Const)
   
   def appTitle as String? is shared # work around for the non-working property
      return Path.getFileName(Application.executablePath) # not Application.productName = 'Program'

   def main is shared
      try
         xInSide ="declared inside of the try block"
         MessageBox.show('Hello from [Program.appTitle].[System.Reflection.MethodBase.getCurrentMethod.name]', Program.appTitle,  MessageBoxButtons.OK ) # Missing: MsgBoxStyle.Exclamation
         Application.run(MainForm())
         zero=0 # since Cobra is to clever 1/0 can't be used!
         MessageBox.show("This will never be shown: [1/zero] ")
      catch ex as Exception # .NET error message here
         MessageBox.show( 'Scope check: xInSide is [xInSide] and in useable in catch block', "One more thing done right in Cobra:")
         ExInfo.show(ex, 'Program', MethodBase.getCurrentMethod.name)