OSG Community College C# Beginners Class #8 Log 03/27/2009

2 views
Skip to first unread message

AdelleF

unread,
Mar 26, 2009, 9:52:12 PM3/26/09
to OSG Community College
Pastebins at the bottom ;-)


[2009/03/26 13:03] Snowdrop Short: how is it comming along?
[2009/03/26 13:03] BlueWall Slade: should have a place soon to
experiment with it
[2009/03/26 13:03] M1sha Dallin: What will that mean - I've heard of
Moodle but know little
[2009/03/26 13:03] BlueWall Slade: It is comming along really good
[2009/03/26 13:03] Adelle Fitzgerald: hmm
[2009/03/26 13:03] BlueWall Slade: it is a front end using objects to
interact with moodle
[2009/03/26 13:03] Adelle Fitzgerald: ahh better
[2009/03/26 13:03] Adelle Fitzgerald: hi everyone
[2009/03/26 13:03] M1sha Dallin: Hi Adelle
[2009/03/26 13:03] BlueWall Slade: Gi Adelle
[2009/03/26 13:04] Snowdrop Short: hi Adelle
[2009/03/26 13:04] Snowdrop Short: ok, most of the regulars are here
[2009/03/26 13:04] M1sha Dallin: New hair?
[2009/03/26 13:04] Adelle Fitzgerald: me?
[2009/03/26 13:04] M1sha Dallin: yep
[2009/03/26 13:04] Adelle Fitzgerald: nah, had it a while
[2009/03/26 13:04] M1sha Dallin: looks great
[2009/03/26 13:04] Adelle Fitzgerald: just dont wear it often
[2009/03/26 13:04] Adelle Fitzgerald: thanks
[2009/03/26 13:05] Snowdrop Short smiles
[2009/03/26 13:05] M1sha Dallin: Here comes Starky
[2009/03/26 13:05] Snowdrop Short: ok
[2009/03/26 13:05] BlueWall Slade: I have to run - see you all later
[2009/03/26 13:05] BlueWall Slade: have a nice class :)
[2009/03/26 13:05] M1sha Dallin: Bye Bluewall
[2009/03/26 13:06] Snowdrop Short: see you
[2009/03/26 13:06] Adelle Fitzgerald: bye BlueWall
[2009/03/26 13:06] Snowdrop Short: ok, so did eveybody have a look at
IO
[2009/03/26 13:06] M1sha Dallin: yep
[2009/03/26 13:06] Snowdrop Short: and writing a log to a file
[2009/03/26 13:06] Adelle Fitzgerald: yeah
[2009/03/26 13:07] Adelle Fitzgerald: http://www.pastebin.ca/1373352
[2009/03/26 13:07] M1sha Dallin: http://www.pastebin.ca/1373325
[2009/03/26 13:07] Adelle Fitzgerald: i think there are a few 'messy'
bits, but it works ;)
[2009/03/26 13:07] Adelle Fitzgerald: i kinda just went for it with
this one, and 'made it work'
[2009/03/26 13:08] Adelle Fitzgerald: i know thats kinda lazy, but
stuff can be tidied afterwards
[2009/03/26 13:08] Adelle Fitzgerald: when i know how to tidy them
hehe
[2009/03/26 13:08] M1sha Dallin: Lines 405-441 and 499-502 are the
relevant bits
[2009/03/26 13:08] Adelle Fitzgerald: starky is on his way
[2009/03/26 13:09] kidd piko: hello all
[2009/03/26 13:09] Adelle Fitzgerald: hi Kidd
[2009/03/26 13:09] M1sha Dallin: Hi kidd
[2009/03/26 13:09] Snowdrop Short: ok .. while he's working his way
here, I'll look at the code
[2009/03/26 13:09] Snowdrop Short: hi Kidd
[2009/03/26 13:10] Snowdrop Short: Adelle .. I like it
[2009/03/26 13:10] Snowdrop Short: nice
[2009/03/26 13:10] Snowdrop Short: direct
[2009/03/26 13:10] Adelle Fitzgerald smiles
[2009/03/26 13:10] Snowdrop Short: and I see you've been working on
exceptions
[2009/03/26 13:10] Adelle Fitzgerald: yes :)
[2009/03/26 13:10] Snowdrop Short: which is good
[2009/03/26 13:10] Adelle Fitzgerald: its slowly sinking in now
[2009/03/26 13:10] Adelle Fitzgerald: i think i i keep things small
it works much easier for me
[2009/03/26 13:10] Starky Rubble: Sheesh!
[2009/03/26 13:11] Snowdrop Short: especially when working with IO,
error handling is important
[2009/03/26 13:11] Adelle Fitzgerald: ahh hiya starky :)
[2009/03/26 13:11] M1sha Dallin: Hi Starky
[2009/03/26 13:11] Snowdrop Short: you never know what happens when
the bits starts to move in the physical world
[2009/03/26 13:11] Starky Rubble: Hi all
[2009/03/26 13:12] Snowdrop Short: Hi Starky
[2009/03/26 13:12] Snowdrop Short: M1sha .. I like the little detail
you have with "Logging commenced......"
[2009/03/26 13:13] M1sha Dallin: :-)
[2009/03/26 13:13] M1sha Dallin: seemed handy for an append
[2009/03/26 13:13] Snowdrop Short: that's pretty cool
[2009/03/26 13:13] Snowdrop Short: and you've also taken a pretty
direct apprach
[2009/03/26 13:14] Snowdrop Short: ok
[2009/03/26 13:14] Snowdrop Short: I like both solutions I've seen
[2009/03/26 13:14] Snowdrop Short: nice and direct
[2009/03/26 13:14] Snowdrop Short: and both feels like c# code
[2009/03/26 13:15] Snowdrop Short: which is pretty good
[2009/03/26 13:15] Snowdrop Short: ok
[2009/03/26 13:16] Snowdrop Short: on my agenda for today is to
revisit the foreach statement
[2009/03/26 13:16] Adelle Fitzgerald nods
[2009/03/26 13:16] M1sha Dallin: ok
[2009/03/26 13:16] Snowdrop Short: which is a pretty nice thing
[2009/03/26 13:16] Snowdrop Short: although it is only "syntactic
sugar"
[2009/03/26 13:17] Snowdrop Short: you can always do the same things
with a traditional for loop
[2009/03/26 13:17] Starky Rubble: sugar is nice tho
[2009/03/26 13:18] Adelle Fitzgerald: hehe
[2009/03/26 13:18] Snowdrop Short: in order to understand what is
going on, we need to understand a bit more about interfaces
[2009/03/26 13:18] Snowdrop Short: those of you, who are on the
tueday class, knows a bit about interfaces
[2009/03/26 13:19] Snowdrop Short: to cut it short, an interface is a
kind of contract put on a class
[2009/03/26 13:19] Snowdrop Short: a contract, which specifies which
methods you know for certain will exist
[2009/03/26 13:19] Snowdrop Short: kind of like when you subclass
[2009/03/26 13:20] Snowdrop Short: you know that the subclass will
atleast have the same methods as its superclass
[2009/03/26 13:20] Starky Rubble: right
[2009/03/26 13:21] Snowdrop Short: an interface definition looks very
much like a class definition
[2009/03/26 13:21] Snowdrop Short: except that everything is public
by default, and there are not bodies to the methods
[2009/03/26 13:21] Snowdrop Short: only the method name, the return
type and parameter specification
[2009/03/26 13:21] Adelle Fitzgerald nods
[2009/03/26 13:24] Snowdrop Short: http://www.pastebin.ca/1373370
[2009/03/26 13:24] Snowdrop Short: here is an example of two
interfaces
[2009/03/26 13:25] Adelle Fitzgerald: got it
[2009/03/26 13:25] M1sha Dallin: ok
[2009/03/26 13:25] kidd piko: :)
[2009/03/26 13:25] Starky Rubble: kk
[2009/03/26 13:25] Snowdrop Short: ok
[2009/03/26 13:25] Snowdrop Short: first we can see it mimics a class
definition very much
[2009/03/26 13:26] Snowdrop Short: but instead of the keyword "class"
it has "interface"
[2009/03/26 13:26] Snowdrop Short: and it really is just the
signatures of the methods
[2009/03/26 13:26] Snowdrop Short: no implementation
[2009/03/26 13:26] Snowdrop Short: as they will be provided by the
class implementing the interface
[2009/03/26 13:27] Snowdrop Short: the "I" is purely convention
[2009/03/26 13:27] Adelle Fitzgerald nods
[2009/03/26 13:27] Snowdrop Short: but that way people know that
we're talking about an interface
[2009/03/26 13:28] Starky Rubble: the convention is widely observed?
[2009/03/26 13:28] Snowdrop Short: yes,
[2009/03/26 13:28] Snowdrop Short: I cannot remember it not being
followed
[2009/03/26 13:28] Starky Rubble: Thats what I thought...
[2009/03/26 13:28] Snowdrop Short: not one single example springs to
my mind
[2009/03/26 13:29] Starky Rubble: ok ok lol
[2009/03/26 13:29] Snowdrop Short: so any class which "inherits" from
IPencil
[2009/03/26 13:29] Snowdrop Short: is guaranteed to have a method
called "Write"
[2009/03/26 13:29] Snowdrop Short: and an accessor called "IsSharp"
[2009/03/26 13:30] Snowdrop Short: and any class inheriting from
"IPencilSharpener" is guaranteed to have a method called "Sharpen"
[2009/03/26 13:31] Snowdrop Short: the compiler will validate that,
and complain if it isn't implemented fully
[2009/03/26 13:32] Snowdrop Short: some interfaces are part of the
framework, and actually built into the compiler
[2009/03/26 13:32] Snowdrop Short: two such examples are
"IEnumerable" and "IEnumerator"
[2009/03/26 13:33] Snowdrop Short: lets first take a look at
IEnumerable"
[2009/03/26 13:33] kidd piko: k
[2009/03/26 13:34] Snowdrop Short:
http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx
[2009/03/26 13:34] Snowdrop Short: is the MSDN description of
IEnumerable
[2009/03/26 13:35] Snowdrop Short: as you can see
[2009/03/26 13:35] Snowdrop Short: "IEnumerable"
[2009/03/26 13:35] Snowdrop Short: gives a guarantee that one method
will exist in the class
[2009/03/26 13:35] Snowdrop Short: called
[2009/03/26 13:35] Snowdrop Short: IEnumerator
[2009/03/26 13:35] Snowdrop Short: sorry
[2009/03/26 13:35] Snowdrop Short: GetEnumerator()
[2009/03/26 13:36] Snowdrop Short: which will return an IEnumertor
class
[2009/03/26 13:36] Snowdrop Short: or rather a class implementing the
IEnumerator interfaces
[2009/03/26 13:36] Snowdrop Short: and since the c# compiler knows
about IEnumerator
[2009/03/26 13:37] Snowdrop Short: it uses that in the foreach
statement
[2009/03/26 13:37] Snowdrop Short: so with code like: "foreach(char c
in "Hello World")
[2009/03/26 13:37] Snowdrop Short: what really is going on is that:
[2009/03/26 13:38] Snowdrop Short: the string class, implements the
"IEnumerable" class
[2009/03/26 13:38] Valentine MichaelSmith: hello all
[2009/03/26 13:38] Snowdrop Short: and the c# compiler calls
GetEnumerator() on the string
[2009/03/26 13:38] Adelle Fitzgerald: hiya Valentine
[2009/03/26 13:38] Snowdrop Short: hello Valentine
[2009/03/26 13:38] Snowdrop Short: welcome
[2009/03/26 13:38] Starky Rubble: welcome to C# for beginners class
number 7
[2009/03/26 13:39] Valentine MichaelSmith: wow, C, perhaps a bit 2
deep 4 me, may I sit, just in case ???
[2009/03/26 13:39] Snowdrop Short: sure
[2009/03/26 13:39] Snowdrop Short: but not c
[2009/03/26 13:39] Snowdrop Short: but c#
[2009/03/26 13:39] Valentine MichaelSmith: ty, I did take a C class
decades ago :)
[2009/03/26 13:40] Snowdrop Short: ok ... so in order to fully
understand the foreach statement
[2009/03/26 13:40] Snowdrop Short: we need to examine the IEnumerator
interface
[2009/03/26 13:40] Valentine MichaelSmith: is there a set of notes
for this class ???
[2009/03/26 13:40] Snowdrop Short: it is actually pretty simple
[2009/03/26 13:41] Snowdrop Short: no ... just the log
[2009/03/26 13:41] Snowdrop Short: everything is logged
[2009/03/26 13:41] Snowdrop Short: and available later on
[2009/03/26 13:41] Snowdrop Short: it has two methods
[2009/03/26 13:41] Snowdrop Short: "MoveNext"
[2009/03/26 13:42] Snowdrop Short: which advances the "cursor" to the
next item in the collection
[2009/03/26 13:42] Snowdrop Short: "Reset" which sets the cursor to
the first element
[2009/03/26 13:43] Snowdrop Short: and the property "Current"
[2009/03/26 13:43] Snowdrop Short: which returns the element located
at the "cursor"
[2009/03/26 13:43] Adelle Fitzgerald nods
[2009/03/26 13:44] Snowdrop Short: and when "MoveNext" method
encounters the end, it returns false
[2009/03/26 13:44] Snowdrop Short: otherwise true
[2009/03/26 13:45] Snowdrop Short: so behind the scenes, the foreach
statement calls "MoveNext" and "Current"
[2009/03/26 13:45] Starky Rubble: right
[2009/03/26 13:46] Snowdrop Short: in order to mange the looping
through a collection
[2009/03/26 13:46] Snowdrop Short: so any class implementing
"IEnumerable" can be used in a foreach loop
[2009/03/26 13:46] Snowdrop Short: almost all of the standard
collections implements it
[2009/03/26 13:46] Snowdrop Short: and "string" does, as I showed in
my example
[2009/03/26 13:47] Snowdrop Short: usually it is implemented as two
classes
[2009/03/26 13:48] Snowdrop Short: the container class creates a
special iterator class, when the "GetEnumerator" method is called
[2009/03/26 13:48] Snowdrop Short: just like the example on the MSDN
web-site
[2009/03/26 13:49] Snowdrop Short: the things I've just talked about,
were part of the original c# version 1 language
[2009/03/26 13:49] Snowdrop Short: when generics were introduced in
version 2
[2009/03/26 13:50] Snowdrop Short: generic versions of IEnumerator
and IEnumerable were introduced as well
[2009/03/26 13:50] Snowdrop Short: IEnumerator<T> and IEnumerable<T>
[2009/03/26 13:50] Snowdrop Short: but they work pretty much the same
[2009/03/26 13:50] Starky Rubble: ok
[2009/03/26 13:51] Snowdrop Short: except that they have the build in
safety mechanisms of generics
[2009/03/26 13:51] Adelle Fitzgerald nods
[2009/03/26 13:52] Snowdrop Short: like you know that
IEnumerable<int> will always return integers
[2009/03/26 13:52] Snowdrop Short: where as with the "old"
IEnumerable you'd just get an object
[2009/03/26 13:52] Snowdrop Short: and then you had to make sure that
the object was really an int
[2009/03/26 13:53] Starky Rubble: right
[2009/03/26 13:53] Snowdrop Short: a lot of those worries dissapeared
in version 2 with the generics
[2009/03/26 13:54] Snowdrop Short: and since implementing and
IEnumerator class is really just writing boilerplate code
[2009/03/26 13:54] Snowdrop Short: it was automated quite a lot
[2009/03/26 13:54] Snowdrop Short: pastebinning
[2009/03/26 13:56] Snowdrop Short: http://www.pastebin.ca/1373396
[2009/03/26 13:56] M1sha Dallin: ok
[2009/03/26 13:56] Starky Rubble: ok
[2009/03/26 13:56] Adelle Fitzgerald: kk
[2009/03/26 13:57] Snowdrop Short: anybody care to make a guess as to
what this odd method will do?
[2009/03/26 13:58] M1sha Dallin: nope :-)
[2009/03/26 13:58] Starky Rubble: hmmm the position if any of each?
[2009/03/26 14:00] Snowdrop Short: let me pase a more complete sample
[2009/03/26 14:00] Snowdrop Short: http://www.pastebin.ca/1373398
[2009/03/26 14:00] Snowdrop Short: try to run it
[2009/03/26 14:01] Starky Rubble: so yeild is a verb
[2009/03/26 14:01] Starky Rubble: yield even lol
[2009/03/26 14:01] Snowdrop Short: yes
[2009/03/26 14:01] Snowdrop Short: what is does
[2009/03/26 14:02] Snowdrop Short: is that the c# compiler does a bit
of magic behind our backs
[2009/03/26 14:02] Snowdrop Short: and takes away the task of writing
the boilerplate code of the IEnumerator class
[2009/03/26 14:03] Snowdrop Short: when the foreach statement is
executed in line 20
[2009/03/26 14:03] Snowdrop Short: the c# compiler calls the MyChars
() method, which implements "automagically" the IEnumrable Interface
[2009/03/26 14:04] Snowdrop Short: and returns an IEnumerator<char>
[2009/03/26 14:04] Snowdrop Short: the MoveNext's
[2009/03/26 14:04] M1sha Dallin: So you are essentially defining an
enumeration type (as known in Ada, PAscal etc.)?
[2009/03/26 14:04] Snowdrop Short: yes
[2009/03/26 14:05] M1sha Dallin: ok
[2009/03/26 14:05] Snowdrop Short: the MoveNext dives into the the
MyChars method and runs until it meets the first yield
[2009/03/26 14:05] Snowdrop Short: the first yield "returns" the
value A
[2009/03/26 14:06] Snowdrop Short: so then "Current" is called in
order to assign a value to c
[2009/03/26 14:06] Snowdrop Short: and the letter "A" is written to
the console
[2009/03/26 14:06] Snowdrop Short: and it loops
[2009/03/26 14:07] Snowdrop Short: and then again "MoveNext" is
called on "MyChars"
[2009/03/26 14:07] Snowdrop Short: and the execution continues from
the last yield"
[2009/03/26 14:07] Snowdrop Short: so this time "B" is returned
[2009/03/26 14:07] Snowdrop Short: then "C"
[2009/03/26 14:07] Snowdrop Short: then "D"
[2009/03/26 14:07] Starky Rubble: ok
[2009/03/26 14:08] Snowdrop Short: until it reaches the end, and then
MoveNext returns false
[2009/03/26 14:08] Snowdrop Short: and the foreach loop is completed
[2009/03/26 14:09] Snowdrop Short: yield can either be "yield return
<somevalue>" or "yield break"
[2009/03/26 14:09] Snowdrop Short: in order to end the iteration
[2009/03/26 14:10] Snowdrop Short: using this, writing your own
things for use in "foreach" becomes a snap
[2009/03/26 14:10] Snowdrop Short: and often it is easier to
implement things using yield, than the traditional oldfashioned for
loops
[2009/03/26 14:11] Starky Rubble: sounds handy
[2009/03/26 14:11] Adelle Fitzgerald: yes
[2009/03/26 14:11] Snowdrop Short: here are a few more examples
[2009/03/26 14:12] Adelle Fitzgerald: so you could use this to
enumerate a list, and run a function or something against each og the
list contents?
[2009/03/26 14:12] Adelle Fitzgerald: indexes i mean, couldnt think
of the word
[2009/03/26 14:12] Snowdrop Short: yes, or anything, traversals of
trees
[2009/03/26 14:12] Adelle Fitzgerald nods
[2009/03/26 14:13] Snowdrop Short: or every other element in a list
[2009/03/26 14:13] Snowdrop Short: what ever you need
[2009/03/26 14:13] Adelle Fitzgerald: cool
[2009/03/26 14:13] Adelle Fitzgerald: i like the way you dont need to
indicate the end like you would have to in a conventional for loop
[2009/03/26 14:14] Snowdrop Short nods
[2009/03/26 14:14] Snowdrop Short: it lessens the danger of off by
one errors
[2009/03/26 14:14] Adelle Fitzgerald: yes
[2009/03/26 14:15] Snowdrop Short: http://www.pastebin.ca/1373413
[2009/03/26 14:15] Snowdrop Short: here is another one
[2009/03/26 14:15] Snowdrop Short: but don't run it
[2009/03/26 14:15] Snowdrop Short: it will loop for ever
[2009/03/26 14:15] Adelle Fitzgerald: hehe ok
[2009/03/26 14:15] Adelle Fitzgerald: got it
[2009/03/26 14:16] M1sha Dallin: ok :-)
[2009/03/26 14:16] Starky Rubble: lol
[2009/03/26 14:16] Snowdrop Short: remember, execution is restarted
from the last yield return statement
[2009/03/26 14:16] Snowdrop Short: only the imagination puts a limit
on what you can use it for
[2009/03/26 14:17] Snowdrop Short: and since it is so easy, to do
magic like that using the yield statement
[2009/03/26 14:17] Snowdrop Short: you sometimes get tempted to go
overboard
[2009/03/26 14:17] Starky Rubble: I can imagine
[2009/03/26 14:19] Snowdrop Short: ok
[2009/03/26 14:20] Snowdrop Short: any questions on yield?
[2009/03/26 14:20] Starky Rubble: not now
[2009/03/26 14:20] Adelle Fitzgerald: no, i think i have it
[2009/03/26 14:20] M1sha Dallin: The yield break - why would you use
it?
[2009/03/26 14:20] Snowdrop Short: let's take the last sample I gave
you
[2009/03/26 14:21] Snowdrop Short: infiniteAlternatingBools
[2009/03/26 14:21] Snowdrop Short: I could have introduced a paramert
on the method (int count)
[2009/03/26 14:22] Snowdrop Short: and then when it had returned
"count" bools, it would terminate
[2009/03/26 14:22] Snowdrop Short: using "yield break"
[2009/03/26 14:22] M1sha Dallin: Ah OK
[2009/03/26 14:22] Starky Rubble: right and made a cut off point
[2009/03/26 14:22] Snowdrop Short: yes, just like the break statement
inside a loop
[2009/03/26 14:22] Snowdrop Short: a means of breaking out of the
logic
[2009/03/26 14:23] Snowdrop Short: ok ...
[2009/03/26 14:24] Snowdrop Short: if there are no questions .. I
think we should move on to some more magic
[2009/03/26 14:24] M1sha Dallin: ok
[2009/03/26 14:24] Snowdrop Short: ... oh by the way, M1sha...
[2009/03/26 14:24] M1sha Dallin: yep
[2009/03/26 14:25] Snowdrop Short: using yield, you could have
implemented an IEnumerable<Token> in your parser :-)
[2009/03/26 14:25] M1sha Dallin: I was thinking that - missed an
enumeration type :-)
[2009/03/26 14:25] M1sha Dallin: I'll revisit
[2009/03/26 14:26] Snowdrop Short: well, now you have the tools for
that in your tool kit
[2009/03/26 14:26] M1sha Dallin: also thinking of using Tokens for
the command line interpretation
[2009/03/26 14:26] Snowdrop Short: ok
[2009/03/26 14:26] Snowdrop Short: so next
[2009/03/26 14:27] Snowdrop Short: I think we should talk a bit about
error handling
[2009/03/26 14:27] Snowdrop Short: I can see you're allready using it
[2009/03/26 14:27] Snowdrop Short: so perhaps it's time for me to
talk a bit about it
[2009/03/26 14:27] Snowdrop Short: c# uses a "try/catch" mechanism
[2009/03/26 14:28] Snowdrop Short: for error handling
[2009/03/26 14:28] Snowdrop Short: sometimes referred to as
"strutured error handling"
[2009/03/26 14:28] Starky Rubble: I'm afraid I'll have to read it
later... got to meet a taxman... thanks and bye
[2009/03/26 14:28] Snowdrop Short: "structured" even
[2009/03/26 14:28] M1sha Dallin: good luck
[2009/03/26 14:28] Snowdrop Short: okie .. have "fun"
[2009/03/26 14:29] Snowdrop Short: the execption handling is build
into the language and the runtime
[2009/03/26 14:30] Snowdrop Short: and is acutally better performing
than the similar impementation in some other languages
[2009/03/26 14:30] Snowdrop Short: like c++
[2009/03/26 14:30] Snowdrop Short: stilll, execptions has an
performace overhead, so it should only be used to handle "unusual"
situation
[2009/03/26 14:30] Adelle Fitzgerald: i noticed that streamwriter is
capable of writing directly to the windows event log
[2009/03/26 14:31] Snowdrop Short: like out of memory, out of disk,
network disconnects and that kind of stuff
[2009/03/26 14:31] Snowdrop Short: yes
[2009/03/26 14:31] Snowdrop Short: but then your code won't run on
linux or mac
[2009/03/26 14:31] Adelle Fitzgerald: right
[2009/03/26 14:32] Snowdrop Short: that particular logging mechanism
is windows only
[2009/03/26 14:32] Adelle Fitzgerald: gotcha
[2009/03/26 14:32] Snowdrop Short: so you'll have to decide if you
want to support other OS'es or not
[2009/03/26 14:32] Adelle Fitzgerald: so it would be better to write
to a logfile in all circumstances
[2009/03/26 14:32] Adelle Fitzgerald: for portability
[2009/03/26 14:32] Snowdrop Short: if you want to be portable, yes
[2009/03/26 14:33] Adelle Fitzgerald: okies
[2009/03/26 14:33] Snowdrop Short: so lets look at a try catch
scenario
[2009/03/26 14:34] Valentine MichaelSmith: Snow Crash :)
[2009/03/26 14:34] Valentine MichaelSmith: Oh U said Catch, not
Crash, sorry
[2009/03/26 14:34] Snowdrop Short: hehe
[2009/03/26 14:35] Snowdrop Short: http://www.pastebin.ca/1373425
[2009/03/26 14:35] Snowdrop Short: ok .. here is an example
[2009/03/26 14:36] Adelle Fitzgerald: got it
[2009/03/26 14:36] M1sha Dallin: ok
[2009/03/26 14:37] Snowdrop Short: what happens here is that we've
put a "guard" on the code inside the "try" block
[2009/03/26 14:37] Snowdrop Short: if an exception occurs, it will
immediately break out of the try block
[2009/03/26 14:38] Snowdrop Short: and depending on the kind of
exception occuring, it might execute the catch block
[2009/03/26 14:38] Snowdrop Short: if the exception was an
"ArgumentException"
[2009/03/26 14:38] Snowdrop Short: the catch block will be executed
[2009/03/26 14:39] Snowdrop Short: regardless of how we exit the try
block, the finally block will ALWAYS be executed
[2009/03/26 14:39] M1sha Dallin: In Ada the exceptions can be
propogated up the call tree - can that be done in C#?
[2009/03/26 14:39] Snowdrop Short: yes
[2009/03/26 14:39] M1sha Dallin: ok
[2009/03/26 14:40] Snowdrop Short: the thing to observe is that no
matter what, the code in the finally block will always be executed
[2009/03/26 14:40] Adelle Fitzgerald: is this similar to how the
exception and crash logging works in opensim?
[2009/03/26 14:40] Snowdrop Short: hmm .... good question, I haven't
looked at that part, but I'd guess it is atleast part of the crash
handler
[2009/03/26 14:40] Adelle Fitzgerald: gotcha
[2009/03/26 14:41] Snowdrop Short: ok
[2009/03/26 14:41] Snowdrop Short: the other thing to observer is
that both catch and finally are optional
[2009/03/26 14:41] Snowdrop Short: but atleast one of them must exist
[2009/03/26 14:41] Snowdrop Short: so try/finally is ok
[2009/03/26 14:42] Snowdrop Short: try/catch is ok
[2009/03/26 14:42] Snowdrop Short: and try/catch/finally is ok
[2009/03/26 14:42] Snowdrop Short: but try, without anything else is
invalid
[2009/03/26 14:42] Adelle Fitzgerald: gotcha
[2009/03/26 14:44] Snowdrop Short: http://www.pastebin.ca/1373438
[2009/03/26 14:44] Snowdrop Short: here is a little more
[2009/03/26 14:45] Adelle Fitzgerald: kk
[2009/03/26 14:45] Snowdrop Short: I have extended the
infiniteAlternatingBools method
[2009/03/26 14:45] M1sha Dallin: ol
[2009/03/26 14:45] Snowdrop Short: now it will throw an exception
after 11 invocations
[2009/03/26 14:46] Snowdrop Short: and this is how the framework
signals error conditions
[2009/03/26 14:46] Snowdrop Short: it "throws" and eception
[2009/03/26 14:46] Snowdrop Short: which is a special class
[2009/03/26 14:47] Snowdrop Short: there is a convention, that all
"Exception" classes should end with the name "Exception"
[2009/03/26 14:47] Snowdrop Short: and a long range of exceptions are
already defined
[2009/03/26 14:47] Snowdrop Short: IOErrors, Argument errors
[2009/03/26 14:47] Snowdrop Short: etc
[2009/03/26 14:48] Adelle Fitzgerald nods
[2009/03/26 14:48] Snowdrop Short: they are scattered all over the
framework
[2009/03/26 14:48] Snowdrop Short: and one of the bad things is that
it is difficult to get a clear picture of all the exceptions used in
the framework
[2009/03/26 14:49] Snowdrop Short: the only thing you can be sure of
is that they all inherit from the class "Exception"
[2009/03/26 14:49] Snowdrop Short: so what happens is that when an
exception is thrown
[2009/03/26 14:49] Snowdrop Short: the runtime will search for a
"catch" block which will handle the Exception
[2009/03/26 14:50] Snowdrop Short: in our example
[2009/03/26 14:50] Snowdrop Short: when the execption is thrown
execution will break out of the try block
[2009/03/26 14:51] Snowdrop Short: if the exception matches one of
the catch blocks, the catch block is executed
[2009/03/26 14:51] Snowdrop Short: and then the finally block, and
the exception is considered handled
[2009/03/26 14:51] Snowdrop Short: and is thrown away, and execution
continues after the finally block
[2009/03/26 14:52] Snowdrop Short: if the execption is *not* handled
locally
[2009/03/26 14:52] Snowdrop Short: the finally block is excuted
[2009/03/26 14:52] Snowdrop Short: and the current method is exited
[2009/03/26 14:53] Snowdrop Short: if were deep into a nested method,
called inside a method, called inside a method and so on
[2009/03/26 14:53] Snowdrop Short: it will continue exiting the
"current" method, calling finally on the way
[2009/03/26 14:53] Snowdrop Short: until it leaves main
[2009/03/26 14:54] Snowdrop Short: and the program will terminate
with an error
[2009/03/26 14:54] Snowdrop Short: for "crash"
[2009/03/26 14:54] Snowdrop Short: or "crash" rather
[2009/03/26 14:54] Adelle Fitzgerald nods
[2009/03/26 14:54] Snowdrop Short: lets take a look at how that could
look
[2009/03/26 14:56] Snowdrop Short: http://www.pastebin.ca/1373452
[2009/03/26 14:57] Snowdrop Short: as you can see
[2009/03/26 14:57] M1sha Dallin: ok
[2009/03/26 14:57] Adelle Fitzgerald: got it
[2009/03/26 14:58] Snowdrop Short: we're still throwing an
ArgumentException inside the enumerator
[2009/03/26 14:58] Snowdrop Short: but now we have a number of catch
blocks
[2009/03/26 14:58] Snowdrop Short: one tries to catch a
"ThreadException"
[2009/03/26 14:58] Snowdrop Short: another an "OutOfMemoryException"
[2009/03/26 14:58] Snowdrop Short: and finally "SomeOtherExcption"
[2009/03/26 14:59] Snowdrop Short: SomeOtherException is perfectly
valid
[2009/03/26 14:59] Snowdrop Short: since we are free to implement our
own exception classes
[2009/03/26 14:59] Snowdrop Short: and in larger frameworks it is
pretty common to have specialized exceptions
[2009/03/26 15:00] Snowdrop Short: but since we do not catch
"ArgumentException"
[2009/03/26 15:00] Snowdrop Short: the application will crash (if it
would compile"
[2009/03/26 15:01] Snowdrop Short: another intersting thing is the
throw statement in lin 52
[2009/03/26 15:01] Snowdrop Short: remember if an exception is
caught, it is considered as handled
[2009/03/26 15:01] Snowdrop Short: and the application will continue
[2009/03/26 15:02] Snowdrop Short: so if we get a "ThreadException",
the compiler expects that the code inside the "catch" block had dealt
with the problem
[2009/03/26 15:02] Snowdrop Short: and that execution can contine
[2009/03/26 15:02] Snowdrop Short: in the case of
"SomeOtherException"
[2009/03/26 15:03] Snowdrop Short: we're saying, we have not
recovered from the exeception, and the runtime should continue to seek
for other catch blocks which will handle the exception
[2009/03/26 15:04] Valentine MichaelSmith: RL calls
[2009/03/26 15:04] Snowdrop Short: yes,
[2009/03/26 15:04] Valentine MichaelSmith: this was great, I must
attend again
[2009/03/26 15:04] Snowdrop Short: for me too
[2009/03/26 15:04] Valentine MichaelSmith: please add me to the
group, etc.
[2009/03/26 15:04] Snowdrop Short: I have one last thing
[2009/03/26 15:04] Valentine MichaelSmith: take care M.D. - Agape
Michael
[2009/03/26 15:04] Valentine MichaelSmith: [ok], sure ...
[2009/03/26 15:05] Adelle Fitzgerald: you have to add yourself
Valentine, http://groups.google.com/group/osg-community-college
[2009/03/26 15:05] Adelle Fitzgerald: if you want the logs etc that
is :)
[2009/03/26 15:06] Snowdrop Short: http://www.pastebin.ca/1373462
[2009/03/26 15:06] Snowdrop Short: in this example I have two more
twists
[2009/03/26 15:06] Snowdrop Short: there is a "catch(Exception ex)"
block
[2009/03/26 15:06] Snowdrop Short: which generally is a no no, except
in the main class
[2009/03/26 15:07] Snowdrop Short: the reason for this is that when
the runtime tries to find a matching catch block
[2009/03/26 15:07] Snowdrop Short: it also considers the inheritance
chain, and since all exceptions inherits from "Exception"
[2009/03/26 15:08] Snowdrop Short: it will catch all exceptions
[2009/03/26 15:08] Snowdrop Short: no matter what the error was
[2009/03/26 15:08] Adelle Fitzgerald: gotcha
[2009/03/26 15:08] Snowdrop Short: it might be a connection failure
to a database server
[2009/03/26 15:08] Snowdrop Short: or a hard disk failure
[2009/03/26 15:08] Snowdrop Short: so it simply cannot recover, all
things
[2009/03/26 15:09] Snowdrop Short: it migh log it
[2009/03/26 15:09] Snowdrop Short: but that's all it can do
[2009/03/26 15:09] Snowdrop Short: so generally, catch(Exception ex)
should only exist in one place in the application
[2009/03/26 15:09] Adelle Fitzgerald nods
[2009/03/26 15:10] Snowdrop Short: the last twist is rethrowing the
exception as a new exception
[2009/03/26 15:10] Snowdrop Short: in line 57
[2009/03/26 15:11] Snowdrop Short: a new exeception is instantiated,
and the old exception is embedded into the new exception as an "inner"
exception
[2009/03/26 15:11] Snowdrop Short: ok
[2009/03/26 15:11] Snowdrop Short: I think that was a quick run down
on try/catch/catch/catch/finally
[2009/03/26 15:12] Adelle Fitzgerald: :)
[2009/03/26 15:12] Snowdrop Short: and with that...
[2009/03/26 15:12] Snowdrop Short: I'll hurry of to bed
[2009/03/26 15:12] M1sha Dallin: sleep well :-)
[2009/03/26 15:12] Adelle Fitzgerald: hehe okies
[2009/03/26 15:12] Snowdrop Short: unless you have some questions
[2009/03/26 15:12] Adelle Fitzgerald: thanks Snowdrop, sleep well :)
[2009/03/26 15:12] M1sha Dallin: nope
[2009/03/26 15:12] Adelle Fitzgerald: im good thanks
[2009/03/26 15:12] M1sha Dallin: ty Snowdrop
[2009/03/26 15:12] Snowdrop Short: thank you for listening in on me
[2009/03/26 15:12] Snowdrop Short: sweet dreams


http://www.pastebin.ca/1373352

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Calculator
{
class Calculator
{
public void getInput(ref string op, ref int a, ref int b, ref
bool running)
{
string getInput;
for (int i = 0; i < 3; i++)
{
getInput = Console.ReadLine();
if (getInput.ToLower() == "q")
{
Console.WriteLine("Quit");
running = false;
return;
}
WriteLogFile.WriteLog(getInput + " "); //Write
input to log file
if (i == 0)
{
if (!int.TryParse(getInput, out a))
{
Console.WriteLine("The input must be an
integer");
WriteLogFile.WriteLog(" :: " + getInput + " is
not a correct integer "); //Write error to log file
return;
}
}
else if (i == 1)
{
op = getInput;
if ((op != "+") & (op != "-") & (op != "/") & (op !
= "*"))
{
Console.WriteLine("The input {0} is not the
correct operand. Allowed operands are: + - / *", op);
WriteLogFile.WriteLog(" :: " + op + " is not
the correct operand "); //Write error to log file
return;
}
}
else if (i == 2)
{
if (!int.TryParse(getInput, out b))
{
Console.WriteLine("The input must be an
integer");
WriteLogFile.WriteLog(" :: " + getInput + " is
not a correct integer "); //Write error to log file
return;
}
}
}
}
public int doCalc(string op, int a, int b)
{
int result;
switch (op)
{
case "+":
result = a + b;
return result;
case "-":
result = a - b;
return result;
case "/":
result = a / b;
return result;
case "*":
result = a * b;
return result;
default:
result = 0;
return result;
}
}
}
//Write log file class
class WriteLogFile
{
public static void WriteLog(string Log)
{
try
{
using (StreamWriter text = File.AppendText
("calculator_log.txt"))
{
text.Write(Log);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}

class MainClass
{
static void Main(string[] args)
{
do
{
WriteLogFile.WriteLog("\r\n" + DateTime.Now + " ");
int a = 0, b = 0;
string op = "";
bool running = true;
int result;
//string log;

Calculator calc = new Calculator();
Console.WriteLine("Please input calculation, hitting
[Enter] after each line.\nEnter q at any time to quit.");
calc.getInput(ref op, ref a, ref b, ref running);
if (running == false)
{
WriteLogFile.WriteLog("Quit");
return;
}
result = calc.doCalc(op, a, b); //Do calculation
Console.WriteLine("The answer is {0}\n\n",
result); //Write to console
WriteLogFile.WriteLog("= " + result.ToString()); //
Write result to log file
}
while (true == true);
}
}
}



calculator_log.txt


26/03/2009 18:43:08 5 - 3 = 2
26/03/2009 18:43:14 Quit
26/03/2009 18:43:29 56 * 34 = 1904
26/03/2009 18:43:37 2000 / 5 = 400
26/03/2009 18:43:47 r :: r is not a correct integer = 0
26/03/2009 18:43:50 56 t :: t is not the correct operand = 0
26/03/2009 18:43:55 300 * 45 = 13500
26/03/2009 18:44:00 Quit
26/03/2009 18:54:20 56 + 3 = 59
26/03/2009 18:54:27 Quit
26/03/2009 18:57:16 32 - 5 = 27
26/03/2009 18:57:22 Quit




http://www.pastebin.ca/1373325

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.Specialized;


namespace Calculator
{

// --------------- Command line argument handling from Here
-------------------
/// <summary>
/// Arguments class
/// </summary>
public class Arguments
{
// Variables
private StringDictionary Parameters;

// Constructor
public Arguments(string[] Args)
{
Parameters = new StringDictionary();
Regex Spliter = new Regex(@"^-{1,2}|^/|=|:",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

Regex Remover = new Regex(@"^['""]?(.*?)['""]?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

string Parameter = null;
string[] Parts;

// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach (string Txt in Args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
Parts = Spliter.Split(Txt, 3);

switch (Parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
{
Parts[0] =
Remover.Replace(Parts[0], "$1");

Parameters.Add(Parameter, Parts[0]);
}
Parameter = null;
}
// else Error: no parameter waiting for a
value (skipped)
break;

// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter = Parts[1];
break;

// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}

Parameter = Parts[1];

// Remove possible enclosing characters (",')
if (!Parameters.ContainsKey(Parameter))
{
Parts[2] = Remover.Replace(Parts[2],
"$1");
Parameters.Add(Parameter, Parts[2]);
}

Parameter = null;
break;
}
}
// In case a parameter is still waiting
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
}

// Retrieve a parameter value if it exists
// (overriding C# indexer property)
public string this[string Param]
{
get
{
return (Parameters[Param]);
}
}
}
// --------------- To Here borrowed from the
web-------------------------------

//
----------------------------------------------------------------------------
public class Token<T1, T2>
{
public T1 m_Index;
public T2 m_Value;

public Token(T1 index, T2 value)
{
m_Index = index;
m_Value = value;
}

// Delegate method for converting regex output into a token
public delegate Token<T1,T2> BuildToken(int aKey, string
aValue);

// Structures for representing and converting an expression
public class TokenStack : Stack<Token<T1, T2>> { }

public class TokenList : List<Token<T1, T2>>
{
public TokenList() { }

public TokenList(Regex re, string theString, BuildToken
theTokenBuilder)
{
MatchCollection Matches = re.Matches(theString);

foreach (Match match in Matches)
{
int i = 0;
foreach (Group group in match.Groups)
{
string matchValue = group.Value;
bool success = group.Success;

// Ignore Whitespace
if (success && i > 1)
{
this.Add(theTokenBuilder(i, matchValue));
}
i++;
}
}
}
}
}



//
----------------------------------------------------------------------------
public class ExpressionCalculator
{
// Basic type definitions for Postfix conversion
public enum CalcTokenType
{
None = 0,
WhiteSpace = 1,
Operand = 2,
Op_OpenParen = 3,
Op_CloseParen = 4,
Op_Plus = 5,
Op_Minus = 6,
Op_Multiply = 7,
Op_Divide = 8,
Op_Exponentiate = 9,
Op_UnaryMinus = 10,
Invalid = 11
}

private enum OperatorAssociativity
{
LeftRight,
RightLeft,
None
}

// The following array MUST match the order for TokenType
private static OperatorAssociativity[] TokenAssociativity ={

OperatorAssociativity.None,

OperatorAssociativity.None,

OperatorAssociativity.None,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.RightLeft,

OperatorAssociativity.RightLeft
};
// Regex definition for tokenizing
private const string CalculatorPattern =
@"(?<whitespace>\s+)|" +
@"(?<operand>\d+\.?\d*|\.\d+)|" +
@"(?<open>\()|" +
@"(?<close>\))|" +
@"(?<plus>\+)|" +
@"(?<minus>\-)|" +
@"(?<multiply>\*)|" +
@"(?<divide>\/)|" +
@"(?<exponentiate>\^)|";

private class MathToken : Token<CalcTokenType, Double>
{
private MathToken(CalcTokenType index, Double value) : base
(index, value) { }

// Method: MathTokenBuilder - translates an index and
string pair from a regex parse to a MathToken
// input: int
// output: string
// return: MathToken
//
public static MathToken MathTokenBuilder(int aKey, string
aValue)
{
if ((CalcTokenType)aKey == CalcTokenType.Operand)
//Note the aValue will Parse since it has been
checked by the Regex
return new MathToken((CalcTokenType)aKey,
Double.Parse(aValue));
else
return new MathToken((CalcTokenType)aKey,
Double.NaN);
}
}


// Method: ToPostfix - translates a list of tokens
representing an infix expression to a list representing postfix
// input: TokenList
// output: TokenList
// return: Boolean
//
private static Boolean ToPostfix(MathToken.TokenList
infixList, out MathToken.TokenList postfixList)
{
MathToken.TokenStack OperandStack = new
MathToken.TokenStack();
MathToken.TokenList PostfixList = new MathToken.TokenList
();
Boolean ExpectOperand = true;

postfixList = PostfixList;

foreach (MathToken token in infixList)
{
switch (token.m_Index)
{
case CalcTokenType.Operand:
if (!ExpectOperand) return false;
PostfixList.Add(token);
ExpectOperand = false;
break;

case CalcTokenType.Op_Minus:
case CalcTokenType.Op_Plus:
case CalcTokenType.Op_Multiply:
case CalcTokenType.Op_Divide:
case CalcTokenType.Op_Exponentiate:
if (ExpectOperand && token.m_Index ==
CalcTokenType.Op_Minus) token.m_Index = CalcTokenType.Op_UnaryMinus;
do
{
if (OperandStack.Count == 0 ||
token.m_Index > OperandStack.Peek
().m_Index && TokenAssociativity[(int)token.m_Index] ==
OperatorAssociativity.LeftRight ||
token.m_Index >= OperandStack.Peek
().m_Index && TokenAssociativity[(int)token.m_Index] ==
OperatorAssociativity.RightLeft)
{
OperandStack.Push(token);
break;
}
else if (token.m_Index < OperandStack.Peek
().m_Index)
PostfixList.Add(OperandStack.Pop());
else
return false;
}
while (true == true);

ExpectOperand = true;
break;

case CalcTokenType.Op_OpenParen:
OperandStack.Push(token);
break;

case CalcTokenType.Op_CloseParen:
while (OperandStack.Peek().m_Index !=
CalcTokenType.Op_OpenParen) PostfixList.Add(OperandStack.Pop());

OperandStack.Pop();
break;

default:
Console.WriteLine("Token invalid in this
context");
return false;
}
}

// Transfer remaining stack
while (OperandStack.Count > 0) PostfixList.Add
(OperandStack.Pop());

postfixList = PostfixList;
return true;
}

// Method: EvaluatePostfixList - evaluates the expression in
a postfix list
// input: TokenList
// output: Double
// return: Boolean
private static Boolean EvaluatePostfixList(MathToken.TokenList
postfixList, out Double result)
{
MathToken.TokenStack ProcessStack = new
MathToken.TokenStack();
MathToken OperA;

foreach (MathToken token in postfixList)
{
try
{
switch (token.m_Index)
{
case CalcTokenType.Operand:
ProcessStack.Push(token);
break;

case CalcTokenType.Op_UnaryMinus:
ProcessStack.Peek().m_Value = -
ProcessStack.Peek().m_Value;
break;

case CalcTokenType.Op_Plus:
OperA = (MathToken)ProcessStack.Pop();
ProcessStack.Peek().m_Value +=
OperA.m_Value;
break;

case CalcTokenType.Op_Minus:
OperA = (MathToken)ProcessStack.Pop();
ProcessStack.Peek().m_Value -=
OperA.m_Value;
break;

case CalcTokenType.Op_Multiply:
OperA = (MathToken)ProcessStack.Pop();
ProcessStack.Peek().m_Value *=
OperA.m_Value;
break;

case CalcTokenType.Op_Divide:
OperA = (MathToken)ProcessStack.Pop();
ProcessStack.Peek().m_Value /=
OperA.m_Value;
break;

case CalcTokenType.Op_Exponentiate:
OperA = (MathToken)ProcessStack.Pop();
ProcessStack.Peek().m_Value = Math.Pow
(ProcessStack.Peek().m_Value, OperA.m_Value);
break;

default:
Console.WriteLine("Token invalid in this
context");
result = 0.0;
return false;
}
}
catch
{
result = 0.0;
return false;
}
}

result = ProcessStack.Pop().m_Value;
return true;
}


// Method: EvaluateStringExpression - evaluates the
expression in a string
// input: string
// output: Double
// return: Boolean - true if a successful evaluation
//
public static Boolean EvaluateStringExpression(string
expression, out Double result)
{
Regex Re = new Regex(CalculatorPattern);
MathToken.TokenList InfixList = new MathToken.TokenList
(Re, expression, MathToken.MathTokenBuilder);
MathToken.TokenList PostfixList = new MathToken.TokenList
();

Boolean status;
Double ExpressionResult;

if (ExpressionCalculator.ToPostfix(InfixList, out
PostfixList))
{
status = ExpressionCalculator.EvaluatePostfixList
(PostfixList, out ExpressionResult);

result = ExpressionResult;
return status;
}

result = 0.0;
return false;
}
}

//
----------------------------------------------------------------------------
class TextLogger
{
TextWriter m_Writer;
Boolean m_Logging;

public TextLogger(string filename, Boolean append)
{
if (filename != "")
{
m_Logging = true;
try
{
m_Writer = new StreamWriter(filename, append);
m_Writer.WriteLine("\nLogging commenced at: {0}
UTC", DateTime.UtcNow);
}
catch
{
Console.WriteLine("\nOpen file {0} for append:{1}
failed - exiting", filename, append);
Environment.Exit(0);
}
}
else
m_Logging = false;
}

public void Log(string data)
{
if (m_Logging)
{
using (m_Writer)
{
m_Writer.WriteLine("{0} UTC: {1}",
DateTime.UtcNow, data);
}
}
Console.WriteLine("{0} UTC: {1}", DateTime.UtcNow, data);
}
}


//
----------------------------------------------------------------------------
class Program
{
// Method - Get a string from the console after displaying a
prompt
// input: string
// return: string
//
public static string GetStringWithPrompt(string prompt)
{
Console.Write("\n\n{0}\n==> ", prompt);
return Console.ReadLine();
}


// Main program
static void Main(params string[] args)
{
string CalcString;
string FileName="";

Double Answer = Double.NaN;

Boolean Converted = false;
Boolean Append = false;

Arguments CommandLine = new Arguments(args);

if (CommandLine["help"] != null || CommandLine["?"] !=
null)
{
Console.WriteLine("Usage: {0} [-file <filename>] [-
append]", Environment.CommandLine);
Environment.Exit(0);
}

if (CommandLine["file"] != null)
FileName = CommandLine["file"];

if (CommandLine["append"] != null)
Append = Boolean.Parse(CommandLine["append"]);


TextLogger LogReport = new TextLogger(FileName, Append);

Console.WriteLine(" An 'Expression Calculator' written in
C#.");
Console.WriteLine(" It evaluates expressions (and
parenthesised expressions) written using");
Console.WriteLine(" (double precision) numerics and the
standard operators +, -, *, /");
Console.WriteLine(" as well as ^ (for exponentiation).");
Console.WriteLine(" The normal rules for operator
precedence apply, ");
Console.WriteLine(" e.g. (2+3.01^3*4)/2 evaluates to
55.541802");

do
{
CalcString = GetStringWithPrompt("Enter expression or
q (or Q) to quit");
if (CalcString.ToLower()[0] == 'q') break;

Converted =
ExpressionCalculator.EvaluateStringExpression(CalcString, out Answer);
if (Converted)
LogReport.Log(string.Format("The expression
\"{0}\" evaluated to: {1}", CalcString, Answer));
else
LogReport.Log(string.Format("The expression
\"{0}\" contained errors", CalcString));
} while (true);
}
}
}




http://www.pastebin.ca/1373370

public interface IPencil
{
void Write();

bool IsSharp { get; }
}


public interface IPencilSharpener
{
void Sharpen(IPencil pencil);
}





http://www.pastebin.ca/1373396

public IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}





http://www.pastebin.ca/1373398

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

public static void Main(string[] args)
{
foreach(char c in MyChars())
{
Console.WriteLine(c);
}
}
}
}





http://www.pastebin.ca/1373413

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

public static IEnumerable<bool> infiniteAlternatingBools()
{
bool cur = false;
while(true)
{
yield return cur;
cur = !cur;
}
}

public static void Main(string[] args)
{
foreach(bool b in infiniteAlternatingBools())
{
Console.WriteLine(b);
}
}
}
}





http://www.pastebin.ca/1373425

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

public static IEnumerable<bool> infiniteAlternatingBools()
{
bool cur = false;
while(true)
{
yield return cur;
cur = !cur;
}
}

public static void Main(string[] args)
{
try {
IEnumerable<bool> d = infiniteAlternatingBools();
{
Console.WriteLine(b);
}
} catch(ArgumentException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("AllDone");
}
}
}
}





http://www.pastebin.ca/1373438

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

static int max = 0;
public static IEnumerable<bool> infiniteAlternatingBools()
{

bool cur = false;
while(true)
{
yield return cur;
cur = !cur;
max++;
if(max > 10)
throw new ArgumentException("More than 10 executions");
}
}

public static void Main(string[] args)
{
try {
foreach(bool b in infiniteAlternatingBools())
{
Console.WriteLine(b);
}
} catch(ArgumentException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("AllDone");
}
}
}
}





http://www.pastebin.ca/1373452

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

static int max = 0;
public static IEnumerable<bool> infiniteAlternatingBools()
{

bool cur = false;
while(true)
{
yield return cur;
cur = !cur;
max++;
if(max > 10)
throw new ArgumentException("More than 10 executions");
}
}

public static void Main(string[] args)
{
try {
foreach(bool b in infiniteAlternatingBools())
{
Console.WriteLine(b);
}
}
catch(ThreadExceptoin ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
catch(OutOfMemoryException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
catch(SomeOtherException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw;
}
finally
{
Console.WriteLine("AllDone");
}
}
}
}





http://www.pastebin.ca/1373462

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Lesson07
{
class main
{
public static IEnumerable<char> MyChars()
{
yield return 'A';
yield return 'B';
yield return 'C';
yield return 'D';
}

static int max = 0;
public static IEnumerable<bool> infiniteAlternatingBools()
{

bool cur = false;
while(true)
{
yield return cur;
cur = !cur;
max++;
if(max > 10)
throw new ArgumentException("More than 10 executions");
}
}

public static void Main(string[] args)
{
try {
foreach(bool b in infiniteAlternatingBools())
{
Console.WriteLine(b);
}
}
catch(ThreadException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
catch(OutOfMemoryException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
catch(SomeOtherException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw;
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw new SomeOtherException("this is what really happend", ex);
}
finally
{
Console.WriteLine("AllDone");
}
}
}
}
Reply all
Reply to author
Forward
0 new messages