Object based programming: Declaring classes in ilogic

Object based programming: Declaring classes in ilogic

Thomas.Long
Advocate Advocate
3,391 Views
9 Replies
Message 1 of 10

Object based programming: Declaring classes in ilogic

Thomas.Long
Advocate
Advocate

I'm attempting to write a class in Ilogic because I need to store multiple pieces of information associated with a single item. I attempted and Ilogic is fully capable of declaring a class. However, I'm having difficulty accessing properties inside of it. Because the properties are by default private I attempted to go through and write the standard get, let, and set procedures to access them. 

 

That's where the issues really started. Ilogic would throw the error that Get/Let/ Set were no longer supported and use the new property access syntax. So I tried looking it up and it said to properly define it in the block and end with End Get or End Set (Let seems to have been eliminated entirely. So I attempted that as well but then it throws the error "End Property must be preceded by a matching 'Property'" even though property is already declared in there. It also states that End Get/Set must be preceded by Get/Set even though if I add those back in it simply errors again, saying they are no longer supported.

 

It also continuously says declaration expected and that auto implement properties cannot have parameters.

 

 

 

SyntaxEditor Code Snippet

Public Class ZBracketBreak

    Private pStart As Double
    Private pLength As Double
    Private pAnchor As Boolean
    
    Public Property Start() As Double
        Start = pStart
    End Property
    
    Public Property Start(Value As Double)
         pStart = Value
    End Property
    
    
    Public Property PropertyLength() As Double
         Length = pLength
    End Property
    
    Public Property Length(Value As Double)
         pLength = Value
    End Property
    
    
    Public Property Anchor() As Boolean
        Anchor = pAnchor 
    End Property
    
    Public Property Anchor(Value As Boolean)
        pAnchor = Value
    End Set
    
End Class

 

 Thank you,

Thomas Long

0 Likes
Accepted solutions (1)
3,392 Views
9 Replies
Replies (9)
Message 2 of 10

MechMachineMan
Advisor
Advisor

Very close. See example below:

 

Private opropertyval as Double = 7

Private Property SearchFactor6 As Double Get Return oSF6Val End Get Set(opropertyval as Double)
oSF6Val = opropertyval
End Set
End Property

 

or also:

http://stackoverflow.com/questions/1462673/vb-net-properties-public-get-private-set

https://support.microsoft.com/en-us/help/308230/how-to-define-and-use-properties-in-visual-basic-.ne...


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 10

Owner2229
Advisor
Advisor

Why don't you just make the properties public? o.O

 

Public Class ZBracketBreak
    Public pStart As Double
    Public pLength As Double
    Public pAnchor As Boolean
End Class
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 4 of 10

Thomas.Long
Advocate
Advocate

I was actually told that its not generally good practice to make properties to a class public (Though I admit to having little enough knowledge about the subject to truly understand why this is the case.) Still, it remains an option if I cannot get the private programming to work

0 Likes
Message 5 of 10

Owner2229
Advisor
Advisor
Accepted solution

I don't see a reason behind this. I'm using it as that (public) for some time in all my code (40+k lines in my current project) without any issue.

The only reason I would use Get/Set would be when I want to disable one or another, so I can only read or only write the value.

 

Also, you might want to add the properties to the builder, to shorten your code:

 

Public Class ZBracketBreak
    Public pStart As Double
    Public pLength As Double
    Public pAnchor As Boolean
    Public Sub New(pStart As Double, pLength As Double, pAnchor As Boolean)
        MyClass.pStart = pStart
        MyClass.pLength = pLength
        MyClass.pAnchor = pAnchor
    End Sub
End Class

Usage:

 

Dim ZBB As New ZBracketBreak(1.1, 1.2, True) 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 6 of 10

Thomas.Long
Advocate
Advocate

I don't actually know what a builder is, but I will add the sub you show to my class. I actually did a bit of reading and apparently the point of private members was to avoid having to track errors down in large chunks of code. My program isn't nearly so large so that shouldn't really be an issue. 

 

Edit: Ok, I see what you mean by the builder, its just a function to pass values for each variable in the class. I'll have to see how that will work with declaring an array of the class, though it seems to me that I can just initialize each value after already having created the array with the function you mentioned. 

 

Thank you,

Thomas Long

0 Likes
Message 7 of 10

Owner2229
Advisor
Advisor

The builder (the sub called "New") is used to create a new instance of the class.

The code I gave you is an overload of that sub (taking it's place), so it will result in the creation of new instance.

 

I'm saying this because if you'll not add all the properties to the builder:

 

Public Class ZBracketBreak
    Public pStart As Double
    Public pLength As Double
    Public pAnchor As Boolean
    Public Sub New(pStart As Double, pLength As Double)
        MyClass.pStart = pStart
        MyClass.pLength = pLength
    End Sub
End Class

 

And do this:

 

Dim ZBB As New ZBracketBreak(1.1, 1.2)
ZBB.pAnchor = True
ZBB = New ZBracketBreak(1.3, 1.4)

 

You'll lose the "pAnchor" value.

 

You can also add optional parameters and default values to the builder, e.g.:

 

Public Class ZBracketBreak
    Public pStart As Double
    Public pLength As Double
    Public pAnchor As Boolean
    Public Sub New(pStart As Double, pLength As Double, Optional pAnchor As Boolean = True)
        MyClass.pStart = pStart
        MyClass.pLength = pLength
        MyClass.pAnchor = pAnchor
    End Sub
End Class

 

Default value not included in the builder:

Public Class ZBracketBreak
    Public pStart As Double
    Public pLength As Double
    Public pAnchor As Boolean = True
    Public Sub New(pStart As Double, pLength As Double)
        MyClass.pStart = pStart
        MyClass.pLength = pLength
    End Sub
End Class

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 8 of 10

Thomas.Long
Advocate
Advocate

So If I did this as an array it would look like 


Dim ZBBArray(Counter - 1) As ZBracketBreak For i = 0 to Counter ZBBArray(i) = New ZBracketBreak(Value, Value, Value) Next
0 Likes
Message 9 of 10

Owner2229
Advisor
Advisor

Correct (almost). Let me know if you need some further help with the implementation of this class or smt.

Orange is the change I've made.

 

Dim ZBBArray(Counter - 1) As ZBracketBreak

For i = 0 to Counter - 1
       ZBBArray(i) = New ZBracketBreak(Value, Value, Value)
Next
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 10 of 10

sam
Advocate
Advocate

Thanks for explaining this mike. Really appreciate that. 

0 Likes