My computer crashed when I was almost done so I got mad and typed this quickly and went from memory which is a very very bad thing. Someone might want to double check it.
First Example Converted
In VB you 'Import' the namespace and in C# you 'use' the namespace.
So
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
becomes
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
Here is the KeepStraightOverrule class
Public Class KeepStraightOverrule
Inherits TransformOverrule
'We want to change how an AttributeReference responds to being
' transformed (moved, rotated, etc.), so we override its
' standard TransformBy function.
Public Overrides Sub TransformBy(ByVal entity As Entity,
ByVal transform As Matrix3d)
'Call the normal TransformBy function for the attribute
' reference we're overruling.
MyBase.TransformBy(entity, transform)
'We know entity must be an AttributeReference because
' that is the only entity we registered the overrule for.
Dim attRef As AttributeReference = entity
'Set rotation of attribute reference to 0 (horizontal)
attRef.Rotation = 0.0
End Sub
End Class
In VB when a class inherits from another the first line in the class is 'Inherits' then the the name of the class.
In C# you use ':'
In the TransformOverrule class it has a method TransformBy marked as
Public Overridable Sub TransformBy(ByVal entity As Entity, ByVal transform As Matrix3d)
or in C#
public virtual void TransformBy(Entity entity, Matrix3d transform)
Overridable = virtual
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
The Overidable keyword specifies that a property or procedure can be overridden by an identically named property or procedure in a derived class.
A derived class is one that inherits another class.
Also a Sub is method that 'does something' and does not return anything so in C# a 'void' method.
Some of you might wonder how is entity passed with ByVal(Meaning By Value) and in the body of the method the entity is changed and the original entity is changed. When a custom class is used in an argument it is always passed ByRef(By Reference) meaning its memory address not a copy of the value is passed and any changes made in the method will change the original object.
As Kerry mentioned if Option Strict is on then one of the things that will cause a compile error is
Implicit narrowing conversions
Since AttributeReference is derived from Entity and Entity does not contain all the methods as AttributeReference it is considered a narrowing conversion(A conversion with Data loss)
Another example would be
Dim d As Double = 45.25
Dim i As Integer = d
With option strict On it will throw a error with it off the compiler will do the conversion.
As Kerry mentioned you must explicitly do the conversion in C#
AttributeReference attRef = entity as AttributeReference;
MyBase(VB) and base(C#) call the 'base'(The class you are inherting from) class method.
Last couple things as my computer crashed and I am lazy and do not feel like retyping but in VB to make a null check you check if 'Is Nothing', and static = Shared.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace HorizontalAttributesCSharp
{
public class Commands
{
private static KeepStraightOverrule myOverrule;
[CommandMethod("KeepStraight")]
public static void ImplementOverrule()
{
if (myOverrule == null)
{
myOverrule = new KeepStraightOverrule();
Overrule.AddOverrule(RXClass.GetClass(typeof(AttributeReference)), myOverrule, false);
}
Overrule.Overruling = true;
}
}
public class KeepStraightOverrule : TransformOverrule
{
public override void TransformBy(Entity entity, Matrix3d transform)
{
base.TransformBy(entity, transform);
AttributeReference attRef = entity as AttributeReference;
attRef.Rotation = 0.0;
}
}
}
**********************Edit************************************
Also AddOverule first arguement is a System.Type to read more for VB
GetTypeand for C#
typeof