🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# Workshop - Week 2 (Ch. 5 - 6)

Started by
22 comments, last by SpiderPig 16 years, 8 months ago
Yes you must call the base constructor for the base constructor to get used, it is called constructor chaining. All other ones as long as they are protected or public can be called in the inherited class. Private members of the base class are still not accessible by the inherited class.

theTroll
Advertisement
When will the exercises be coming?
Helo JWalsh,

Just wanted to drop a message and thank you for all the hard work.

I currently have only one question...

Are there going to be any best practices type suggestions, such as organization of cs files etc in a project. I'm a C/C++ programmer and so I'm inclined to seperate functional units into seperate cs files. This is still a good practice right ??

J
Computers are useless, they can only give you answers. - Pablo Picasso
[opinion]
My preference is one file per class. With a few exceptions to the rule. If I have an exception class for a class then I normally keep the exception class and the class in a single file. If I have a struct that is only used with a single class then I will put it in the class file.

I also always name the .cs file after the class. That way I can glance at the .cs files and know which ones are which.

I have a habit about reusing classes from project to project, so that helps a lot to have well named single class files.

theTroll
[/opinion]
People are welcome to post "best practices" questions. I only ask that people answering those put the answer in [opinion] tags, so that readers understand that while "best practices" may not be hard, fast rules by nature, they are also highly subjective.

My understanding or rules for "best practices" may not be the same as someone elses. We've all got different educations and backgrounds.

[opinion]
I, however, tend to separate code into 1 class per .cs file whenever possible. It makes it easier to find code when you need it by simply heading to the appropriately named file.

For enums or structs its more flexible. I generally have 1 file for all of my enums, and I tend to store structs in the same file as their primary handlers.
[/opinion]

Also, Microsoft has a C# style Guideline online which covers many style and organizational topics of the language. You can find it Here.
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
While attempting to set my console title I received the following error (using Ubuntu//MonoDevelop):

[Task:File=/home/matt/workshop/workshop/Main.cs, Line=9, Column=2, Type=Error, Description=`System.Console' does not contain a definition for `Title'(CS0117)


Am I missing a certain file?

Here is my code:

using System;//Read up on:// - namespacesclass Program{	static void Main()	{	Console.Title = "TEST";	string welcomemessage = "Welcome to The C# Maze Generator Program";	Console.WriteLine(welcomemessage);	}}
Quote: Original post by Gallivan
While attempting to set my console title I received the following error (using Ubuntu//MonoDevelop):

Am I missing a certain file?



Last I checked, .NET 2.0 was sketchy at best on unixlikes. Console.Title was added with 2.0
I actually switched my Runtime from 1.1 to 2.0 and it compiled without error, however the console title set as:

workshop.exe ;echo; read -p 'Press any key to continue'...

This is all to due with Linux, or did I miss some sort of library download?
Hi, i know this workshop has ended but I was just wondering if anyone could clear up some of my questions regarding Chapter 5.

Quoting from page 27:

But you can‘t have assignment statements outside of methods. This code doesn‘t compile at all:

class Program
{
static string strDisplay;
strDisplay = "Hello, Microsoft .NET Framework";

static void Main()
{
System.Console.WriteLine(strDisplay);
}

}
The compiler error message is "Invalid token '=' in class, struct, or inter-face member declaration," meaning that when the C# compiler was parsing the program, everything appeared OK until it got to the equal sign.

You can use the same name for fields and local variables:

class Program
{
static string strDisplay = "This is a field";

static void Main()
{
string strDisplay = "This is a local variable";
System.Console.WriteLine(strDisplay);
}
}

I do not understand how in the first example assigning a value to strDisplay outside Main is not valid but in the 2nd example it is also assigning a value but it is still valid.

Could someone explain this to me, thanks in advance
Hi SpiderPig. The problem with your code is that you want to make a value assignment outside a method. The second example is treated as initialization of the variable and not as an assignment.

[Edited by - Razvan1024 on October 9, 2007 4:18:30 AM]

This topic is closed to new replies.

Advertisement