Variable Length Array as Property of a Class

Variable Length Array as Property of a Class

parikhnidi
Advocate Advocate
853 Views
5 Replies
Message 1 of 6

Variable Length Array as Property of a Class

parikhnidi
Advocate
Advocate

Hi Friends,

 

Recently I started programming in .NET and trying to bid farewell to VBA. I have a class that defines a plate with specified diameter, thickness and unknown number of slots (having orientation, width and depth property) as shown in the sketch below. I am not sure how to implement this in the class. I did ask similar question in the past and got a good responses. However, I am not able to understand that, as I never ventured in this territory.

 

Can anyone help me in implementation of this kind of property in classes.

PlateWSlots.PNG

Thanks

Nimish

0 Likes
Accepted solutions (3)
854 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor
Accepted solution

Unlike VBA, where you only have dynamic array and Collection as built-in type of holding of collection of objects of variable length, .NET has many classes of collection, such as List<T>, Dictionary<T, T>...

 

I would imagine you could have 2 classes, simplified like:

 

public class Slot

{

  public double Width { set; get; }

  public double Depth { set; get; }

  public double Angle { set; get; }

  //....

}

 

public class Plate

{

  public double Radius { set; get; }

  public double Thickness { set; get; }

  public List<Slot> Slots { get; } = new List<Slot>();

  // more properties/methods...

}

 

At the moment when you know how many Slots are needed, you simply create slot instances and add them to Slots property. Of course you can remove one or more slots from the Slots collection as needed.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

parikhnidi
Advocate
Advocate

Hi Norman,

 

Thanks for your inputs. I have developed class definition as follows. However, I am not able to frame kind of syntext required to implement Set / Get property which is a list of class.

Public Class Slot
   Private mSlotIndex As Integer
   Private mWidth As Double
   Private mDepth As Double
   Private mAngle As Double
   Public Property SlotIndex As Integer
       Set(value As Integer)
           mSlotIndex = value
       End Set
       Get
           Return mSlotIndex
       End Get
   End Property
   Public Property Width As Double
       Set(value As Double)
           mWidth = value
       End Set
       Get
           Return mWidth
       End Get
   End Property
   Public Property Depth As Double
       Set(value As Double)
           mDepth = value
       End Set
       Get
           Return mDepth
       End Get
   End Property
   Public Property Angle As Double
       Set(value As Double)
           mAngle = value
       End Set
       Get
           Return mAngle
       End Get
   End Property
End Class
Public Class InternalPlate
   Private mDiameter As Double
   Private mSlots As List(Of Slot)
   Public Property Diameter As Double
       Set(value As Double)
           mDiameter = value
       End Set
       Get
           Return mDiameter
       End Get
   End Property
   '=========
   'How to implement Set / Get properties of the class Slot here in VB.NET?
End Class

 Can you provide further directions.

 

Nimish

0 Likes
Message 4 of 6

dgorsman
Consultant
Consultant
Accepted solution

You need an instance of the Slot object to be initialized and/or set the properties.  Declaring a List<Slot> doesn't create any actual slot objects, it just tells the compiler what that List can contain.  So you create each instance of Slot that you need, and feed them into the list in turn.

 

I'd recommend a constructor on both classes, so you can create the new object with appropriate starting values.  So something like this (C#):

 

public Slot (double width, double length)
{
   mWidth = width;
   mDepth = depth;
}

 

Angle and index might be better served being set after all slots are created, so I've left those out.

 

The InternalPlate object could have a method which creates the required number of Slot objects.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor
Accepted solution

It is no different from other properties:

 

Public Property Slots As List(Of Slot)

  Set (value As List(Of Slot)

    mSlots=value

  End Set

  Get

    Return mSlots

  End Get

End Property

 

However, I usually tend to create the collection member as an empty one when the class is instantiated and expose it as read-only property:

 

Public Class InternalPlate

   ...

  Private mSlots As New List(Of Slot)()

  ...

  ...

  Public ReadOnly Property Slots As List(Of Slot)

    Get

      Return mSlots

    End Get

  End Property

  ...

  ...

End Class

 

BTW, if you just started doing .NET programming (AutoCAD .NET API, or generic .NET programming), at this point of time, choosing VB.NET over C# might be a bad choice. Not only for AutoCAD .NET programming, you would be able to find more sample code/resources in C# than VB.NET, but also C# would be more "future-proof", because MS has already stopped advancing VB.NET as .NET development language, which might not impact AutoCAD .NET API programming as long as AutoCAD .NET API is still based on .NET framework, but what if AutoCAD moved on to support .NET 6 in future?

 

You might thought your VBA experience would make it easier to move to VB.NET. I'd say the opposite: moving to C# would help you to get rid of so many bad programming practices one might get used to while doing classical VB/VBA.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 6

parikhnidi
Advocate
Advocate

Thanks Norman and @dgorsman 

 

I had a very brief excursion in VB.NET and during that time, I wrote lots of helper functions to perform geometric calculations and standard drafting components even before working on actual objects. I think it will not be that difficult to convert those functions to C#, as there is lots of similarity in the code.

 

Good to know that I am not hung up with the platform that would become obsolete in future.

 

Nimish

0 Likes