Forums

problem converting c# code to cobra

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

problem converting c# code to cobra

Postby RIGHT_THEN » Fri Jan 15, 2010 1:40 pm

GentleMen,
I was trying to convert c# file to cobra in order to learn it but got stuck
with following problems
1) a class is deriving from an abstract class but is unable to access the members in the abstract class
that are declared protected and even some public members from another abstract class which is
implemented in someclass in the hirarchy of the assemblies been referenced. it works well with c# compiler ,
compiles and program runs well. but converting the same code to Cobra gives errors as to cannot
find the Variables in question or suggesting other variables.below i am providing the problematic snippets
to keep precise
this is the abstract class i am talking about whose member "helper"
cobra is not allowing me to reference
First citation
Code: Select all
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using System;

namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
{
    public abstract class AbstractProjectOptionPanel : AbstractOptionPanel, ICanBeDirty
    {
        protected ConfigurationGuiHelper helper; //cobra says another member with same name
                          "InitializeHelper" is there use it
        protected MSBuildBasedProject project;

        protected AbstractProjectOptionPanel();

        public bool IsDirty { get; set; }

        public event EventHandler IsDirtyChanged;

        protected void InitializeHelper(); // cobra advocates for using this
        public override bool StorePanelContents();
    }
}

c# the above mentioned members are called like this and pose no problem
i am merely converting that to cobra which is shown below
Code: Select all
protected virtual ConfigurationGuiBinding BindString(string control, string property, TextBoxEditMode textBoxEditMode)
      {
         return helper.BindString(control, property, textBoxEditMode);
      }
      
      protected virtual ConfigurationGuiBinding BindEnum<T>(string control, string property) where T : struct
      {
         return helper.BindEnum<T>(control, property);
      }
///another snippet from some func
Get<TextBox>("projectFolder").Text = project.Directory;
Get<TextBox>("projectFile").Text = Path.GetFileName(project.FileName);
Get<TextBox>("projectFile").ReadOnly = true;


cobra code
Code: Select all
use System
use System.IO
use System.Windows.Forms
# i tried this thinking maybe this outside namespace needs mention of its assembly
# but it did not work
use ICSharpCode.SharpDevelop.Gui.OptionPanels from ICSharpCode.SharpDevelop
use ICSharpCode.SharpDevelop.Project from ICSharpCode.SharpDevelop

namespace Testing

   
   class ApplicationSettingsPanel 
      is public
      inherits AbstractProjectOptionPanel
      
      #i had to convert the name to small letter first
      #because Cobra would not allow otherwise
      def bindString(control as String, property as String,textBoxEditMode as TextBoxEditMode ) as    ConfigurationGuiBinding is protected , virtual
         # cobra error Cannot find "helper". There is a member named "initializeHelper" with a
         # similar name that you can access with a leading period (".").   
         return helper.bindString(control, property, textBoxEditMode)
            
      def bindEnum<of T>(control as String ,property as String) as ConfigurationGuiBinding is protected, virtual
         where T must be struct
         #same above error
         return helper.bindEnum<of T>(control, property)
      
         #below i think i can
         # make Get<of to get<of because it is a methood in it class
         # and since it is coming from a class i am deriving from
         # i can even do .get<of
         # but i have tried all the option Corba compiler says it cannot find it
         # cobra error Cannot find "project".
         # cobra error Cannot find type for "Get<of>".
         Get<of TextBox>("projectFolder").text = project.directory
         Get<of TextBox>("projectFile").text = Path.getFileName(project.fileName)
         Get<of TextBox>("projectFile").readOnly = true



Get<of> is located in its class like this
Code: Select all
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace ICSharpCode.SharpDevelop.Gui.XmlForms
{
    public abstract class XmlUserControl : UserControl
    {
        protected XmlLoader xmlLoader;

        public XmlUserControl();

        public Dictionary<string, Control> ControlDictionary { get; }

        public T Get<T>(string name) where T : Control; /// <---
        protected void SetupFromXmlResource(string resourceName);
        protected void SetupFromXmlStream(Stream stream);
        protected virtual void SetupXmlLoader();
    }
}



few more questions please:-
1) is below conversion okay?
SetupFromXmlStream(typeof(CompilingOptionsPanel).Assembly.GetManifestResourceStream(resource));
.setupFromXmlStream(sharp'typeof(CompilingOptionsPanel)'.Assembly.getManifestResourceStream(resource))
i get if c# expression of type "(int)Var1" will become "Var1 to int" but what
about typeof as in above context




Thankyou
RIGHT_THEN
RIGHT_THEN
 
Posts: 99

Re: problem converting c# code to cobra

Postby Charles » Fri Jan 15, 2010 8:12 pm

-- If C# is invoking a method with:
Foo<of T>(a, b)

then try this in Cobra:
.foo<of T>(a, b)

Notice the use of the "." which is short for "this."

-- If you write:
Foo.bar

