Author Topic: Translations ...  (Read 8747 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Translations ...
« on: September 16, 2006, 07:59:40 PM »
I thought we could use this thread for notes about translations between VB and C#  [ or vice versa ]

added Note :
I'm currently using VS2005 for coding and SharpDevelop 2.0 for translations.

« Last Edit: September 16, 2006, 08:09:47 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #1 on: September 16, 2006, 08:07:41 PM »
The VBNet code :
Code: [Select]
If (Not trans Is Nothing) Then
    trans.Dispose()
End If

SharpDevelop 2.0 translates to :
Code: [Select]
if ((!trans == null))
{
     trans.Dispose();
}

This should be :
Code: [Select]
if (trans != null)
{
     trans.Dispose();
}

Added :-
The following does not work because
'Autodesk.AutoCAD.DatabaseServices.Transaction' does not contain a definition for 'isNull'
Code: [Select]
if (!trans.isNull())
{
     trans.Dispose();
}

Edit : piccy added :-
« Last Edit: September 16, 2006, 10:00:47 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #2 on: September 16, 2006, 08:17:00 PM »
The VBNet code :
Code: [Select]
Dim objId As ObjectId

For Each objId In btr
    Dim dynblkUtil As New DynamicBlock.DynBlockUtil(objId)
    dynblkUtil.ListDynBlock()
Next

SharpDevelop 2.0 translates to :
Code: [Select]
ObjectId objId ;
foreach ( objId in btr)
{
    DynamicBlock.DynBlockUtil dynblkUtil = new DynamicBlock.DynBlockUtil(objId);
    dynblkUtil.ListDynBlock();
}

This should be :
Code: [Select]

foreach ( ObjectId objId in btr)
{
    DynamicBlock.DynBlockUtil dynblkUtil = new DynamicBlock.DynBlockUtil(objId);
    dynblkUtil.ListDynBlock();
}

edit : piccy added :-

« Last Edit: September 16, 2006, 10:18:04 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #3 on: September 16, 2006, 08:30:51 PM »
The VBNet code :
Code: [Select]
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
            Dim trans As Autodesk.AutoCAD.DatabaseServices.Transaction = tm.StartTransaction

SharpDevelop 2.0 translates to :
Code: [Select]
    Database db = HostApplicationServices.WorkingDatabase;
    Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
    Autodesk.AutoCAD.DatabaseServices.Transaction trans = tm.StartTransaction;

The last line should be :
Code: [Select]

Autodesk.AutoCAD.DatabaseServices.Transaction trans = tm.StartTransaction();

and if these using statement aliases  are added to the code :
Code: [Select]
//
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;

the code block could be revised to :
Code: [Select]
Database db = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager tm = db.TransactionManager;
AcDb.Transaction trans = tm.StartTransaction();

edit : piccy added :
« Last Edit: September 16, 2006, 09:56:51 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #4 on: September 16, 2006, 08:39:36 PM »
The VBNet code :
Code: [Select]
                Dim bt As BlockTable
                Dim btr As BlockTableRecord
                bt = tm.GetObject(db.BlockTableId, OpenMode.ForRead)
                btr = tm.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForRead)

SharpDevelop 2.0 translates to :
Code: [Select]
BlockTable bt;
 BlockTableRecord btr;
 bt = tm.GetObject(db.BlockTableId, OpenMode.ForRead);
 btr = tm.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForRead);

This should be :
Code: [Select]

 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
 BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);


edit : piccy added :-
« Last Edit: September 16, 2006, 10:22:14 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #5 on: September 16, 2006, 09:11:36 PM »
The VBNet code :
Code: [Select]
               If blkRefId.IsNull Then
                    ' Nothing selected
                    Exit Try
                End If

SharpDevelop 2.0 translates to :
Code: [Select]
if (blkRefId.IsNull)
{
// Nothing selected
break; // TODO: might not be correct. Was : Exit Try
}

This should be < To be confirmed > :
Code: [Select]

