CPSC 481 – Tutorial 4 Intro to Visual Studio and C# Brennan Jones bdgjones@ucalgary.ca (based on previous tutorials by Alice Thudt, Fateme Rajabiyazdi, and David Ledo)
Announcements • I emailed you an example showing how the evolved prototype and walkthrough should be presented in your portfolio. • Please look through this and refer to it while you’re putting together your portfolios. • Your assignment deadline is now Oct. 13 @ 10am.
Visual Studio • Visual Studio is installed on the lab machines • You may download Visual Studio onto your machine for free here • Use your IT/webmail account and password to access MSDN • Install it after downloading
Setting Up SVN Accounts • For those who want an SVN account, go to the CPSC help desk (just outside our tutorial room) and ask for one. • OR email CPSC Help Desk: cpschelp@ucalgary.ca • Request for an SVN account • Include members’ IT account usernames or all usernames you wish to have access to it • State it is for your CPSC 481 group project
SVN and Visual Studio We won’t go over this in tutorial, but here are some resources that can help you: 1. AnkhSVN: http://grouplab.cpsc.ucalgary.ca/cookbook/index .php/VisualStudio/Subversion 2. VisualSVN: https://www.visualsvn.com/ 3. TortoiseSVN: http://tortoisesvn.net/visualstudio.html
Starting a Project
Starting a Project click
Starting a Project click
Starting a Project click
Starting a Project change
Workspace
Workspace coding area
Workspace solution explorer
Workspace run your program
Hello World!
Hello World! click to create a breakpoint
Hello World!
Hello World! call stack and variables
Hello World! debug functions: run, pause, stop, restart
By now, you should know… • Where to download and how to install Visual Studio • How to get SVN accounts • How to create a new project in Visual Studio • Basic features of the workspace • How to make a console application • How to use the debugger
Intro to C# Coding Sources/Materials: • https://msdn.microsoft.com/en-us/library/kx37x362.aspx • https://msdn.microsoft.com/en- us/library/aa288436(v=vs.71).aspx
C# Naming Conventions • Every language has its own naming conventions: • E.g., Java uses camelCasing, while C# uses camelCasing for variables and PascalCasing for methods. int myInteger; public void ReturnResults (){ … } • A lot of people like to call instance variables (or fields) starting with an underscore: int _myInteger • Interfaces always start with an I (e.g., ICloneable )
Basic C# Syntax • Comments work similar to Java: // This is a single-line comment /* This is a multi-line comment */ /// This is how you describe methods /** (This is how you describe methods in Java) */
Basic C# Syntax • Access modifiers public – accessible anywhere private – only accessible within the class or struct protected – only accessible within the same hierarchy internal – only accessible by the same assembly
Properties • A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. • Also called accessor methods. • Properties are public methods. • Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
Properties class TimePeriod { private double seconds; public double hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } • A get property accessor is used to return the property value. • A set accessor is used to assign a new value. • The value keyword is used to define the value being assigned by the set accessor. • Properties that do not implement a set accessor are read only.
class Program { static void Main() { TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours); } } // Output: Time in hours: 24
Foreach Loop using System.Collections; ArrayList list = new ArrayList(); list.Add(1); list.Add(2); foreach (int i in list) { int j = i; } If you use generic containers (e.g., ArrayList), casting is implicit, unlike Java where you have to explicitly cast.
Inheritance A class can inherit a base class and also implement one or more interfaces. public class ChildClass : InterfaceName public class ChildClass : ParentClass public class ChildClass : ParentClass, IInterface1, IInterface2 …
Prototype Iteration Based on your walkthrough and the problems you have identified, create updated designs for your prototype that incorporate your solutions. • This exercise does not have to be integrated into your portfolio • It will help you for your later implementation • Try to address all identified problems
Next Week • Intro to Assignment 2 (phase 2 of your projects) • Intro to WPF (interface building)
Recommend
More recommend