then that invokes the shared/static method "bar" on "Foo". If instead, you want to access the type of Foo, ask it for its (meta)type:
trace Foo.getType.name

Or you can assign a type to a local var and use that:
type = Foo
trace type.name

-- Make sure you are using the latest Cobra from source as there are some bug fixes.

I didn't have much time today. If I missed something, let me know.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: problem converting c# code to cobra

Postby hopscc » Sat Jan 16, 2010 4:22 am

So your cobra code block should be something like:
use System
use System.IO
use System.Windows.Forms

use ICSharpCode.SharpDevelop.Gui.OptionPanels from ICSharpCode.SharpDevelop
use ICSharpCode.SharpDevelop.Project from ICSharpCode.SharpDevelop

namespace Testing

class ApplicationSettingsPanel is public inherits AbstractProjectOptionPanel

def bindString(control as String, property as String,textBoxEditMode as TextBoxEditMode) as ConfigurationGuiBinding is protected, virtual
return .helper.bindString(control, property, textBoxEditMode)
# above same as return this.helper.bindString....

def bindEnum<of T>(control as String ,property as String) as ConfigurationGuiBinding is protected, virtual
where T must be struct
return .helper.bindEnum<of T>(control, property)

#.... (below probably not in same class)
.get<of TextBox>("projectFolder").text = .project.directory # or = this.project.directory
.get<of TextBox>("projectFile").text = Path.getFileName(.project.fileName)
.get<of TextBox>("projectFile").readOnly = true


You dont have to have the 'use' directives for SharpDevelop but if you dont you will need to specify the fully qualified name for each reference
(full namespace and type,...) to items from that namespace and you will
explicitly need to tell the compiler you are using that/those assemblies ( -ref:ICSharpCode.SharpDevelop).
lines like
class ApplicationSettingsPanel  is public inherits AbstractProjectOptionPanel
would become something like
class ApplicationSettingsPanel is public inherits ICSharpCode.SharpDevelop.Gui.OptionPanels.AbstractProjectOptionPanel

(this is not greatly different from C#).

This in C#
Code: Select all
SetupFromXmlStream(typeof(CompilingOptionsPanel).Assembly.GetManifestResourceStream(resource));

becomes this in cobra
.setupFromXmlStream(CompilingOptionsPanel.getType.Assembly.getManifestResourceStream(resource))


Leading Upcase method names in C# (assemblies) are referenced from cobra with leading lowcase letter,
As an example you've seen, method 'InitializeHelper' written in C# is accessed from cobra as '.initializeHelper ( or 'this.initializeHelper'')
(simplistically) Methods from base classes need a leading 'this.' or (better) just '.' - (its a little more complicated than just this as leading underscore tagged names are also treated specially)
The compilers helpfulness in listing alternatives is limited to method names not properties/attributes/instance vars
(re 'helper' vs 'initializeHelper')

The wiki has a Porting C# code to cobra article .. looks like we need a similar 'referencing C# code from cobra'
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: problem converting c# code to cobra

Postby Charles » Sat Jan 16, 2010 5:33 am

The compiler's helpfulness in listing alternatives include members in general, not just methods. Here is an example with a property:
class X

get foobar as int
return 2

def main
trace .foob
# error: Cannot find a definition for "foob" in "this" whose type is "X".
# There is a member named ".foobar" with a similar name.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: problem converting c# code to cobra

Postby RIGHT_THEN » Sat Jan 16, 2010 11:23 am

Thanks Mr.chuck and Mr.hopscc

for the replies

but the problem still stands

and sorry for the code mangling
next time i will upload the files.

when you say to download the latest compiler isnt it the one on
the main page and otherwise i went into wiki and clicked on to
browse source. i couldnt find any packet to download. i tried downloading the
zip after giving some rev numbers but only few file get downloaded
like Expr etc..

i will check tackling the rev numbers again
maybe my head isnt working right.

Thankyou
RIGHT_THEN
RIGHT_THEN
 
Posts: 99

Re: problem converting c# code to cobra

Postby Charles » Sat Jan 16, 2010 7:17 pm

This page on the source code:
http://cobra-language.com/source/

Leads to this page with instructions on how to get and install it:
http://cobra-language.com/trac/cobra/wiki/HowToInstallFromSource

It's fairly easy if you follow the instructions. A script does most of the work.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: problem converting c# code to cobra

Postby hopscc » Sun Jan 17, 2010 12:53 am

The compiler's helpfulness in listing alternatives include members in general, not just methods


That not 100% correct ( for referencing assemblies) otherwise RIGHT_NOWS example would have given both 'helper' and 'initializeHelper'

It probably is right for inline compiled cobra source code as per your example.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: problem converting c# code to cobra

Postby Charles » Sun Jan 17, 2010 3:38 am

Then I would consider that a bug. Can we cook up a case that uses the standard library? I took a shot with TextWriter, but got all the suggestions I was expecting.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 49 guests

cron