if (blkRefId.IsNull)
{
// Nothing selected
return; // TODO: To be confirmed
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #6 on: September 16, 2006, 09:23:17 PM »
The VBNet code :
Code: [Select]
                Dim blkRef As BlockReference
                blkRef = tm.GetObject(blkRefId, OpenMode.ForWrite)

SharpDevelop 2.0 translates to :
Code: [Select]
BlockReference blkRef;
blkRef = tm.GetObject(blkRefId, OpenMode.ForWrite);

This should be :
Code: [Select]

    BlockReference blkref = (BlockReference)tm.GetObject(blkRefId, OpenMode.ForWrite);
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #7 on: September 16, 2006, 09:35:36 PM »
The VBNet code :
Code: [Select]
            Catch
                ed.WriteMessage("Oops!" & vbCrLf)
            End Try

SharpDevelop 2.0 translates to :
Code: [Select]
    catch {
ed.WriteMessage("Oops!" + Constants.vbCrLf);
    }

We'll see a lot of this one ...
This should be  :
Code: [Select]

    catch {
ed.WriteMessage("Oops!" + "\n");
    }

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #8 on: September 16, 2006, 09:47:10 PM »
.. and this is where the definition came from :
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #9 on: September 16, 2006, 10:12:59 PM »
The VBNet code :
Code: [Select]
        Public Property Value() As String
            Get
                If m_prop.Value Is Nothing Then
                    Return ""
                Else
                    Return m_prop.Value.ToString
                End If
            End Get

SharpDevelop 2.0 translates to :
Code: [Select]
public string Value {
get {
if (m_prop.Value == null)
{
return "";
}
else
{
return m_prop.Value.ToString;
}
}

We'll see a lot of this one ...
This should be  :
Code: [Select]

 public string Value {
get {
if (m_prop.Value == null)
{
return "";
}
else
{
return m_prop.Value.ToString() ;
}
}
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #10 on: September 16, 2006, 10:32:23 PM »
I need help with this one .. any ideas .. ? ?

The VBNet code :
Code: [Select]
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.m_bnOk = New System.Windows.Forms.Button()
            Me.m_Label = New System.Windows.Forms.Label()
            Me.m_DataGrid = New System.Windows.Forms.DataGrid()
            CType(Me.m_DataGrid, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()

SharpDevelop 2.0 translates to :
Code: [Select]
private void InitializeComponent()
{
this.m_bnOk = new System.Windows.Forms.Button();
this.m_Label = new System.Windows.Forms.Label();
this.m_DataGrid = new System.Windows.Forms.DataGrid();
(System.ComponentModel.ISupportInitialize)this.m_DataGrid.BeginInit();


this.SuspendLayout();


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #11 on: September 16, 2006, 10:58:00 PM »
Need Help with this one too ...
VBNet ..
Code: [Select]
        Public Property Value() As String
            Get
                If m_prop.Value Is Nothing Then
                    Return ""
                Else
                    Return m_prop.Value.ToString
                End If
            End Get
           
            Set(ByVal Value As String)
                If m_prop.ReadOnly = True Then
                    MessageBox.Show("Property is read-only")
                Else
                    Try
                        Select Case m_prop.PropertyTypeCode
                            Case Is = PropTypeCode.RTNONE
                            Case Is = PropTypeCode.RTSHORT
                                m_prop.Value = System.Convert.ToInt16(Value)
                            Case Is = PropTypeCode.RTLONG
                                m_prop.Value = System.Convert.ToInt64(Value)
                            Case Is = PropTypeCode.RTREAL
                                m_prop.Value = System.Convert.ToDouble(Value)
                            Case Is = PropTypeCode.RTENAME
                            Case Is = PropTypeCode.RTPOINT
                            Case Is = PropTypeCode.RT3DPOINT
                            Case Is = PropTypeCode.RTSTR
                                m_prop.Value = Value
                        End Select
                    Catch e As System.Exception
                        MessageBox.Show(e.Message)
                    End Try
                End If
            End Set
        End Property


C# ..
all the red lines display this error
Cannot implicitly convert type 'DynamicBlock.PropTypeCode' to 'short'. An explicit conversion exists (are you missing a cast?)

I could cast to short (Int16) ie
(int16)PropTypeCode.RTSHORT:
but it doesn't feel right for all of them .. 'cause I'm a little brain dead.

Quote
        public string Value
        {
            get
            {
                if (m_prop.Value == null)
                {
                    return "";
                }
                else
                {
                    return m_prop.Value.ToString();
                }
            }
            set
            {
                if (m_prop.ReadOnly == true)
                {
                    MessageBox.Show("Property is read-only");
                }
                else
                {
                    try
                    {
                        switch (m_prop.PropertyTypeCode)
                        {
                            case PropTypeCode.RTNONE:                               
                                break;
                            case PropTypeCode.RTSHORT:
                                m_prop.Value = System.Convert.ToInt16(value);
                                break;
                            case PropTypeCode.RTLONG:
                                m_prop.Value = System.Convert.ToInt64(value);
                                break;
                            case PropTypeCode.RTREAL:
                                m_prop.Value = System.Convert.ToDouble(value);
                                break;
                            case PropTypeCode.RTENAME:
                                break;
                            case PropTypeCode.RTPOINT:
                                break;
                            case PropTypeCode.RT3DPOINT:
                                break;
                            case PropTypeCode.RTSTR:
                                m_prop.Value = value;
                                break;
                        }
                    }
                    catch (System.Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            }
        }

These are the enumerators ..
Code: [Select]
   
public enum PropTypeCode
    {
        RTREAL = 40,
        //kDouble or AcDb::kDxfReal ''5001
        RTSHORT = 70,
        // kShort or AcDb::kDxfInt16 ''5003
        RTSTR = 1,
        //kString or AcDb::kDxfText ''5005
        //The following are not used in Rio
        RTNONE = -9999,
        //kNone or AcDb::kDxfInvalid ''5000
        RTPOINT = 10,
        //kPoint2d or AcDb::kDxfXCoord ''5002
        RTENAME = 91,
        //kOldId or AcDb::kDxfInt32+1 ''5006
        RT3DPOINT = 11,
        //kPoint3d or AcDb::kDxfXCoord+1 ''5009
        RTLONG = 90
        //kLong or AcDb::kDxfInt32 ''5010
    }
« Last Edit: September 16, 2006, 11:01:48 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Translations ...
« Reply #12 on: September 16, 2006, 11:37:18 PM »
Just in case anyone doesn't know what I'm trying to convert ...

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Translations ...
« Reply #13 on: September 17, 2006, 02:40:28 AM »
I haven't played with SharpDevelop, but from what
you posted, I'd say that its code translation needs
some work :-)

Code: [Select]
this:

   (System.ComponentModel.ISupportInitialize)this.m_DataGrid.BeginInit();

should be:

  ((System.ComponentModel.ISupportInitialize)this.m_DataGrid).BeginInit();



MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Translations ...
« Reply #14 on: September 17, 2006, 06:54:58 PM »
I don't have time to participate these days but just want to give a nod your way Kerry -- this thread (translations, equivalencies etc) is a good idea.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst