Advertisement

C# Workshop - Week 1 (Ch. 1 & 2) - Advanced

Started by July 01, 2007 12:15 AM
337 comments, last by paulecoyote 17 years, 2 months ago
Quote: Original post by JWalsh
popcorn, most of your answers are fine, here's some differences from what I had when writing the review questions:

What's the difference between a virtual and non-virtual method?
When a virtual method is invoked, it's runtime type for the instance is invoked. For a nonvirtual method, the compile-time type is invoked.



What's the difference between compile-time types and runtime types? Moreover, what do they even mean?
Quote: Original post by Menace2Society
Quote: Original post by JWalsh
popcorn, most of your answers are fine, here's some differences from what I had when writing the review questions:

What's the difference between a virtual and non-virtual method?
When a virtual method is invoked, it's runtime type for the instance is invoked. For a nonvirtual method, the compile-time type is invoked.



What's the difference between compile-time types and runtime types? Moreover, what do they even mean?


Does this not answer your question? If so, what more do you want to know, what is not clear, etc. ?
Advertisement
Quote: What's the difference between compile-time types and runtime types? Moreover, what do they even mean?


Here's a quick example...

I have a class called "CharacterClass." Now, CharacterClass is an abstract class, meaning there are no such things as "CharacterClass" objects, however there can be objects of derived types.

Here's the class's declaration:

    public abstract class CharacterClass    {        public abstract void Attack( Living thing);    }


The above illustrates that I now have a CharacterClass Type, however because it is abstract I cannot instantiate a new "CharacterClass" object. Also above we see there is a method of the class called Attack.

