Team 302 Software Training
C# Programming
Activity 0
C# Programming
Text Case and Spaces
Hint: This will be one of the greatest sources of compile errors
Blank Lines
Comments
Hints:
Activity 1
Activity 2
Executable lines
Hints (common sources of compile errors):
Executable Lines
Executable lines can span multiple physical lines. For instance, these are the equivalent:
Abc = a + b +c;
Abc =
a +
b +
c;
Activity 3
public override void DriveWithJoysticks()
{
⇒ Look here
}
Hint: What are some of the common things that cause compile errors?
Variables
Defines a type and a name
VariableType variableName = initialValue;
Common Variable Types
enum - enumerated list
enum MECHANISM_TYPE
{
UNKNOWN_MECHANISM = -1,
GRABBER,
LIFT,
CLIMBER,
SIDE_HANGER,
ACTIVE_GRABBER,
MAX_MECHANISM_TYPES
};
Variables - Examples
int loopCount = 1;
float motorSpeed = 1.0;
bool isButtonPressed = false;
bool isGamePiecePresent = false;
float proportionalConstant;
proportionalConstant = 0.2;
Math Expressions
“Sequence of operators and their operands for a computation”
Assignment:
a = b;
a += b;
a -= b;
a *= b;
a /= b;
Increment / Decrement:
++a;
a++;
--a;
a--;
Arithmetic:
a = b + c;
a = b - c;
a = b * c;
a = b / c;
a = b % c;
Hints:
Activity 4
public static double GetInRange
(
double value, /// <I> - value to check
double minValue, /// <I> - minimum value for the range
double maxValue /// <I> - maximum value for the range
)
{
⇒ line goes here (after block comment)
}
Variable Scope
{ // 1
{ // 2
{ // 3
}
}
}
Constants and Literals
Things we use (typically initial values or right hand side of assignments):
Activity 5a
public void RunMethod()
{
If ( buttonPressed )
{
double value = 1.0;
}
value = 0.5; // this value is not declared and will have a compile error
}
How can this be fixed?
Activity 5b
public void RunMothod()
{
double value = 0.0;
if (buttonPressed)
{
double value = 1.0; // new variable, value not reset
}
else
{
value = 0.5; // changes the value from 0.0 to 0.5
}
}
How can this be fixed?
Conditionals
Hint: Common mistake is to use = instead of ==
Conditionals - if, if/else, if/else if/else
Hint: Try to avoid complex compound comparisons, it is easy to make a mistake.
Careful with 2 ifs next to each other without the else both get evaluated
Conditionals - if
Examples:
if ( isButtonPressed )
{
}
if ( !isButtonPressed )
{
}
if (isButtonAPressed && isButtonBPressed )
{
}
Conditionals - if / else if / else
if ( loopCount < 5 )
{
speed = 0.5;
}
else if ( loopCount < 10 )
{
speed = 0.75;
}
else
{
speed = 0.0;
}
if ( loopCount < 5 )
{
speed = 0.5;
}
if ( loopCount < 10 )
{
speed = 0.75;
}
else
{
speed = 0.0;
}
What is the value of speed if loopCount is 3?
Activity 6
Conditionals - switch /case
Hint: Always have a default case.
Conditionals - switch / case
int abc;
int value = 0;
switch ( abc )
{
case 1:
value = 5;
break;
case 2:
value = 10;
break;
default:
value = -1;
break;
}
int abc;
int value = 0;
switch ( intValue )
{
case 1:
value = 5;
case 2:
value = 10;
break;
default:
value = -1;
break;
}
If abc is 2, the value is set to 10. If the abc is 10, the value is -1. Notice the right column case 1 is missing a break. What is value if abc is 1 for both sides? Is it the same or different?
Classes and Objects
Classes and Objects - Example
Class: Dog
(sub)Class: GoldenRetriever
(sub)Class: Dalmatian
Objects of Class GoldenRetriever: B-Dawg, Buddha, Budderball, Mudbud, Rosebud, etc.
Objects of Class Dalmatian: Pongo, Perdita, Lucky, Patch, etc.
Classes and Objects - Example
Class: TalonSRX
Objects: LeftFrontDrive, LeftRearDrive, RightFrontDrive, RightRearDrive
Class: Automobile
(sub)Class: ChevyCorvette
Objects: JimsCorvette, AnnesCorvette, JaysCorvetter
Activity 7
Which is a class and which is an object
SuperHero
IronMan
CaptainAmerica
MarvelSuperHero
SpiderMan
WonderWoman
DCSuperHero
Batman
Gamora
Bob
Minion
Stuart
Kevin
Dave
Jerry
Mark
Phil
Class/Object Variables
Activity 8
Classes
Class Attributes
Class Methods
Class Layout
Provide access to other namespaces
Defines this namespace
Defines class and baseclass (:IDragonGamePad)
using Microsoft.SPOT;
using HeroDemoBots.Common.Controllers.Axis;
using HeroDemoBots.Common.Controllers.Button;
using CTRE.Phoenix;
using CTRE.Phoenix.Controller;
namespace HeroDemoBots.Common.Controllers
{
public class DragonGamePad : IDragonGamePad
{
Class Layout
Attributes of the class
Constructor
Method - override indicates it replaces a method in a base class
public class DragonGamePad : IDragonGamePad
{
private GameController m_gamepad;
private IButton[] m_buttonAr;
private AnalogAxis[] m_axisAr;
public DragonGamePad()
{
m_gamepad = new GameController(UsbHostDevice.GetInstance());
}
public override bool IsButtonPressed
(
BUTTON_IDENTIFIER button /// <I> - button to check
)
{
Reading a Method Signatures
Visibility ReturnType MethodName( ParameterType ParameterName );
ClassName objectName = new ClassName();
ParameterType ParameterName = initialValue;
ReturnType returnVariable = objectName.MethodName( ParameterName );
Activity 9
Activity 10
Activity 11