Author Topic: How to populate "TypedValu​e" data type using loops.  (Read 1919 times)

0 Members and 1 Guest are viewing this topic.

vnsharifi

  • Guest
How to populate "TypedValu​e" data type using loops.
« on: April 26, 2011, 10:20:21 AM »
I have to add 80 Xdata to some objects. How to populate them within a loop. This what I did for 11 of them . But
I am pretty sure there is a way to do it in a loop which I don't know . Any help from experts would be appreciated.

Code: [Select]


Public Sub SetXData2()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        ' Ask the user to select an entity
        ' for which to set XData
        Dim opt As New PromptEntityOptions(vbLf & "Select entity: ")
        Dim res As PromptEntityResult = ed.GetEntity(opt)
        If res.Status = PromptStatus.OK Then
            Dim tr As Transaction = doc.TransactionManager.StartTransaction()
            Using tr
                Dim obj As DBObject = tr.GetObject(res.ObjectId, OpenMode.ForWrite)
                AddRegAppTableRecord1("Hello")
                Dim rb As New ResultBuffer(New TypedValue(1001, "Hello"), _
                New TypedValue(1000, "test1"), New TypedValue(1000, "test2") _
                , New TypedValue(1000, "test3"), New TypedValue(1000, "test4") _
                , New TypedValue(1000, "test5"), New TypedValue(1000, "test6") _
                , New TypedValue(1000, "test7"), New TypedValue(1000, "test8") _
                , New TypedValue(1000, "test9"), New TypedValue(1000, "test10") _
                , New TypedValue(1000, "test11"))
                obj.XData = rb
                rb.Dispose()
                tr.Commit()
            End Using
        End If
    End Sub


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7320
  • AKA Daniel
Re: How to populate "TypedValu​e" data type using loops.
« Reply #1 on: April 26, 2011, 10:34:46 AM »
Code: [Select]
        ResultBuffer buf = new ResultBuffer();

        for (int idx = 0; idx < 80; idx++)
        {
          buf.Add(new TypedValue(1000,string.Format("test{0}",idx)));
        }

Retired

kaefer

  • Swamp Rat
  • Posts: 572
Re: How to populate "TypedValu​e" data type using loops.
« Reply #2 on: April 26, 2011, 10:36:45 AM »
I have to add 80 Xdata to some objects. How to populate them within a loop.

You want to use the ResultBuffer.Add() method. Keep in mind that XData is seriously limited in the total amount of data (~16 K), consider using an Extension Dictionary instead.

An alternative for stuffing a ResultBuffer with varying amounts of data via params array was presented way back by Tony Tanzillo in his Command Line tool.

Jeff H

  • Needs a day job
  • Posts: 6113
Re: How to populate "TypedValu​e" data type using loops.
« Reply #3 on: April 26, 2011, 01:50:50 PM »
Another vote for 1
please please please please please please please please please let 1 get the most votes

vnsharifi

  • Guest
Re: How to populate "TypedValu​e" data type using loops.
« Reply #4 on: April 26, 2011, 02:24:31 PM »
Code: [Select]
        ResultBuffer buf = new ResultBuffer();

        for (int idx = 0; idx < 80; idx++)
        {
          buf.Add(new TypedValue(1000,string.Format("test{0}",idx)));
        }

[/quote
]



Thanks Assume. I used Online C# converter tools ,  to convert your code  to  vb. Is this in  C# ? I don't know how to make it work ? Sorry.It is my fault. Could somebody help me to incorporate Assume comment in my Code . Thanks for help.

Jeff H

  • Needs a day job
  • Posts: 6113
Re: How to populate "TypedValu​e" data type using loops.
« Reply #5 on: April 26, 2011, 02:34:26 PM »
Code: [Select]
        ResultBuffer buf = new ResultBuffer();

        for (int idx = 0; idx < 80; idx++)
        {
          buf.Add(new TypedValue(1000,string.Format("test{0}",idx)));
        }


Does same thing

Code: [Select]
                Dim resBuf As New ResultBuffer

                For i As Integer = 1 To 80
                    resBuf.Add(New TypedValue(1000, String.Format("test{0}", i)))
                Next


vnsharifi

  • Guest
Re: How to populate "TypedValu​e" data type using loops.
« Reply #6 on: April 26, 2011, 02:42:11 PM »


Thanks To Everybody.  Assume - Jeff - (Special Thaks to Fixo ). attached find the coplete code which does the job for people who may face this in future.

Code: [Select]

    <CommandMethod("Link6")> _
    Public Sub SetXData()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim opt As New PromptEntityOptions(vbLf & "Select entity: ")
        Dim res As PromptEntityResult = ed.GetEntity(opt)
        If res.Status = PromptStatus.OK Then
            Dim tr As Transaction = doc.TransactionManager.StartTransaction()
            Using tr
                Dim obj As DBObject = tr.GetObject(res.ObjectId, OpenMode.ForWrite)
                AddRegAppTableRecord1("TCB")
                Dim lstXdata As List(Of Object) = New List(Of Object)
                For i As Integer = 0 To 99
                    lstXdata.Add("Test" + (i + 1).ToString)
                Next
                Dim rb As New ResultBuffer
                rb.Add(New TypedValue(1001, "TCB"))
                For Each strVal As String In lstXdata
                    rb.Add(New TypedValue(1000, strVal))
                Next
                obj.XData = rb
                rb.Dispose()
                tr.Commit()
            End Using
        End If
    End Sub
    Private Shared Sub AddRegAppTableRecord1(ByVal regAppName As String)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim tr As Transaction = doc.TransactionManager.StartTransaction()
        Using tr
            Dim rat As RegAppTable = DirectCast(tr.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)
            If Not rat.Has(regAppName) Then
                rat.UpgradeOpen()
                Dim ratr As New RegAppTableRecord()
                ratr.Name = regAppName
                rat.Add(ratr)
                tr.AddNewlyCreatedDBObject(ratr, True)
            End If
            tr.Commit()
        End Using
    End Sub
End Class