Bubbling a status message to the UI

P

pamelafluente

Hi guys,

After the Exception question, I have another one, strictly related.
I would also like what is your preferred device
to bubble a message (not by exception) to the user Interface.

Assume for instance the following simple schema. What is the best way
to bubble the "Status" string message to the UI ? Please suggest
appropriate
code changes.

I guess that different programmers might have different ideas on how to
that
in the most flexible way ...

-P

'--------------------------------- SAMPLE CODE -----------------------

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Try
With New SomeProcessor
.SomeTask()

' I want for instance the "Status" issued by SomeOtherTask
' to be appended to a TextBox on this User Interface
' What's the best way to bubble the message here

End With

Catch ex As Exception
Me.RichTextBox1.AppendText(ex.Message)
End Try

End Sub

End Class


'The following is in separate files

Class SomeProcessor

Sub SomeTask()
With New SomeOtherProcessor
Try
.SomeOtherTask()
Catch ex As Exception
Throw
End Try

End With
End Sub

End Class


Class SomeOtherProcessor

Sub SomeOtherTask()

Try
Dim Status As String = "This operation was successful"
Catch ex As Exception
Throw
End Try

End Sub

End Class
 
T

tomb

Pass the textbox as a reference to the called proc, or an object that
can throw an event when changed so you can updated the status control.

Tom
 
P

pamelafluente

Hi tom, thanks

must say I thought about this solution (perhaps the ByRef is optional,
right?). But then I
have some problem when I want to use the code both with Winform or
WebForm.

What I mean that what I would like to bubble is probably
just the message because the interface could be different.

So I wanted your opinions on what is a good method to do
this in such a way we can deal with different UIs.

For instance the Exception mechanism allows to do that.

I was wondering if something similar could be done for a, say, "status"
messages (ie. not accompanied by exceptions).

Thanks you very much,

-P

tomb ha scritto:
 
T

tomb

That would be the choice of using an object that raises an event when it
is changed. Then whatever parent calls the proc, it passes that
object. If the object is changed, the parent can respond to it any way
it wants.

As such:
private withevents myObject as whatever

thisvalue = someproc( myObject )


Tom
 
G

GhostInAK

Hello pamela,

I don't like tomb's solution. You should never pass in an object that listens
for changes. I would suggest simply raising an event from within your processor.


-Boo
 
P

pamelafluente

Hi Ghost,

can you make more precise what you mean. Are you talking about a shared
event?

Actually the idea of an additional argument does not make me
enthusiastic,
but also the idea of some shared event is quite disgusting (to me).

Could you clarify your suggestion, by applying that to my simple
example?

-P

GhostInAK ha scritto:
 
G

GhostInAK

Hello pamela,

public class Form1

private sub Button_click( ... )

Dim tSomeTask as SomeProcessor = New SomeProcessor
AddHandler tSomeTask.StatusChange, addressof StatusChangeHandler

tSomeTask.DanceMonkeyDance

end sub

private sub StatusChangeHandler(byval tStatus as string)

textbox1.text = tstatus

end sub

end class



public class SomeProcessor

public event StatusChange(byval tStatus as string)

private withevents tSomeOtherProcessor as SomeProcessor2 = New SomeProcessor2

private sub StatusChangeHandler(byval tStatus as string) handles tSomeOtherProcessor.StatusChange
raiseevent StatusChange(tStatus)
end sub

public sub DanceMonkeyDance()
tSomeOtherProcessor.DoSomething
end Sub

end class



public class SomeOtherProcessor2

public event StatusChange(byval tStatus as string)

public sub DoSomething
' Change the status.. look at the perdy percolating..
raiseevent StatusChange("Was that good for you?")
end sub

end class
 
P

pamelafluente

Thank you Ghost! Now I see what you meant.

Thanks you very much for taking the time to make this example.

Well, what to say? I am very grateful. I just hoped there was some way
easier to maintain.

It's strange that for errors there is a simple device (exceptions),
while to bubble a simple
message we have to go through all this !

-P


ps
boo :)

GhostInAK ha scritto:
 
G

GhostInAK

Hello (e-mail address removed),

Not so strange. The scenarios which require such a mechanism are fairly few.

In addition to the event solution, you may wish to explore the use of MSMQ
(MS Message Queue). I don't know your architecture, but if the status generation
is burried very deep then MSMQ may be a good solution.

You could also look into custom window messages (in the WM_APP range). This
would not transfer between web/winforms though.

Back on the event solution. You could implement a class from which all your
objects inherited. This base class would define the events. You could then
devise a mechanism, using reflection, which automajikly wired up the events
to be bubbled. *shrug*

Good luck.

-Boo
 
P

pamelafluente

Looks like I have quite some material to think about.

Thank you Ghost. Very helpful. :)

-P

GhostInAK ha scritto:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top