OSG Community College C# for Programmers Class #6 Log 03/17/2009

0 views
Skip to first unread message

AdelleF

unread,
Mar 24, 2009, 3:11:21 PM3/24/09
to OSG Community College
Pastebins at the bottom ;-)

[2009/03/17 12:18] Adelle Fitzgerald: hiya :)
[2009/03/17 12:18] M1sha Dallin: The specific calculator is at
http://www.pastebin.ca/1363689
[2009/03/17 12:18] Starky Rubble: M1sha just told us shes doing a
full on expression parsing calculator
[2009/03/17 12:18] Adelle Fitzgerald: have i missed much?
[2009/03/17 12:18] Snowdrop Short: M1sha, I'd love to look it over
with you
[2009/03/17 12:19] Snowdrop Short: but maybe we should do it later?
[2009/03/17 12:19] M1sha Dallin: I was just trying to generalise
Token and Tokenize
[2009/03/17 12:19] M1sha Dallin: yep
[2009/03/17 12:19] M1sha Dallin: the class may answer my questions
[2009/03/17 12:19] Snowdrop Short: ok .. my agenda for today is two
things
[2009/03/17 12:20] Snowdrop Short: first a bit about visibility and
encapsulation
[2009/03/17 12:20] Snowdrop Short: and then generics 101
[2009/03/17 12:20] Adelle Fitzgerald nods
[2009/03/17 12:21] Snowdrop Short: encapsulation is an fundamental
for code reuse
[2009/03/17 12:22] Snowdrop Short: what you try to do is to make your
class more generic, in order to make it easier to reuse it in other
places
[2009/03/17 12:22] Snowdrop Short: like the Dictionary class we
looked at in the framework
[2009/03/17 12:22] Snowdrop Short: or at least talked a bit about
[2009/03/17 12:23] Snowdrop Short: all we know about it is that it
has some methods for storing and finding other objects
[2009/03/17 12:23] Snowdrop Short: how it works internally is hidden
[2009/03/17 12:23] Snowdrop Short: and the ability to hide
information is actually important
[2009/03/17 12:24] Snowdrop Short: because if you know something
about the internals, you get tempted to put that information to good
use
[2009/03/17 12:25] Snowdrop Short: that is especially important in
the .net and java world, where so many modules are being made, with
the sole purpose of being reused
[2009/03/17 12:26] Snowdrop Short: if you look at our "GroupBase"
class
[2009/03/17 12:26] Snowdrop Short: there is free access to the ID
property
[2009/03/17 12:27] Snowdrop Short: if we look at groupbase and group
and person as an overall solution
[2009/03/17 12:27] Snowdrop Short: which can be reused
[2009/03/17 12:28] Snowdrop Short: then what happens if the "user"
adds a person to a group
[2009/03/17 12:28] Snowdrop Short: and then changes the id
[2009/03/17 12:28] Adelle Fitzgerald: do you have a pastebin for the
code?
[2009/03/17 12:29] Snowdrop Short: yes .. hold on
[2009/03/17 12:30] Adelle Fitzgerald: oh, its ok if you havent posted
anything yet, i didnt know if we was working off anything yet
[2009/03/17 12:31] Snowdrop Short: http://www.pastebin.ca/1363698
[2009/03/17 12:31] Adelle Fitzgerald: thnkies
[2009/03/17 12:31] Snowdrop Short: it is the same code as last
tuesday.. more or less
[2009/03/17 12:32] Adelle Fitzgerald: kk, i installed win 7 over the
weekend, so i ahve stuff all over the place still
[2009/03/17 12:32] Snowdrop Short: :-(
[2009/03/17 12:32] Snowdrop Short: that's always a problem
[2009/03/17 12:32] Adelle Fitzgerald: its not bad, my os is very
clean, just dumped a load of stuff on other drives though hehe
[2009/03/17 12:33] Snowdrop Short: so if we plan on using the Id as
an immutable id for a user or avatar
[2009/03/17 12:34] Snowdrop Short: then we really need to protect our
selves
[2009/03/17 12:34] Snowdrop Short: and c# just like any other OO
language gives us some tools we can use
[2009/03/17 12:34] Snowdrop Short: until today we've always used the
keyword "public"
[2009/03/17 12:34] Starky Rubble: ok
[2009/03/17 12:35] Snowdrop Short: on almost everything we've made
[2009/03/17 12:35] Snowdrop Short: that's because the default for c#
is that everything is hidden
[2009/03/17 12:36] Snowdrop Short: meaning that if instead of having
"public Guid ID" in line 16
[2009/03/17 12:36] Snowdrop Short: we had "Guid ID"
[2009/03/17 12:36] Snowdrop Short: without the public
[2009/03/17 12:37] Snowdrop Short: then only methods inside the
GroupBase class would be allowed to access the ID property
[2009/03/17 12:38] Snowdrop Short: the same thing goes for Name
[2009/03/17 12:38] Snowdrop Short: that is also publicly available
[2009/03/17 12:38] Adelle Fitzgerald: so using public string Name;
makes it 'public' to the rest of the classes?
[2009/03/17 12:38] Snowdrop Short: but if I had left it out or used
the keyword "private", then it would not be useable outside the class
[2009/03/17 12:38] Adelle Fitzgerald nods
[2009/03/17 12:38] Snowdrop Short: yes, public to all other classes
[2009/03/17 12:39] Snowdrop Short: remember the default is "private"
[2009/03/17 12:39] Snowdrop Short: so I really don't need to use it
[2009/03/17 12:39] Snowdrop Short: so
[2009/03/17 12:39] Snowdrop Short: "Private Guid ID" and "Guid ID"
would have the same meaning
[2009/03/17 12:39] Adelle Fitzgerald: gotcha
[2009/03/17 12:39] Snowdrop Short: sorry
[2009/03/17 12:39] Snowdrop Short: "private Guid ID"
[2009/03/17 12:39] Snowdrop Short: no capitals
[2009/03/17 12:40] Adelle Fitzgerald: hehe
[2009/03/17 12:40] Starky Rubble: heh... I like it explicily stated
tho
[2009/03/17 12:40] Adelle Fitzgerald: ok, why would we want to keep
something private from the other classes?
[2009/03/17 12:40] Starky Rubble: so you cant muck with it
[2009/03/17 12:41] Snowdrop Short: stating it explicitly usually
indicates that you have thought about it, and decided that it really
should be private
[2009/03/17 12:41] Adelle Fitzgerald: right, so it like a form of
protection
[2009/03/17 12:41] Snowdrop Short: yes, a form of protectection
[2009/03/17 12:41] Adelle Fitzgerald: okies, i understand that :)
[2009/03/17 12:41] Adelle Fitzgerald: hello rodney
[2009/03/17 12:41] Snowdrop Short: even protecting yourself from
yourself :)
[2009/03/17 12:42] Snowdrop Short: hi Rodney
[2009/03/17 12:42] rodney foobar: hi
[2009/03/17 12:42] Snowdrop Short: not all seats work :-(
[2009/03/17 12:42] Starky Rubble: use one of the sit balls
[2009/03/17 12:42] Starky Rubble: there ya go lol
[2009/03/17 12:42] rodney foobar: ah thanks
[2009/03/17 12:43] rodney foobar: if this is peer to peer does it
mean others have various vectors to attack -- how secure is this ?
[2009/03/17 12:43] Snowdrop Short: ?
[2009/03/17 12:43] Starky Rubble: hunh?
[2009/03/17 12:43] Snowdrop Short: ehh .. you lost me there rodney
[2009/03/17 12:43] Starky Rubble: This is a C# class
[2009/03/17 12:44] rodney foobar: we can attach scripts to objects
etc ? and transfer chars to other peoples servers ?
[2009/03/17 12:44] Adelle Fitzgerald: this is a C# class, if you are
after help try IRC
[2009/03/17 12:44] rodney foobar: ah ok sorry
[2009/03/17 12:44] Adelle Fitzgerald: :)
[2009/03/17 12:44] Snowdrop Short smiles
[2009/03/17 12:44] Starky Rubble: either #osgrid or #opensim
[2009/03/17 12:45] Snowdrop Short: but to answer your question, you
are as secure as you configure your sim :-) .. more or less
[2009/03/17 12:45] rodney foobar: oh ok thanks
[2009/03/17 12:45] rodney foobar: i have no sim
[2009/03/17 12:45] rodney foobar: do i need oen ?
[2009/03/17 12:45] Starky Rubble: youre safe man
[2009/03/17 12:45] Snowdrop Short: no
[2009/03/17 12:45] rodney foobar: ah ok cool thanks :)
[2009/03/17 12:46] Starky Rubble: try poking around wright plaza or
lbsa maybe
[2009/03/17 12:46] Snowdrop Short: but in reality "private" is a good
friend
[2009/03/17 12:46] Adelle Fitzgerald: and take a look here:
http://opensimulator.org/wiki/Main_Page it will point you in all the
right directions
[2009/03/17 12:46] Snowdrop Short: because if you have written a ton
of code
[2009/03/17 12:46] Snowdrop Short: and then decide to change a class
[2009/03/17 12:47] Snowdrop Short: all you have to worry about in
other classes is the stuff which is available for the other classes
[2009/03/17 12:47] Snowdrop Short: so if you only have two public
methods
[2009/03/17 12:47] Snowdrop Short: then you can change all the
others, with out having to worry about breaking your code
[2009/03/17 12:48] Snowdrop Short: as long as the two public methods
were unchanged and still did the same thing
[2009/03/17 12:48] Snowdrop Short: like the Add and Remove method on
the dictionary class
[2009/03/17 12:48] rodney foobar: doesnt that not really work in
practive tho ? as the public methods are quite entangled with teh
private stuff.. aka side effeects ?
[2009/03/17 12:48] Snowdrop Short: you can change all the internals
to your hearts content, without worrying about having to change the
rest of your code
[2009/03/17 12:49] Snowdrop Short: as long as the signature and
semantics of the public methods remains unchanged
[2009/03/17 12:49] Snowdrop Short: you should be safe
[2009/03/17 12:49] rodney foobar: but if alot of the private stuff is
changed its often subtly changed
[2009/03/17 12:49] Snowdrop Short: the key is "semantics" of the
public method
[2009/03/17 12:50] rodney foobar: small public api ?
[2009/03/17 12:50] rodney foobar: standardised, concise, simple ?
[2009/03/17 12:50] rodney foobar: atomic ?
[2009/03/17 12:50] rodney foobar: lego programming ?
[2009/03/17 12:50] Snowdrop Short: this is a development class not,
design class :-)
[2009/03/17 12:51] rodney foobar: aye the design of the current dev
system is serious problem tho ~?
[2009/03/17 12:51] Snowdrop Short: or programming class actually
[2009/03/17 12:51] rodney foobar: in fact the best solution is to
implement an simulation to actually code in >?
[2009/03/17 12:52] Snowdrop Short: ok ... back to visibilty
[2009/03/17 12:52] Starky Rubble: yes, I am feeling off-track here///
lol
[2009/03/17 12:52] Snowdrop Short: as a general rule of thumb, the
more you can make private, the better
[2009/03/17 12:53] Adelle Fitzgerald nods
[2009/03/17 12:53] rodney foobar: yerp
[2009/03/17 12:53] Snowdrop Short: because the less risk you have
when changing the class
[2009/03/17 12:54] Snowdrop Short: but ... unfortunately sometimes
you need things to be a little more nuanced
[2009/03/17 12:54] Snowdrop Short: sometimes public is too much and
private is to restricted
[2009/03/17 12:54] Starky Rubble: ahhh
[2009/03/17 12:55] Starky Rubble: you want it safe but to still see
it maybe
[2009/03/17 12:55] Snowdrop Short: something like that
[2009/03/17 12:55] Snowdrop Short: if you look at our person class
[2009/03/17 12:55] Adelle Fitzgerald: so, if we have a bunch of
variables that are required within several classes, and say a
delegate, then they would need to be public, or they would not work?
[2009/03/17 12:55] Snowdrop Short: it'll give you and updated
version .. hold on
[2009/03/17 12:59] Snowdrop Short: http://www.pastebin.ca/1363723
[2009/03/17 12:59] Snowdrop Short: take a look at the code
[2009/03/17 12:59] Snowdrop Short: try to copy and paste it into your
IDE
[2009/03/17 12:59] Adelle Fitzgerald: got it, thanks
[2009/03/17 13:00] M1sha Dallin: yep
[2009/03/17 13:00] Snowdrop Short: and then run it
[2009/03/17 13:00] Starky Rubble: ok
[2009/03/17 13:00] Snowdrop Short: as you can see
[2009/03/17 13:00] Snowdrop Short: in lines 16 and 17
[2009/03/17 13:01] Snowdrop Short: I've used the keyword "protected"
[2009/03/17 13:01] Snowdrop Short: if you change it to "private"
[2009/03/17 13:01] Snowdrop Short: you get all sorts of errors during
compilation
[2009/03/17 13:01] Snowdrop Short: try it ...
[2009/03/17 13:02] Starky Rubble: right
[2009/03/17 13:02] Adelle Fitzgerald: 'Groups.GroupBase.Name' is
inaccessible due to its protection level
[2009/03/17 13:02] Snowdrop Short: yes
[2009/03/17 13:03] Snowdrop Short: because the property "Name" can
only be accessed from from "GroupBase"
[2009/03/17 13:04] Adelle Fitzgerald: so the public class FullName
can not access 'Name'
[2009/03/17 13:04] Snowdrop Short: so FullName in person cannot
access it
[2009/03/17 13:04] Adelle Fitzgerald nods
[2009/03/17 13:05] Snowdrop Short: it is only accessable inside
GroupBase, so the "ToString" and "WriteToConsole" methods
[2009/03/17 13:05] Snowdrop Short: inside that class works fine
[2009/03/17 13:05] Snowdrop Short: but .. all the other classes are
denied access by the compiler
[2009/03/17 13:06] Snowdrop Short: when we use the keyword
"protected"
[2009/03/17 13:06] Snowdrop Short: it means that generally it isn't
accessible
[2009/03/17 13:06] Snowdrop Short: but any subclass is allowed to
access the method
[2009/03/17 13:06] Snowdrop Short: or property
[2009/03/17 13:07] Snowdrop Short: so when we change the visibility
to "protected" everything works
[2009/03/17 13:07] Snowdrop Short: because both "Person" and "Group"
inherits from "GroupBase"
[2009/03/17 13:08] Snowdrop Short: so they have full access to "ID"
and "Name"
[2009/03/17 13:08] Adelle Fitzgerald nods
[2009/03/17 13:08] Starky Rubble: ok
[2009/03/17 13:08] Snowdrop Short: but the Main method cannot
[2009/03/17 13:09] Starky Rubble: right
[2009/03/17 13:09] Snowdrop Short: anything inside "MainClass" can
only access the publicly availabe properties and methods
[2009/03/17 13:10] Snowdrop Short: so, right now we've kind of
decoupled "MainClass" from "GroupBase", "Person" and "Group"
[2009/03/17 13:10] Snowdrop Short: we can do quite a few changes
inside those classes without forcing a change to "MainClass"
[2009/03/17 13:11] Snowdrop Short: unfortunately any change to "ID"
and "Name" inside GroupBase
[2009/03/17 13:11] Snowdrop Short: may cause problems in "Person" and
"Group"
[2009/03/17 13:12] Snowdrop Short: because of the protected access
[2009/03/17 13:12] Snowdrop Short: hence ...
[2009/03/17 13:12] Snowdrop Short: the more things you can make
private
[2009/03/17 13:12] Snowdrop Short: the better
[2009/03/17 13:13] Snowdrop Short: there is one more level of
visibilty
[2009/03/17 13:13] Snowdrop Short: where you can give access to
certain classes, even though they don't inherit from your base class
[2009/03/17 13:14] Snowdrop Short: it is rarely used, but it happens
[2009/03/17 13:14] Snowdrop Short: it is called "internal"
[2009/03/17 13:14] Snowdrop Short: meaning that any class which is in
the same namespace can have access
[2009/03/17 13:15] Snowdrop Short: so since "MainClass" and
"GroupBase" are in the same namespace
[2009/03/17 13:15] Snowdrop Short: they could access each others
internals
[2009/03/17 13:15] Snowdrop Short: so to take them in order of safety
and scope of visibility
[2009/03/17 13:16] Snowdrop Short: "protected" (the default)
"protected", "internal" and fially "public"
[2009/03/17 13:16] Starky Rubble: private protected internal oublic
[2009/03/17 13:17] Snowdrop Short: the more stuff you can make less
visible, the less maintenance you'll have to do in the long run
[2009/03/17 13:17] Starky Rubble: ok
[2009/03/17 13:18] Snowdrop Short: most oo languages have one more
[2009/03/17 13:18] Adelle Fitzgerald: so, the more you use private,
the more chance your code is better too and things wont be sprawled
all around other classes and such?
[2009/03/17 13:18] Snowdrop Short: fortuantely (one of the things I
love about c#) c# doesn't have that one
[2009/03/17 13:18] Snowdrop Short: yes
[2009/03/17 13:18] Adelle Fitzgerald: goctha
[2009/03/17 13:19] Snowdrop Short: some languages have a keyword
called friend
[2009/03/17 13:19] Snowdrop Short: which allows you to declare the
name of a specific class which should have "private" access to the
internals
[2009/03/17 13:19] Snowdrop Short: fortunately that doesn't really
exist in c#
[2009/03/17 13:20] Snowdrop Short: besides I've always felt that
"friend" was a mis-nomer
[2009/03/17 13:20] Snowdrop Short: is should have been called
"enenmy"
[2009/03/17 13:20] Adelle Fitzgerald: lol
[2009/03/17 13:20] Snowdrop Short: because it encourages entanglement
between classes
[2009/03/17 13:20] Adelle Fitzgerald: right
[2009/03/17 13:21] Starky Rubble: ok
[2009/03/17 13:21] Snowdrop Short: so from now on, when we look at
code, be prepared to look for "protected" and "public"
[2009/03/17 13:21] Snowdrop Short: and ask your selves, why is that
public
[2009/03/17 13:21] Snowdrop Short: or protected
[2009/03/17 13:22] Adelle Fitzgerald nods
[2009/03/17 13:22] Snowdrop Short: we need to cover one more thing
about visibility
[2009/03/17 13:22] Snowdrop Short: because .. we need some more
granularity
[2009/03/17 13:23] Snowdrop Short: a method can either be accessible
or not
[2009/03/17 13:23] Snowdrop Short: i.e. either it can be called
externally or not
[2009/03/17 13:23] Snowdrop Short: but prooerties
[2009/03/17 13:23] Snowdrop Short: like "ID" and "Name"
[2009/03/17 13:23] Snowdrop Short: if you have access, the you can
both read and write
[2009/03/17 13:24] Snowdrop Short: but it would be nice to be able to
restrict things to be only readable from time to tim
[2009/03/17 13:24] Snowdrop Short: time, even
[2009/03/17 13:24] Snowdrop Short: e.g. our ID property
[2009/03/17 13:24] Snowdrop Short: making it so that the Name was
publicly availabe for reading
[2009/03/17 13:24] Snowdrop Short: would be nice
[2009/03/17 13:25] Snowdrop Short: and making writing the ID private
would be nice
[2009/03/17 13:25] Snowdrop Short: and c# is one of the few languages
I know, which has that capability
[2009/03/17 13:26] Snowdrop Short: take a look at line 34
[2009/03/17 13:26] Snowdrop Short: it's a bit of an oddity
[2009/03/17 13:27] Snowdrop Short: we have a something which looks
like a mix between a property declaration and a method declaration
[2009/03/17 13:27] Snowdrop Short: what it really says is
[2009/03/17 13:28] Snowdrop Short: "there is public "get" (read)
access to a property called "FullName", which has a type of "string"
and the way to calculate that propery is in the braces
[2009/03/17 13:29] Adelle Fitzgerald: right, so essentially making
Name read only
[2009/03/17 13:29] Adelle Fitzgerald: to the erst of the classes that
is
[2009/03/17 13:29] Adelle Fitzgerald: *rest
[2009/03/17 13:29] Snowdrop Short: yes
[2009/03/17 13:30] Snowdrop Short: pastebinning right now
[2009/03/17 13:32] Snowdrop Short: http://www.pastebin.ca/1363752
[2009/03/17 13:32] Snowdrop Short: take a look at the GroupBase class
[2009/03/17 13:33] Snowdrop Short: as you can see, I have renamed the
actual properties, and changed their visibility to private
[2009/03/17 13:33] Snowdrop Short: meaning ... only access internally
to GroupBase
[2009/03/17 13:34] Snowdrop Short: and then I have defined two things
which is casses accessors
[2009/03/17 13:34] Snowdrop Short: for accessing the properties
[2009/03/17 13:35] Snowdrop Short: I have given them the same name as
the old properties
[2009/03/17 13:35] Adelle Fitzgerald: so with that we can use name
and ID, and whatever we do with them will have no effect on the
original properties
[2009/03/17 13:35] Snowdrop Short: more or less
[2009/03/17 13:35] Snowdrop Short: yes
[2009/03/17 13:35] Adelle Fitzgerald nods
[2009/03/17 13:35] Starky Rubble: ok
[2009/03/17 13:35] Snowdrop Short: if you look at ID
[2009/03/17 13:35] Snowdrop Short: the general rule is "public"
[2009/03/17 13:36] Snowdrop Short: which is used on the "getter"
[2009/03/17 13:36] Snowdrop Short: but the "setter" is marked as
"private"
[2009/03/17 13:36] Snowdrop Short: this means that inside GroupBase
[2009/03/17 13:37] Snowdrop Short: I could say "this.ID =
Guid.NewGuid"
[2009/03/17 13:37] Snowdrop Short: and the code in the "set" block
would be executed
[2009/03/17 13:38] Snowdrop Short: the name "value" refers to what is
on the right side of =
[2009/03/17 13:38] Snowdrop Short: looking at Name
[2009/03/17 13:38] Snowdrop Short: again .. "getting" Name is public
[2009/03/17 13:38] Snowdrop Short: but setting it is "private"
[2009/03/17 13:39] Snowdrop Short: incidently you can have any amount
of code in the "setter" and "getter"
[2009/03/17 13:39] Snowdrop Short: but I kind of discourage that
[2009/03/17 13:39] Snowdrop Short: because it kind of implies a very
simple assignment of a value
[2009/03/17 13:40] Snowdrop Short: so you see
[2009/03/17 13:40] Snowdrop Short: we can now controll read and write
access to properies
[2009/03/17 13:40] Starky Rubble: right
[2009/03/17 13:41] M1sha Dallin: and 'readonly'? It wouldbe useful
for the id field?
[2009/03/17 13:41] Snowdrop Short: yes
[2009/03/17 13:41] Snowdrop Short: because, you'd very likely want to
restrict changing the ID
[2009/03/17 13:42] M1sha Dallin: yep - protect myself from myself
[2009/03/17 13:42] Snowdrop Short: always
[2009/03/17 13:42] Starky Rubble: heh
[2009/03/17 13:42] Snowdrop Short: the less opportunity you have for
making a bug ...
[2009/03/17 13:42] Adelle Fitzgerald: so, only the Groupbase class
can set the ID, and any sub class can set the name, but any class can
read the ID and name
[2009/03/17 13:42] Snowdrop Short: yes
[2009/03/17 13:43] Starky Rubble: right
[2009/03/17 13:43] Snowdrop Short: that is exactly what we've defined
here
[2009/03/17 13:43] Adelle Fitzgerald: cool
[2009/03/17 13:43] Adelle Fitzgerald: i dont know quite how I would
use that, but its cool :)
[2009/03/17 13:44] Adelle Fitzgerald: i suppose for the ID, you would
only want one class in the entire program to be able to set it
[2009/03/17 13:44] Snowdrop Short: when the program starts to grow in
size, sometimes you worry about "breaking changes"
[2009/03/17 13:44] M1sha Dallin: only yourself - once - on creation
[2009/03/17 13:45] Snowdrop Short: where a chagne in one place
triggers a cascade of changes all over the place
[2009/03/17 13:45] Snowdrop Short: the more things you have which is
private or protected one way or another
[2009/03/17 13:45] Starky Rubble: makes sense
[2009/03/17 13:45] Snowdrop Short: the less the likelihood that that
part will cause problems if you change it
[2009/03/17 13:46] Snowdrop Short: on a small program, it's not a big
deal
[2009/03/17 13:46] Snowdrop Short: but once the number of classes
grows into the thousands
[2009/03/17 13:46] Snowdrop Short: and the number of lines reaches
millions
[2009/03/17 13:46] Snowdrop Short: it's quite important
[2009/03/17 13:47] Starky Rubble: looking for the proverbial needle
lol
[2009/03/17 13:47] Snowdrop Short: yes
[2009/03/17 13:48] Adelle Fitzgerald: ok, quick question, and please
excuse my dumbness, i just want to confirm something... in something
like opensim, there are lots and lots of .cs files, i take it they are
all different namespaces, and they are all kinda little parts to the
actual program... they are called upon by like namespace.this etc but
are compiled into one actual executable?
[2009/03/17 13:48] Adelle Fitzgerald: but variables are passed back
and forth between all the different namespaces
[2009/03/17 13:49] Snowdrop Short: well, usually 4-5-6-7 .cs files
form a namespace
[2009/03/17 13:49] Snowdrop Short: a good practice is one class
per .cs file
[2009/03/17 13:49] Adelle Fitzgerald: right
[2009/03/17 13:49] Snowdrop Short: more or less, there are exceptions
[2009/03/17 13:49] Starky Rubble: using system is using a namespace
right?
[2009/03/17 13:49] Snowdrop Short: System is a namespace
[2009/03/17 13:50] Snowdrop Short: Groups in our example is a
namespace
[2009/03/17 13:50] Starky Rubble: sure and we pass stuff in and out
willy nilly
[2009/03/17 13:50] Snowdrop Short: actually OpenSim consists of a lot
of executables
[2009/03/17 13:51] Starky Rubble: Wee see them in /bin
[2009/03/17 13:51] Snowdrop Short: I believe somewhere around 70
[2009/03/17 13:51] Adelle Fitzgerald: so, without going into it too
much, you would ahve a create group class, on its own, and a delete
group class on its own too, both would use another class where the
name and ID was protected, and that way you could work on the create
and delete classes safely
[2009/03/17 13:52] Snowdrop Short: yes .. and no
[2009/03/17 13:52] Snowdrop Short: you'd want to lump things which
are related closely together
[2009/03/17 13:52] Snowdrop Short: so maybe one namespace called
Groups
[2009/03/17 13:52] Snowdrop Short: a class for "Person"
[2009/03/17 13:52] Snowdrop Short: a class for "Group"
[2009/03/17 13:53] Snowdrop Short: but since we discovered some
commonalities
[2009/03/17 13:53] Snowdrop Short: we introduced a third class
[2009/03/17 13:53] Snowdrop Short: for holding "Name" and "ID", since
that was used by both
[2009/03/17 13:53] Snowdrop Short: remember "DRY"
[2009/03/17 13:53] Adelle Fitzgerald: yes
[2009/03/17 13:54] Snowdrop Short: so you'd likely want to have
AddPerson and RemovePerson as methods on the group
[2009/03/17 13:54] Snowdrop Short: and then maybe a thrid/fourth
class
[2009/03/17 13:54] Snowdrop Short: for holding on to all Groups
[2009/03/17 13:54] Adelle Fitzgerald: gotcha
[2009/03/17 13:54] Snowdrop Short: and methods for adding and
removing groups to that "GroupCollection"
[2009/03/17 13:55] Snowdrop Short: but what things goes where, really
is an art
[2009/03/17 13:55] Snowdrop Short: and falls somewhere between
programming and design and archtecture
[2009/03/17 13:55] Starky Rubble: right
[2009/03/17 13:56] Snowdrop Short: it requires that you both know how
to program and knows the problem your program is supposed to solve
[2009/03/17 13:56] Adelle Fitzgerald nods
[2009/03/17 13:56] Snowdrop Short: if you don't know anything about
train scheduling
[2009/03/17 13:57] Snowdrop Short: you can be the best programmer in
the world and still not be able to write a propper train scheduling
application
[2009/03/17 13:57] Snowdrop Short: and vice versa
[2009/03/17 13:57] Starky Rubble: sure
[2009/03/17 13:58] Starky Rubble: Generics next time? or are we going
on...
[2009/03/17 13:58] Snowdrop Short: I think we'll have to post pone
generics
[2009/03/17 13:58] M1sha Dallin: yep - it's late
[2009/03/17 13:59] Snowdrop Short: I was a bit longer at presenting
what I wanted to present than I anticipated
[2009/03/17 13:59] Snowdrop Short: M1sha
[2009/03/17 13:59] M1sha Dallin: Hi
[2009/03/17 13:59] Starky Rubble: lol I need to make it to the bank
[2009/03/17 13:59] M1sha Dallin: I'll do some more reading
[2009/03/17 13:59] Snowdrop Short: I'll have a look your program
[2009/03/17 13:59] Snowdrop Short: and see what I can figure out
[2009/03/17 14:00] M1sha Dallin: ok - I'll break out the classes for
Thursday
[2009/03/17 14:00] M1sha Dallin: since it's the Thursday problem :-)
[2009/03/17 14:00] Snowdrop Short nods
[2009/03/17 14:00] Starky Rubble: all righty then... Thanks Snowdrop
see you Thirsday
[2009/03/17 14:01] Snowdrop Short: ok ...
[2009/03/17 14:01] M1sha Dallin: but C# seems to prefer strings to
some other types it seems
[2009/03/17 14:01] Snowdrop Short: thanks for listening in on me
babbling
[2009/03/17 14:01] Starky Rubble: bye
[2009/03/17 14:01] M1sha Dallin: ty Snowdrop
[2009/03/17 14:01] rodney foobar: thanks for teh talk :)
[2009/03/17 14:01] Snowdrop Short: have a good night/day everyone


http://www.pastebin.ca/1363689

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

namespace Calculator
{
//
----------------------------------------------------------------------------
public class ExpressionCalculator
{
// Basic type definitions for Postfix conversion
private enum TokenType
{
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>\^)|";

// Basic element of an expression
private class Token
{
public TokenType m_Index;
public Double m_Value;

public Token(TokenType index, Double value)
{
m_Index = index;
m_Value = value;
}
}

// Structures for representing and converting an expression
private class TokenList : List<Token> {}
private class TokenStack : Stack<Token> {}


// Method: Tokenize - analyses the input string and breaks it
into a list of tokens
// against a regular expression
// input: string
// return: TokenList
//
private static TokenList Tokenize(string theString)
{
Regex Re = new Regex(CalculatorPattern);
TokenList TheList = new TokenList();
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)
{
if ((TokenType)i == TokenType.Operand)
TheList.Add(new Token((TokenType)i,
Double.Parse(matchValue)));
else
TheList.Add(new Token((TokenType)i, 0.0));
}
i++;
}
}
return TheList;
}

// 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(TokenList infixList, out
TokenList postfixList)
{
TokenStack OperandStack = new TokenStack();
TokenList PostfixList = new TokenList();
Boolean ExpectOperand = true;

postfixList = PostfixList;

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

case TokenType.Op_Minus:
case TokenType.Op_Plus:
case TokenType.Op_Multiply:
case TokenType.Op_Divide:
case TokenType.Op_Exponentiate:
if (ExpectOperand && token.m_Index ==
TokenType.Op_Minus) token.m_Index = TokenType.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 TokenType.Op_OpenParen:
OperandStack.Push(token);
break;

case TokenType.Op_CloseParen:
while (OperandStack.Peek().m_Index !=
TokenType.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(TokenList
postfixList, out Double result)
{
Stack<Token> ProcessStack = new Stack<Token>();
Token OperA;

foreach (Token token in postfixList)
{
switch (token.m_Index)
{
case TokenType.Operand:
ProcessStack.Push(token);
break;

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

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

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

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

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

case TokenType.Op_Exponentiate:
OperA = 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;
}
}

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)
{
TokenList InfixList = new TokenList();
TokenList PostfixList = new TokenList();

Boolean status;
Double ExpressionResult;

InfixList = Tokenize(expression);
if (ToPostfix(InfixList, out PostfixList))
{
status = EvaluatePostfixList(PostfixList, out
ExpressionResult);

result = ExpressionResult;
return status;
}

result = 0.0;
return false;
}

}

//
----------------------------------------------------------------------------
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(string[] args)
{
string CalcString;

Double Answer = 0.0;
Boolean Converted = false;

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)
Console.WriteLine("\nThe expression \"{0}\"
evaluated to: {1}", CalcString, Answer);
else
Console.WriteLine("\nThe expression \"{0}\"
contained errors", CalcString);
} while (true == true);
}
}
}