Now, lets make this more interested by providing some concrete classes which are based on CharacterClass.

    public class Healer : CharacterClass    {        public override void Attack(Living thing)        {            UseWand(thing );        }        protected void UseWand(Living thing)        {        }    }    public class Nuker : CharacterClass    {        public override void Attack(Living thing)        {            CastSpell(thing);        }        protected void CastSpell(Living thing)        {        }    }    public class Tank : CharacterClass    {        public override void Attack(Living thing)        {            // Bash             ShieldBash(thing);        }        protected void ShieldBash(Living thing)        {        }    }


Now you can see that I've got 3 concrete (not abstract) classes which inherit the Attack method from CharacterClass. Now, you'll notice I put override on each of those within the derived class. I did that because by making the Attack Method abstract in the base class, I implicitly made it virtual as well. In the derived classes I want to override the default implementation (which in this case didnt exist, as it was abstract and not just virtual).

Now I can do something like this in my program...
    static void Main(string[] args)    {        // ... do lots of setup        CharacterClass myClass = GetClassFromPlayer();         // .. do some more stuff        myClass.Attack(someLiving);    }


Ok, so that's an overly simplified example, but here's what's happening. Inside of my main fuction I'm declaring an object called myClass, but I'm saying it's of type CharacterClass. "OMGWTF!! You said I couldn't do that!!" Yeah, you're right, sortof.

In the example above I'm not actually instantiating an object of type CharacterClass, instead I'm an object of whatever type is returned from GetClassFromPlayer. Lets say for a moment that GetClassFromPlayer displays a menu for me. And then asks me which class I'd like to play. Assume for a moment that I select "Healer". What GetClassFromPlayer() will return is an object of type "Healer". However, as you'll learn more of when we get into the chapter on inheritance, since "Healer" inherits all the members of CharacterClass than a Healer IS-A Character Class.

In spite of the fact that I've created an object of type Healer, I'm allowed to assign it to a variable of type CharacterClass. Now, here's the magic. When I call "Attack()" on myClass, which Attack method does it call? CharacterClass's? Nope. It's Attack method was abstract, and so had no implementation. (In hindsight, it might have been better just to make CharacterClass's Attack method empty, rather than abstract so I could prove a point, oh well).

In reality, it will call the Attack() method owned by Healer. Why? Polymorphism. Because the Attack method in CharacterClass was abstract (and thus virtual) the application knows to call the Method associated with the object that was assigned to variable myClass, and not necessarily what type it has in front of it.

Ready for the punchline?

CharacterClass is the Compile-Time class associated with myClass.
Healer is the Run-Time class associated with myClass. Why is it "run-time"? Because it wasn't until run-time that the CLR program was able to determine what type of object would actually be stored in myClass.

Poof! There it is! Compile-Time types are the types as determined at compile-time. Run-time types are the types of objects ACTUALLY stored in a variable at run-time.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Do you recommend pacing yourself when reading the pages this week?

I'm a huge procrastinator - so, knowing me, I'd probably wait until the very last day to read the chapter. This may or may not be an effective means of learning, as I don't really know the content.

Should I just read 10-20 pages now, and continue doing that for the rest of the week?
Yes. 50 pages of dense programming language content is a lot (nearly impossible) to consume and understand in a night.

The best way to read through a book/specification like this is to read 5-15 pages a night. Re-Read it if necessary, come on here and ask questions about the material, and then re-read it again if necessary. Make sure you understand before moving forward.

By reading 10 pages a night you'll easily complete the workshop in sync (if not ahead) of everyone else, while giving yourself plenty of time to absorb the information and ask questions if necessary.

After you've read over 5-15 pages, check the review questions (they're in order) to make sure you understood the material you just read. Then, after you've finished a chapter, go to the exercises at the bottom of the review sheet and see if you can do the exercises. If not, find the sections you're still vague about and re-read or ask clarification questions here.

This workshop is a valuable resource to be used, if you use it, and if you've read the assigned material before trying to come on here and decipher what everyone is talking about. [lol]

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by JWalsh
Quote:
Expressions: An expression is just a statement which results in a value. ex. int myInteger = 2 + 3; that is an expression who's value is 5, and is assigned to MyInteger.


"An expression is just a statement which returns a value"...check
"That is an expression who's value is 5"...check (2+3 is the expression)
..and is assigned to MyInteger...check (the assignment operator '=' makes it an expression statement)

So, summary?


MyInt = 3 + 5 // an expression, value 7
int MyInt = 3 + 5; // no longer an expression. Sticking that int in front of it makes it no longer a valid expression, but a statement containing an expression.
int MyInt = 4 + (int MyInt2 = 4); // illegal, expression expected or invalid expression term 'int'

That was my point.
Advertisement
Quote: Original post by DvDmanDT
I'm pretty new to C# myself, but I do have some experience with C++, and I assume expressions and statements are the same thing in both languages.

NOTE, the following text refers to C/C++, and not to C#, even though parts of it _might_ be true for C# as well.

I know they changed the syntax of the for-loop from three expressions to one statement and two expressions to allow

for(int i = 0; i < 10; i++)

instead of

int i;
for(i = 0; i < 10; i++)


that is, they changed

for(expr ; expr ; expr)

to

for(statment expr ; expr)


[digression]
Actually, both C and C++ allow you to write
for(int i = 0; i < 10; i++)
That is:
for(statment ; expr; statement)
So, nothing was changed in C# regarding for :)
[/digression]
Quote:
MyInt = 3 + 5 // an expression, value 7
int MyInt = 3 + 5; // no longer an expression. Sticking that int in front of it makes it no longer a valid expression, but a statement containing an expression.
int MyInt = 4 + (int MyInt2 = 4); // illegal, expression expected or invalid expression term 'int'

That was my point.


You're still not quite understanding. How's this...

3 + 5 // an expression, value 8MyInt = 3 + 5; // A statement, due to the '=' sign, and also an expression, thus, an Expression Statement// Still a statement, but because of the "int" in front it, // it is now classified as a Declaration Statement and not an Expression// Statement. However it still contains a valid expression.int MyInt = 3 + 5;


Thus, if you were to say:

int MyInt = 3 + 5;

... was an expression who's value was assigned to an integer, you'd be correct. To say that the whole thing is an expression, would be incorrect. In its entirety, that statement is a Declaration.
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by MaxBog
Quote: Original post by DvDmanDT
I'm pretty new to C# myself, but I do have some experience with C++, and I assume expressions and statements are the same thing in both languages.

NOTE, the following text refers to C/C++, and not to C#, even though parts of it _might_ be true for C# as well.

I know they changed the syntax of the for-loop from three expressions to one statement and two expressions to allow

for(int i = 0; i < 10; i++)

instead of

int i;
for(i = 0; i < 10; i++)


that is, they changed

for(expr ; expr ; expr)

to

for(statment expr ; expr)


[digression]
Actually, both C and C++ allow you to write
for(int i = 0; i < 10; i++)
That is:
for(statment ; expr; statement)
So, nothing was changed in C# regarding for :)
[/digression]


Actually, that's not quite correct.

In C# it's for(expression_statement_list or declaration, boolean expression, expression_statement_list)
statement

that is, the first part can either be a variable deceleration, or a list of expression statements separated by commas.

The conditional has to be a boolean expression, and since an expression is automatically a statement, assignment is also allowed.

The final part is an expression statement list, so it's once again a comma separation list of expression statements (which means assignment is still possible)

Cheers!

[Edited by - JWalsh on July 3, 2007 8:33:15 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
I think we need some assignments to test this concepts, even if they have nothing to do with the Final Project. A list of basic system commands would help too.
like console.write and such.

This topic is closed to new replies.

Advertisement