Quantcast
Channel: MicroStation Programming Forum - Recent Threads
Viewing all articles
Browse latest Browse all 7260

RE: tag definition or tagset

$
0
0

Again, one more note ;-)

I have a problem with your code, because it's a bit ugly and dirty in my opinion, especially because it breaks DRY rule (Dont Repeat Yourself): To do the same thing (to rewrite tag value) in every case selection is wrong, regardless it's one line of the code.

Also I don't like to use select case construction, but I don't know your code and its functionality enough to evaluate whether is really necessary or not.

To change a tag value can be "centralized" to one function and it can be even implemented using different approaches. There are two examples, the first one requires to specify also TagSet name, but would be typically faster; the second one works with all tags attached to an element, but is slower (because it iterates through all attached tags.

Private Sub SetTagValue1(el As Element, tagSetName As String, tagName As String, tagValue As Variant)

    On Error GoTo ErrorHandler

    Dim tSet As TagSet
    Set tSet = ActiveDesignFile.TagSets(tagSetName)

    Dim tagEl As TagElement
    Set tagEl = el.GetTag(tSet, tagName)

    tagEl.Value = tagValue
    tagEl.Rewrite
    Exit Sub

ErrorHandler:
End Sub

Private Sub SetTagValue2(el As Element, tagName As String, tagValue As Variant)

    On Error GoTo ErrorHandler

    Dim tags() As TagElement
    tags = el.GetTags

    Dim i As Long

    For i = LBound(tags) To UBound(tags)
        If (tags(i).TagDefinitionName = tagName) Then
            tags(i).Value = tagValue
            tags(i).Rewrite
            Exit Sub
        End If
    Next i

ErrorHandler:
End Sub

An advantage in my opinion is that they can be used for any element with any parameters, if a tag or tagset does not exist or an element is e.g. non-graphical, nothing happens, an error is trapped inside the function.

Using them, the first one or the second, in the code you can use something like

SetTagValue1 el, "dati_punto_6", "2_nord", "10"
SetTagValue2 el, "22_nord", "10"

which I am sure is nicer and better readable code (which brings simpler maintainability).

With regards,

  Jan


Viewing all articles
Browse latest Browse all 7260

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>