[CONNECT .NET] What does ElementId (ref long value) Do?

The .NET ElementId constructor requires a long value.  But that value is passed by ref.  To me, ref implies that the value may be changed inside the ElementId.  Why is ref in the constructor?

Parents
  • Hi ,

    Passing Values and Pointers across various API languages by Reference (ref) have a number of sometimes subtle and yet important use case differences, like these common ones to consider:

    Where similar to Jan's code below is ensuring that the referenced long/ulong value is provided (initialized) though not modified (internally).  If you were to attempt to compile your code where ElementId was not initialized it would be immediately flagged as an error.

    HTH,
    Bob



  • Where similar to Jan's code below is ensuring that the referenced long/ulong value is provided (initialized) though not modified (internally).  If you were to attempt to compile your code where ElementId was not initialized it would be immediately flagged as an error.

    That doesn't answer the question.

    If the constructor were ElementId (long id) then there is no way for the value not to be initialised because it's a value type.  Adding ref confuses bears of little brain like me.

    Background — Text Style ID

    I posted this question because I was attempting to find the text style name used by a text element. TextStyleId is a uint32 but DgnTextStyle.GetById wants an ElementId, so we have to convert ...

    public string GetTextStyleName (TextBlock textBlock)
    {
        TextBlockProperties props = textBlock.GetProperties();
        DgnTextStyleApplyable tsa = props.AsDgnTextStyleApplyable();
        // Assign uint to ulong OK
        ulong styleId = tsa.TextStyleId;
        if (0 != styleId)
        {
            // Pass ulong byref OK, but can't pass uint32 byref because it's the wrong type
    	ElementId id = new ElementId(ref styleId);
    	using (DgnTextStyle style = DgnTextStyle.GetById(id, textBlock.GetDgnModel().GetDgnFile()))
    	{
    	    return null == style ? null : style.Name;
    	}
        }
        return null;
    }
    

    This begs the question: Why is DgnTextStyleApplyable.TextStyleId a uint when DgnTextStyle.GetById wants an ElementId?

    Because of that wacky constructor for the .NET ElementId, one can't use the TextStyleId directly because the required pass-by-reference is illegal.  A consequence is that it's necessary to construct an ElementId only to pass its value to another function.

    In C++, we can assign a uint to an ElementId, which is a typedef for int64. No classes are involved.

     
    Regards, Jon Summers
    LA Solutions

Reply
  • Where similar to Jan's code below is ensuring that the referenced long/ulong value is provided (initialized) though not modified (internally).  If you were to attempt to compile your code where ElementId was not initialized it would be immediately flagged as an error.

    That doesn't answer the question.

    If the constructor were ElementId (long id) then there is no way for the value not to be initialised because it's a value type.  Adding ref confuses bears of little brain like me.

    Background — Text Style ID

    I posted this question because I was attempting to find the text style name used by a text element. TextStyleId is a uint32 but DgnTextStyle.GetById wants an ElementId, so we have to convert ...

    public string GetTextStyleName (TextBlock textBlock)
    {
        TextBlockProperties props = textBlock.GetProperties();
        DgnTextStyleApplyable tsa = props.AsDgnTextStyleApplyable();
        // Assign uint to ulong OK
        ulong styleId = tsa.TextStyleId;
        if (0 != styleId)
        {
            // Pass ulong byref OK, but can't pass uint32 byref because it's the wrong type
    	ElementId id = new ElementId(ref styleId);
    	using (DgnTextStyle style = DgnTextStyle.GetById(id, textBlock.GetDgnModel().GetDgnFile()))
    	{
    	    return null == style ? null : style.Name;
    	}
        }
        return null;
    }
    

    This begs the question: Why is DgnTextStyleApplyable.TextStyleId a uint when DgnTextStyle.GetById wants an ElementId?

    Because of that wacky constructor for the .NET ElementId, one can't use the TextStyleId directly because the required pass-by-reference is illegal.  A consequence is that it's necessary to construct an ElementId only to pass its value to another function.

    In C++, we can assign a uint to an ElementId, which is a typedef for int64. No classes are involved.

     
    Regards, Jon Summers
    LA Solutions

Children
No Data