shortcut for adding some value to existing int/double parameter.

shortcut for adding some value to existing int/double parameter.

amir.aroesti
Advocate Advocate
1,362 Views
2 Replies
Message 1 of 3

shortcut for adding some value to existing int/double parameter.

amir.aroesti
Advocate
Advocate

hi,

 

some really basic question i cant find an solution.

normally on programming you can add value to parameter by += / -=.

 

when it related to works with "Set" parameter method it a little bit different because the method expected to specific value and that force me to use the content below  :

 

temp = E.LookupParameter("Area").AsDouble();
E.LookupParameter("Area").Set(temp+new value);

i will expected for something like that:

E.LookupParameter("Area").Set(+=new value);

 

another question.

looking for the right way to reset some parameter without looping. 

 

foreach(Wall zeroWall in walls)
{
   zeroWall.LookupParameter("Area").Set(0)
}

is that the right way to do that?

 

Thnaks 

 

0 Likes
Accepted solutions (1)
1,363 Views
2 Replies
Replies (2)
Message 2 of 3

TripleM-Dev.net
Advisor
Advisor

Hi,

 

For setting/retrieving parameters from large amounts of elements I usually use the definition to retrieve the parameter and Set/Get the value, unless it's a builtinparameter.

 

Never tested if retrieving the parameter with LookupParameter or the Parameter Definition would have a huge impact. Personally I wouldn't expect it to, but first checking if the parameter HAS to be set could...

 

But, from the parameter Definition I wouldn't have to check the Parameter value type etc..

This would only be needed once when retrieving the definition (for example on the First element or so)

 

The function lacks error checking, LookupParameter could return a NULL value, parameter value type, is there another parameter with the same name, readonly state etc...

 

- Michel

0 Likes
Message 3 of 3

RPTHOMAS108
Mentor
Mentor
Accepted solution

You can create extension methods if you would prefer such code shortcuts. As an example in VB with the below example I can add the value 21 directly to a parameter i.e.:

 

 

 Element.LookupParameter("Some number")?.Add(21)

 

 

 

    <Extension()>
    Public Sub Add(P As Parameter, D As Double)
        'Internal units expected
        Select Case P.StorageType
            Case StorageType.Double
                Dim D1 As Double = P.AsDouble
                P.Set(D1 + D)
            Case StorageType.Integer
                Dim D1 As Integer = P.AsInteger
                P.Set(D1 + CInt(D))
            Case StorageType.String
                Dim D1 As Double
                Dim S As String = P.AsString
                If Double.TryParse(S, D1) = True Then
                    P.Set(CStr(D1 + D))
                Else
                    'I could concatenate but seems better to thow exception.
                    Throw New NotSupportedException("Current parameter has storage type string but it's value doesn't represent a double.")
                End If
            Case Else
                Throw New NotSupportedException("Parameter storage type must be double, integer or string.")
        End Select
    End Sub

 

 

I tend not to do such things personally because you get further away from the intentions of the API and consistency in understanding what is required when things go wrong. Obviously if you are doing the same checks over and over then there are efficiencies to be had. For example in the above you can mitigate for different storage types where numbers are not added but should you?  Likewise I'm adding the '?' to only add the value if the parameter is found but probably knowing the parameter does not exist is an important result that would not be observed with the '?'.