http://www.pastebin.ca/1363698

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

namespace Groups
{
public class GroupBase
{

public GroupBase(Guid ID, string name)
{
this.ID = ID;
this.Name = name;
}

public Guid ID;
public string Name;

override public string ToString()
{
return string.Format("ID={0} Name={1}", ID, Name);
}

// Write Bae Details on the Console
public void WriteToConsole()
{
Console.WriteLine(ToString());
}

}

public class Person : GroupBase
{
public string FullName { get { return base.Name; } }

public Person(string name, Guid id) : base(id, name)
{
}
}

public class Group : GroupBase
{
public delegate void PersonInfo(string name);
public delegate string AdditionalInfo(int n);
// Constructor
public Group(string name, string description) : base
(Guid.NewGuid(), name)
{
this._Description = description;
this._PermittedRoles = new ArrayList();
this._Members = new ArrayList();

}

public event PersonInfo OnNewPerson;

public void
AddPerson(Person p)
{
_Members.Add(p);
OnNewPerson(p.Name);
}

public new string ToString()
{
return string.Format("Description={0}, #of Members {1}",
_Description, _Members.Count);
}

// Write Group Details on the Console
public void WriteToConsole(AdditionalInfo message)
{
base.WriteToConsole();
Console.WriteLine(ToString() + message(5));
foreach (Group GroupMember in _Members)
GroupMember.WriteToConsole();
}

// Type definitions for the Group Structure
public string _Name;
public string _Description;
public ArrayList _PermittedRoles;
public ArrayList _Members;

}

class MainClass
{
static void OnNewPerson(string name)
{
Console.WriteLine("New person added:" + name);
}

public static void Main(string[] args)
{
int m = 7;
Group.AdditionalInfo bubble = delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
};

Group.AdditionalInfo b2;

b2 = bubble;
GroupBase b = new GroupBase(Guid.NewGuid(), "Base Group");
Console.WriteLine("--------------------------------");
Console.WriteLine(b.ToString());
Console.WriteLine("--------------------------------");
b.WriteToConsole();
Console.WriteLine("--------------------------------");

Group g = new Group("The real group", "For cool people");
g.OnNewPerson += OnNewPerson;
Console.WriteLine(g.ToString());
Console.WriteLine("--------------------------------");
g.WriteToConsole(delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
});
Console.WriteLine("--------------------------------");
object o = g;
Console.WriteLine(o.ToString());
g.OnNewPerson -= OnNewPerson;
}
}
}




http://www.pastebin.ca/1363723

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

namespace Groups
{
public class GroupBase
{

public GroupBase(Guid ID, string name)
{
this.ID = ID;
this.Name = name;
}

protected Guid ID;
protected string Name;

override public string ToString()
{
return string.Format("ID={0} Name={1}", ID, Name);
}

// Write Bae Details on the Console
public void WriteToConsole()
{
Console.WriteLine(ToString());
}

}

public class Person : GroupBase
{
public string FullName { get { return base.Name; } }

public Person(string name, Guid id) : base(id, name)
{
}
}

public class Group : GroupBase
{
public delegate void PersonInfo(string name);
public delegate string AdditionalInfo(int n);
// Constructor
public Group(string name, string description) : base
(Guid.NewGuid(), name)
{
this._Description = description;
this._PermittedRoles = new ArrayList();
this._Members = new ArrayList();

}

public event PersonInfo OnNewPerson;

public void AddPerson(Person p)
{
_Members.Add(p);
OnNewPerson(p.FullName);
}

public new string ToString()
{
return string.Format("Description={0}, #of Members {1}",
_Description, _Members.Count);
}

// Write Group Details on the Console
public void WriteToConsole(AdditionalInfo message)
{
base.WriteToConsole();
Console.WriteLine(ToString() + message(5));
foreach (Group GroupMember in _Members)
GroupMember.WriteToConsole();
}

// Type definitions for the Group Structure
public string _Name;
public string _Description;
public ArrayList _PermittedRoles;
public ArrayList _Members;

}

class MainClass
{
static void OnNewPerson(string name)
{
Console.WriteLine("New person added:" + name);
}

public static void Main(string[] args)
{
int m = 7;
Group.AdditionalInfo bubble = delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
};

Group.AdditionalInfo b2;

b2 = bubble;
GroupBase b = new GroupBase(Guid.NewGuid(), "Base Group");
Console.WriteLine("--------------------------------");
Console.WriteLine(b.ToString());
Console.WriteLine("--------------------------------");
b.WriteToConsole();
Console.WriteLine("--------------------------------");

Group g = new Group("The real group", "For cool people");
g.OnNewPerson += OnNewPerson;
Console.WriteLine(g.ToString());
Console.WriteLine("--------------------------------");
g.WriteToConsole(delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
});
Console.WriteLine("--------------------------------");
object o = g;
Console.WriteLine(o.ToString());
g.OnNewPerson -= OnNewPerson;
}
}
}




http://www.pastebin.ca/1363752

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

namespace Groups
{
public class GroupBase
{

public GroupBase(Guid ID, string name)
{
this._ID = ID;
this._Name = name;
}

public Guid ID
{
get { return _ID; }
private set { _ID = value; }
}

public string Name
{
get { return _Name; }
protected set { Name = value; }
}

private Guid _ID;
private string _Name;

override public string ToString()
{
return string.Format("ID={0} Name={1}", ID, Name);
}

// Write Bae Details on the Console
public void WriteToConsole()
{
Console.WriteLine(ToString());
}

}

public class Person : GroupBase
{
public string FullName { get { return base.Name; } }

public Person(string name, Guid id) : base(id, name)
{
}
}

public class Group : GroupBase
{
public delegate void PersonInfo(string name);
public delegate string AdditionalInfo(int n);
// Constructor
public Group(string name, string description) : base
(Guid.NewGuid(), name)
{
this._Description = description;
this._PermittedRoles = new ArrayList();
this._Members = new ArrayList();

}

public event PersonInfo OnNewPerson;

public void AddPerson(Person p)
{
_Members.Add(p);
OnNewPerson(p.FullName);
}

public new string ToString()
{
return string.Format("Description={0}, #of Members {1}",
_Description, _Members.Count);
}

// Write Group Details on the Console
public void WriteToConsole(AdditionalInfo message)
{
base.WriteToConsole();
Console.WriteLine(ToString() + message(5));
foreach (Group GroupMember in _Members)
GroupMember.WriteToConsole();
}

// Type definitions for the Group Structure
public string _Name;
public string _Description;
public ArrayList _PermittedRoles;
public ArrayList _Members;

}

class MainClass
{
static void OnNewPerson(string name)
{
Console.WriteLine("New person added:" + name);
}

public static void Main(string[] args)
{
int m = 7;
Group.AdditionalInfo bubble = delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
};

Group.AdditionalInfo b2;

b2 = bubble;
GroupBase b = new GroupBase(Guid.NewGuid(), "Base Group");
Console.WriteLine("--------------------------------");
Console.WriteLine(b.ToString());
Console.WriteLine("--------------------------------");
b.WriteToConsole();
Console.WriteLine("--------------------------------");

Group g = new Group("The real group", "For cool people");
g.OnNewPerson += OnNewPerson;
Console.WriteLine(g.ToString());
Console.WriteLine("--------------------------------");
g.WriteToConsole(delegate(int n)
{
return " Bubble(" +n +")[" + m + "]";
});
Console.WriteLine("--------------------------------");
object o = g;
Console.WriteLine(o.ToString());
g.OnNewPerson -= OnNewPerson;
}
}
}
Reply all
Reply to author
Forward
0 new messages