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
i don't really have any experience at all, to get that out of the way first.

I took like 10 online lessons in c# before but it seems i learned almost a baby language if that makes sense? (curly ka n kicking ka rather than 'kay' and 'see') i've just finished chapter 1 and damn was there alot to take in there.. not really asking for help or anything at the moment.. just felt like posting something about it.. it's very detailed and i'm sure after one or two more read throughs i should understand a little bit more, thanks for the oppurtunity anyways JWalsh.
Quote: Original post by Ezbez


In summary I would think that having the following concepts explained briefly would help:

-Variables
--Declaration

Declaration is letting the compiler that you are making a variable of this name and type; int a, string b, float c.

Quote:
--Allocation

Allocation is setting aside memory for storing information in that variable. Value type are allocated when you declare them, reference types need you to use the "new" keyword.
Value type, int a, float c, it is both a declaration and allocation.
Reference types;
string a // declaration;
a = new string(); //allocation, sets aside memory for your string 'a'

Quote:
--Assignment

Assignment is when you give a value to the variable.
int a = 12, string b = "xyz". // those are assignments, they are also declaration, and allocations in this case.

string a; // declaration
a = new string(); // allocation
a = "xyz"; // assignment.
Quote:
-Functions

In C# a better term for a function is a method. Unlike in c++ functions can not exist outside of classes. A method of class gives your class the ability to do things, it is the actions the class takes.
Quote:
--Parameters

Parameters are information you are passing into a function. Say you want the area of a square.
public float Area(float length, float width){   return length * width;}


length and width are the parameters.
Quote:
--Returning

Returning is the value you are passing back out of a method.
So up above I am returning length * width.

float length = 3.2f;
float width = 1.7f;

float area = Area(length, width);

Quote:
--Defining a function


A function definition is the the access, return type, name and parameters of the function.

Up above public was the access,
float for the return type,
Area was the name,
and float, float were the parameters. You noticed I did not say length and width as the parameters. The types of the parameter are what defines the parameters not the names.
Quote:
--Calling a function


If you look up you can see how to call the area function
Quote:

Even just a few sentences on each should prove beneficial.


Hope that helps.
theTroll
Advertisement
Quote: Original post by Ezbez

In summary I would think that having the following concepts explained briefly would help:

-Variables
--Declaration
--Allocation
--Assignment
-Functions
--Parameters
--Returning
--Defining a function
--Calling a function

Even just a few sentences on each should prove beneficial.


Imagine a real life box.

Variable - Label for box
Declaration - Describe shape of box
Allocation - Create box
Assignment - Put something in the box

Functions - Do something with the contents of box, then giving a box back
Parameters - Either directions where to find the box, or a box
Returning - Handing you back a box when the function is finished doing stuff to it
Defining a function - Instructions of what to do with the box(es) given
Calling a function - Putting a box in to a machine, expect the function to do something with it. Perhaps X-Ray it or something.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote: Original post by JWalsh


"My hair color is Auburn"

Why does is do this? Because I told it to print myCharacter.HairColor which takes the value of my hair color (ie. HairColor.Auburn) and converts it to a string. With enums, converting the value to a string always creates a string which has the value equal to the name of the value in the enum. In this case, "Auburn".


I don't quite get this part, you didn't specify anywhere in the code that you wanted to convert the enum value into a string. Isn't it supposed to return an integer since that's the underlying type of the enum? How can you tell when an enum will be converted into a string and when it will return the value of its underlying type?
Quote: Original post by Menace2Society
Quote: Original post by JWalsh


"My hair color is Auburn"

Why does is do this? Because I told it to print myCharacter.HairColor which takes the value of my hair color (ie. HairColor.Auburn) and converts it to a string. With enums, converting the value to a string always creates a string which has the value equal to the name of the value in the enum. In this case, "Auburn".


I don't quite get this part, you didn't specify anywhere in the code that you wanted to convert the enum value into a string. Isn't it supposed to return an integer since that's the underlying type of the enum? How can you tell when an enum will be converted into a string and when it will return the value of its underlying type?


It is an enum... not its underlying type. So the ToString on an enum behaves differently then a ToString on an int.

If you are working with an enum, most of the time you would want to use the name rather then the number on it. You can cast it to an int at any time.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote: Original post by Menace2Society
Quote: Original post by JWalsh


"My hair color is Auburn"

Why does is do this? Because I told it to print myCharacter.HairColor which takes the value of my hair color (ie. HairColor.Auburn) and converts it to a string. With enums, converting the value to a string always creates a string which has the value equal to the name of the value in the enum. In this case, "Auburn".


I don't quite get this part, you didn't specify anywhere in the code that you wanted to convert the enum value into a string. Isn't it supposed to return an integer since that's the underlying type of the enum? How can you tell when an enum will be converted into a string and when it will return the value of its underlying type?


Actually an enum does not have the underlying type of int, it is an enum structure. Part of the enum struct is the ToString() method.

So to convert an enum to a string.
enum Day{     Sunday,     Monday,     Tuesday,     Wednesday,     Thursday,     Friday,     Saterday}Day day = Day.Sunday;string day_string = day.ToString();  // the string would be sundayint day_number = (int)day; // the int would be 0


Hope that explains it.
theTroll





Advertisement
I think this my help other http://msdn.microsoft.com/vstudio/express/beginner/windows/tier1/csharp/

It has been hard to keep up with the workshop without an automatic email notification for new posts. Is there a way to configure this?
I have created a new thread for week 1 specifically designed for beginners. People with little or no previous programming experience, or people who feel their question is of a more beginner nature should post their questions in this thread.

Those with a good amount of previous programming experience, or who need clarification on functionality differences between C# and C++ should post their questions in the week 1 thread marked as advanced.

These threads for week 1 will remain, and future weeks may or may not be split up in such a fashion. The GDNet staff and moderators are looking at a more long-term solution to make posting questions for Workshops easier. I'll keep you updated.

Finally, due to the general feeling that the beginners are having a hard time grasping the basic concepts, we will be extending week 1 by another week. That is, week 1 will actually be active for 2 weeks.

The beginner thread is located Here.
While the advanced thread is located Here.

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 TheTroll
Actually an enum does not have the underlying type of int, it is an enum structure. Part of the enum struct is the ToString() method.

So to convert an enum to a string.
*** Source Snippet Removed ***

Hope that explains it.
theTroll


Actually this conflicts with what I read in the spec which states:


Each enum type has a corresponding integral type called the underlying type of the enum type. An enum type that does not explicitly declare an underlying type has an underlying type of int. An enum type’s storage format and range of possible values are determined by its underlying type. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.


How about them apples?
Quote: Original post by popcorn
Quote: Original post by TheTroll
Actually an enum does not have the underlying type of int, it is an enum structure. Part of the enum struct is the ToString() method.

So to convert an enum to a string.
*** Source Snippet Removed ***

Hope that explains it.
theTroll


Actually this conflicts with what I read in the spec which states:


Each enum type has a corresponding integral type called the underlying type of the enum type. An enum type that does not explicitly declare an underlying type has an underlying type of int. An enum type’s storage format and range of possible values are determined by its underlying type. The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

To be specific, each enumeration type is a type of its own, however the underlying storage type (used to hold the values) default to integer. You can actually override the default storage type as such:
public enum Days : byte {    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement