I want to make a more universal "Progress Bar" which my API has some pre-defined methods. As the progress bar moves, I can update the Message which it displays. I have created a universal "For Each" sub-routine that I can send a Sub, and now I am linking the Progress Bar inside it that also contains a "Cancel" button which I can use to interrupt any loop. But in order to update the message, I want more feedback than just "Item [7/42]", I would rather be able to define "Object.Name" and other times it would need "Object.DisplayName" as each Object I send it will have a different Property that I want to show in the Message.
Is there a way to send an Identifier for which .Property or .Method to reference?
Sub FE(Of T)(c As System.Collections.Generic.IEnumerable(Of T), body As Action(Of T), Optional pb As ProgBar = Nothing, Optional _id As Identifier = Nothing)
If pb IsNot Nothing Then pb.Start(c.Count)
For Each i As T In c
If pb IsNot Nothing AndAlso pb.Cancel Then Exit For
body(i)
If pb IsNot Nothing Then pb.Update(i._id)
Next
If pb IsNot Nothing Then pb.Close
End Sub
The final input to the function is where I want help, I don't know how to describe what it could be?
Optional _id As Identifier = Nothing
So I could invoke this
FE(Of Sketch)(collection(Of Sketch), _
Sub(x As Sketch)
x.Visible = Not x.Visible
End Sub, _
New ProgBar("Some Title"), _
.Name )
Then it could use the .Name when it updates the ProgBar as the Message String. Obviously some error checking would have to be programmed, but I think this gets the point across?
You could create an interface that all the objects you send would implement:
Your
Sketchclass would then implement this interface:In your main loop, you can cast you
iobject toIProgressBarItemand useDisplayText:This offsets the decision-making of what property to display to the object itself.
You should add code to verify that
iimplementsIProgressBarItemthough.Another approach is to use Reflection as described in this question. You would just pass the property name as a String parameter to
FE(instead of_id) and use GetValue to get the property value (Miroslav Zadravec code here):