![]()
|
|
Technologies Used:
ASP.NET
C#
Level of Difficulty
Beginner
|
Being in the news game, our lives
revolve around time. It’s the one thing that as programmers we can code around,
against, or with – but can’t control.
Doing online news everyday at KUAM.COM, we tightly integrate our online content
with what runs on our TV news broadcasts that night, as well as what we report
on our radio newscasts throughout the day. Everyday is all about 6PM for us –
showtime. And when we’re on the air, every single second of our lives for
the next 60 minutes is tediously scripted and planned. And because of this, when
we write the news that you see every night, knowing how long it will take for
one of our anchors to read each script – right down to the precise second – is
critical.
For us, it's all about calculating
the Total Read Time (TRT) for a story. It's this functionality that we'll
very easily build onto an ASP.NET WebForm this week.
And being that regardless of the amount of speech training, no two reporters are
alike, so we need some variability in the amount of time it’s going to take to
read each story, so that we can fit the maximum amount of stories within a
one-hour show. This is also a sneak-peek inside how we do things inside the
newsroom for “Guam’s newsteam”.
There’s nothing really earth-shattering about this script…it just demonstrates
how to use the C# programming language creatively with a couple of custom
algorithms to time-code a bit of text, translating content entered by a user to
it’s equivalent minutes and seconds. It makes use of several key concepts about
ASP.NET programming, namely a clear separation of code from content, and having
the WebForm post back to itself, rather than pass information onto another
script.
Beneath the Hood
Looking at the WebForm itself (all of the elements and contents within the
<HTML> tags, we can see that we’re using several ASP.NET Web server controls,
namely two Labels, a DropDownList, a TextBox, and a Button.
Looking now at the C# code within the <SCRIPT> blocks, we first define protected
three variables that will be used throughout a couple of custom methods we’ll
use in the WebForm. The first method, Convert_Time, is wired to the
Button server control’s OnClick event, and first instantiates a
StringBuilder object and uses the object’s Append method to add-in the
text entered in the TextBox. We then pass the total length of the text entered
via our StringBuilder object’s Length property as an argument to a custom
method, GenerateReadTime.
|
This custom method returns data of
type decimal, and divides the total number of characters by the variable value
selected for the DropDownList, obtained through a switch statement, which is the
speed at which a user can read. This quotient is returned to the calling code
(to our Convert_Time method).
When control returns to Convert_Time, the remaining code of the script
uses various static methods to format an output string that returns the total
minutes and seconds needed to read the text to the Web browser, and then assigns
this string to one of the Label’s Text property.
The only gotcha that exists for this WebForm (at least that I know of at this
point), is that you can only enter a maximum of 79,200 characters in the Textbox
server control.
This demo also isn’t too far removed from the time-coding system we use
internally at KUAM News to calculate our read times for when we go on the air.
We take the same programming logic and subclass it to be inherited and reused by
more grandiose applications within and complementary to, our intranet.
You could also use this for timing yourself for speeches, for presentations,
etc.
Have fun – and happy programming!