TheSwamp
Code Red => .NET => Topic started by: Kerry 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.
-
The VBNet code :
If (Not trans Is Nothing) Then
trans.Dispose()
End If
SharpDevelop 2.0 translates to :
if ((!trans == null))
{
trans.Dispose();
}
This should be :
if (trans != null)
{
trans.Dispose();
}
Added :-
The following does not work because
'Autodesk.AutoCAD.DatabaseServices.Transaction' does not contain a definition for 'isNull'
if (!trans.isNull())
{
trans.Dispose();
}
Edit : piccy added :-
-
The VBNet code :
Dim objId As ObjectId
For Each objId In btr
Dim dynblkUtil As New DynamicBlock.DynBlockUtil(objId)
dynblkUtil.ListDynBlock()
Next
SharpDevelop 2.0 translates to :
ObjectId objId ;
foreach ( objId in btr)
{
DynamicBlock.DynBlockUtil dynblkUtil = new DynamicBlock.DynBlockUtil(objId);
dynblkUtil.ListDynBlock();
}
This should be :
foreach ( ObjectId objId in btr)
{
DynamicBlock.DynBlockUtil dynblkUtil = new DynamicBlock.DynBlockUtil(objId);
dynblkUtil.ListDynBlock();
}
edit : piccy added :-
-
The VBNet code :
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 :
Database db = HostApplicationServices.WorkingDatabase;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
Autodesk.AutoCAD.DatabaseServices.Transaction trans = tm.StartTransaction;
The last line should be :
Autodesk.AutoCAD.DatabaseServices.Transaction trans = tm.StartTransaction();
and if these using statement aliases are added to the code :
//
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
the code block could be revised to :
Database db = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager tm = db.TransactionManager;
AcDb.Transaction trans = tm.StartTransaction();
edit : piccy added :
-
The VBNet code :
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 :
BlockTable bt;
BlockTableRecord btr;
bt = tm.GetObject(db.BlockTableId, OpenMode.ForRead);
btr = tm.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForRead);
This should be :
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
edit : piccy added :-
-
The VBNet code :
If blkRefId.IsNull Then
' Nothing selected
Exit Try
End If
SharpDevelop 2.0 translates to :
if (blkRefId.IsNull)
{
// Nothing selected
break; // TODO: might not be correct. Was : Exit Try
}
This should be < To be confirmed > :
if (blkRefId.IsNull)
{
// Nothing selected
return; // TODO: To be confirmed
}
-
The VBNet code :
Dim blkRef As BlockReference
blkRef = tm.GetObject(blkRefId, OpenMode.ForWrite)
SharpDevelop 2.0 translates to :
BlockReference blkRef;
blkRef = tm.GetObject(blkRefId, OpenMode.ForWrite);
This should be :
BlockReference blkref = (BlockReference)tm.GetObject(blkRefId, OpenMode.ForWrite);
-
The VBNet code :
Catch
ed.WriteMessage("Oops!" & vbCrLf)
End Try
SharpDevelop 2.0 translates to :
catch {
ed.WriteMessage("Oops!" + Constants.vbCrLf);
}
We'll see a lot of this one ...
This should be :
catch {
ed.WriteMessage("Oops!" + "\n");
}
-
.. and this is where the definition came from :
-
The VBNet code :
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 :
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 :
public string Value {
get {
if (m_prop.Value == null)
{
return "";
}
else
{
return m_prop.Value.ToString() ;
}
}
-
I need help with this one .. any ideas .. ? ?
The VBNet code :
<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 :
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();
-
Need Help with this one too ...
VBNet ..
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.
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 ..
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
}
-
Just in case anyone doesn't know what I'm trying to convert ...
-
I haven't played with SharpDevelop, but from what
you posted, I'd say that its code translation needs
some work :-)
this:
(System.ComponentModel.ISupportInitialize)this.m_DataGrid.BeginInit();
should be:
((System.ComponentModel.ISupportInitialize)this.m_DataGrid).BeginInit();
-
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.
-
this:
(System.ComponentModel.ISupportInitialize)this.m_DataGrid.BeginInit();
should be:
((System.ComponentModel.ISupportInitialize)this.m_DataGrid).BeginInit();
Thanks Tony .. makes sense when I see it ..
I hadn't planned on doing much translation .. this was just an intellectual learning exercise .. it wasn't as efficient as I'd hoped, but chasing down the errors was sort of interesting.
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.
I'm a little stretched too Michael, had some study time Sunday morning, so ...
If others participate, it may turn into a reasonable resource.
-
VB....yuck! :) <runs away >
-
HEH, COME BACK HERE ... :lmao:
As you probably know, not all of the samples use C#, some are VBNet only ... so ... I was playing just to see how the translator worked.
.. I won't sully the forum with my opinions about the language ...
-
I've downloaded Instant C#: VB to C# Converter
from http://www.tangiblesoftwaresolutions.com/
The translation seems to be a little more complete.
To purchase the Standard Edition of Instant C# for $179, click < >.
Demo
Would you like to see Instant C# in action? We have a Demo Edition available which allows project conversions of up to 1000 lines and code or ASP.NET snippet conversions of up to 100 lines at a time in addition to offering dozens of sample code conversions. To request the Demo Edition of Instant C#, click < >.
Has anyone any other recommendations ? ?