csharp - workbook question edition

Upload: loredana-roba

Post on 08-Apr-2018

256 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 CSharp - Workbook Question Edition

    1/168

    Document Version 1.0

    Copyright 2004 LearnVisualStudio.NET

  • 8/6/2019 CSharp - Workbook Question Edition

    2/168

    !"#$%&'

    (

    )*+, -

    -./

    '0#123"3"0)

    4.

    56)/

    /7.)5

    !"8!'

    6274

    !"8 5

    &+#6 //

    )9:1 /4

    -!"0#;6)

    '!"00"

    40"0(

    52(000" 5

    /*+;

    0": )

  • 8/6/2019 CSharp - Workbook Question Edition

    3/168

    !

    "## $ %&'() * #(#+

    ' ,% '!*-.

    )

    !

    #"/%0(

  • 8/6/2019 CSharp - Workbook Question Edition

    4/168

    ! " ! " !

    ! " # $ !

    !# $%

  • 8/6/2019 CSharp - Workbook Question Edition

    5/168

    &'(

    # )

    *+, -" .#

    (/

    012 34,

    %

    /

    (5

    ,. 1(67879"1:"166"

    ; !,;,

  • 8/6/2019 CSharp - Workbook Question Edition

    6/168

    (0

    1 4 ( ! , * +

    !=>&:((23

    ( >? , 6,="@2.3=>?.?,$,( >? #>?:(A

    .

    ?#!$?!$(,,!,:(A :( A , ?#!$

    %&'

    (:(A,, !

    .BC. ? . :( . D999 . EF * - F+

  • 8/6/2019 CSharp - Workbook Question Edition

    7/168

    ((((

    . A (0@

    )

    0. ( @ GG > &:( = 6 ?E*+5!"

    .H

    #@

    ! ; , ==#GG;;4>0

    ! 2:( $3 :( 6 $ :( A .""

    )

    :(6$

    .F!

    ?.

    &!*1:"166"+

  • 8/6/2019 CSharp - Workbook Question Edition

    8/168

    ****

    (...('! >&:(

    $ > & I :( ! > & 203

    (@

    !:1(;4>&GG=(, ! , * +

    !!>&:((!"23 > &:( ;,:(6

    0 -0 *,+

    > #:( ?/ > &:( ' * 0,+

    %&+,

    - ( 0"(@

    4! , '!0 ( ,(

  • 8/6/2019 CSharp - Workbook Question Edition

    9/168

    ----

    / ' ? @ 2#

  • 8/6/2019 CSharp - Workbook Question Edition

    10/168

    ....

    2 3 #

    !"0 ; !",!!2"3# 0 / -;023; (4H

    /

    -"@

    " !" ; 0 F A"0- *!" , + ,!!"!!"',,!!F

    #!>&K=8BBK!"0>&68BBL!0!"'5!"

    - H ( - $>#:( >#:(

  • 8/6/2019 CSharp - Workbook Question Edition

    11/168

    $% !01@""/ !"

    ! ! "&(.

    +%

    !#!

    >?

    :(A

    :(6$

    #$

  • 8/6/2019 CSharp - Workbook Question Edition

    12/168

    0

    '!#

    8,

    D $

    K(0.'0H

  • 8/6/2019 CSharp - Workbook Question Edition

    13/168

    ! " # ! " $!!%! " ! !

    " !

    !&!'!()

    !!

    *& +"'!,-)+ .!

  • 8/6/2019 CSharp - Workbook Question Edition

    14/168

    !"#

    "!/

    !

    $%#

    !!/

    "#$$

    0!!&!'!()"/

    #"%"!

    &

    !!!-!!!!"/

    -"

    -

  • 8/6/2019 CSharp - Workbook Question Edition

    15/168

    ''''

    (

    -!")+

    '!

    ,

    )##$#1!!! 23!4

    /5555!6!7558!5"955":'!

  • 8/6/2019 CSharp - Workbook Question Edition

    16/168

    ****

    +

    % $! " )+

    ;&!'!()

  • 8/6/2019 CSharp - Workbook Question Edition

    17/168

    ! "#$ %& ' ('(!")'(*

    *++""++",-./01230245346563/4730002/7879.

    &!

    $'("#$%&: "#$ ; "

  • 8/6/2019 CSharp - Workbook Question Edition

    18/168

    """"

    ,@A?B&'"#$")GH26>I7"J6"KE;:*

    *++""++++"

    E&"$&"

    >'"#$*

    = " ? @ 3"!; 7/11"D>L"#$"

    = '&">L"#$MMN"#$"""

    = ? O'P:'1666'#'O!

    1666 ' ' ' 1666 #, ' 1666"? '' # $ !

    = ? #>#LB&

  • 8/6/2019 CSharp - Workbook Question Edition

    19/168

    $$$$

    -")"!" )'&*

    'O"#$O>L"#$,: # K" ='#

    #$,:#'"#$O#I8J!"'K""

    ? ? >'"#$O#IJ16"?K""

    !%&'(

    ?Q'O"#$O@,*

    *++""+,+",-,.49.42

    ?%;## C ? >'"#$%'O"#$"?"

  • 8/6/2019 CSharp - Workbook Question Edition

    20/168

    ))))

    (*

    E,"$&" $&A!

    %'O&*

    *++""+++

    %O##&*

    *++""+++"

  • 8/6/2019 CSharp - Workbook Question Edition

    21/168

    ! "#$%&

    % &

    ' ! $

    #$

    %( )*' +, -"

  • 8/6/2019 CSharp - Workbook Question Edition

    22/168

    !

    "#$%!

    &().-

    !"

    !$&'

    -"#"#$%##

    #

    +

    ""

  • 8/6/2019 CSharp - Workbook Question Edition

    23/168

    +"

    *"

    #

    *"

    (

    +#$

    /

    0

    1

    2

    3

  • 8/6/2019 CSharp - Workbook Question Edition

    24/168

    4

    5

    )

    '""-"

    $

    '

    *%

    6 7 -"

    /%&'8%8

    0%8

    1%98

    2%:;8

  • 8/6/2019 CSharp - Workbook Question Edition

    25/168

  • 8/6/2019 CSharp - Workbook Question Edition

    26/168

    !" # $ $

    % ' #

    ( ")* !"% +, !

  • 8/6/2019 CSharp - Workbook Question Edition

    27/168

    !"#$%

    #()!

    !"

    &!

    ' -

    .

    ////01//2///!3

  • 8/6/2019 CSharp - Workbook Question Edition

    28/168

    ''''

    ()

    %!"

    !

    !3

    *#

    4 !"

    5+$

    6 3 #0

  • 8/6/2019 CSharp - Workbook Question Edition

    29/168

    ++++

    7+!3

    &$

    8

    #"

    * +9:!;

    -. #

  • 8/6/2019 CSharp - Workbook Question Edition

    30/168

    !!

    " # $

    %&'(&

    %&'(#

    !

    ") %( * +

  • 8/6/2019 CSharp - Workbook Question Edition

    31/168

    !

    ,-.//(//0#

    1%

    "#$%&

    #2# ## # # #,#!

    ### 34 %##

    5'

    5)516% 7 ,8

    9:;#'

    -.

  • 8/6/2019 CSharp - Workbook Question Edition

    32/168

  • 8/6/2019 CSharp - Workbook Question Edition

    33/168

    %&

    >&

    *

    ? @ #

    ,

  • 8/6/2019 CSharp - Workbook Question Edition

    34/168

    ++++

    ,'

    5'

    "#$%&'#$%&'()*+$/0

    (2DE+F

    /( :3 #!3:5&:&23:5&B

    #G

  • 8/6/2019 CSharp - Workbook Question Edition

    35/168

    !

    "

    # $"%

    ! " #

    &' () *+ , *+

  • 8/6/2019 CSharp - Workbook Question Edition

    36/168

    $$$$

    -./01023024./0102301

    /.3

    "5 "

    #6 7

    /.308

    "9:;%

  • 8/6/2019 CSharp - Workbook Question Edition

    37/168

    ;7

    ( ; C # ; 7 )D!;'=-*"("H " H:7;

    (C":)D

    ! 0; & !5+$+;%

    "#$%&"

    -"; & I;-7 ; ( 7

    !J9&J-H;"!

    H;;%

    #%"

    ")D;%

    -;'=-*"*;;K")D4E "F!74G:GK

  • 8/6/2019 CSharp - Workbook Question Edition

    38/168

    %%%%

    &'

    "%

    '()*+,()*+,-./0!

    ()*+

    "%

    '()*+,((1./(

    5'(-*"

    %

    '()*+,(1

    ,#

    ";=@

    /

    .

    3

    -(+"; E";G

    F

  • 8/6/2019 CSharp - Workbook Question Edition

    39/168

    ....

    %BBBB4BBBL0

    %BBBB4BBBL8

    %BBBBCMBB4BBBL

    E!1121+3)44(4 4 " + )D#!/)D'=-*"F

    /

    +

    +*+

    '

    "

    'E'F

    )

  • 8/6/2019 CSharp - Workbook Question Edition

    40/168

    01010101

    2

    # H *+

    /&'"C

    .&C

    3&5+6N

    -

    N+%

    '()*+,()*+,-./0

    )7

  • 8/6/2019 CSharp - Workbook Question Edition

    41/168

    !

    " "! "

    "

    !#" $%

  • 8/6/2019 CSharp - Workbook Question Edition

    42/168

    &!'()'(*+

    ,)-.$/+

    0111121113456

    !)-.$/+

    0111121113756

    /)-.$/+

    0111121113856

    9(,#$:;(!(2

    )$7+((0

    '

  • 8/6/2019 CSharp - Workbook Question Edition

    43/168

    !#)+ 0

    "/033333333333333333333333333333333333333333333333

    33333333333333333333333333333333333333333333333333333333333333333

    !/033333333333333333333333333333333333333333333333

    33333333333333333333333333333333333333333333333333333333333333333

    //0333333333333333333333333333333333333333333333333

    33333333333333333333333333333333333333333333333333333333333333333

    /03333333333333333333333333333333333333333333333333

    33333333333333333333333333333333333333333333333333333333333333333

    ?/0333333333333333333333333333333333333333333333333333

    33333333333333333333333333333333333333333333333333333333333333333

    '/03333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333

    "/)$72"',,"+(/?'!/ )! "/ +

    ! (

  • 8/6/2019 CSharp - Workbook Question Edition

    44/168

    "0

    @!

    01111561121711!

    '!*

    !"#

    'A(0

    $%&'!"#

    * !@!((0

    !"#

    (0

    ()$ &))*+ ,+ &),)*+ ,)*+ ) ) '-,*.&),)'/**)*0

    * !*!((0

    '(

    :B'/ CDE )D+ )F+

  • 8/6/2019 CSharp - Workbook Question Edition

    45/168

    /!(

    =!!0

    x / 3

    System.DateTime.Parse("12/7/1969")

    "Bob" + " " + "Tabor"

    )!+!/0

    myVariable = x / 3;

    myVariable = System.DateTime.Parse("12/7/1969");

    myVariable = "Bob" + " " + "Tabor";

    "(=G')(+!)+

    G ! / G !!'! ! ! !'@

    Bob == Tabor)#@+

    3 > 1)#'+

    myVariable.Length > 4)",+

    G /()&1+0

    if (myVariable.Length > 4)

    {// Write some code here

    }

    :!,$HA22I&1(//

    !$CJ

  • 8/6/2019 CSharp - Workbook Question Edition

    46/168

    !!!!

    G('0

    Bob

    42

    3.12

    12/7/1969

    $',)*)*(

    #0

    0111121113K3A3A56

    "#$

    ;-.@!( &10

    string if;

    ''8J(0

    0111121113K3A3E56

  • 8/6/2019 CSharp - Workbook Question Edition

    47/168

    %%%%

    B!#-./0

    011112111373K56

    B53333333333333333333333333333333333333333333333

    B53333333333333333333333333333333333333333333333

    ' -. / C+ K+!0

    011112111373K3C56

    *!0

    GB033333333333333333333333333333333333333333333333333

    9B03333333333333333333333333333333333333333333333

    #''B033333333333333333333333333333333333

    LB033333333333333333333333333333333333333333333333333

    GB0333333333333333333333333333333333333333333333333

    -G:"0333333333333333333333333333333333333333333333333333

    -B#033333333333333333333333333333333333333333333333333333

    ! -. )+ !'*M

    33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333

    333333333333333333333333333333333333333333333333333333333333333333

  • 8/6/2019 CSharp - Workbook Question Edition

    48/168

    &'&

    '!N#0

    011112

    1113K3A3456

    $(-./0

    ->OP333333333333333333333333333333333333333333333333333

    /L>(QR333333333333333333333333333333333333333333333333333

    N)+333333333333333333333333333333333333333333333333333333

    "3333333333333333333333333333333333333333333333333333333333333

    -33333333333333333333333333333333333333333333333333333333333

    -0333333333333333333333333333333333333333333333333333333333333

    B&,)')+&(,>:'L0

    myVariable = "Bob" & " " & "Tabor"

    L3):0',>:'+

    myVariable = "Bob" &_

    " " & "Tabor"

    -. '(0

    myVariable = "Bob" +

    " " + "Tabor";

    B @, /:' (L(0

  • 8/6/2019 CSharp - Workbook Question Edition

    49/168

  • 8/6/2019 CSharp - Workbook Question Edition

    50/168

    !

    !"## $%&

    ' ()

    * + + , -./ + ,-./ 01

    2&(

    3 2+0 42* &5 4

  • 8/6/2019 CSharp - Workbook Question Edition

    51/168

    6)(-++.01+7%801

    -*+.-'.(

    ++!"##

    if (taxableAmount > 100)

    {taxableAmount = taxableAmount * .95;

    }

    9:!"##-.;"##

    (!"## "#: 9:

    if (taxableAmount > 100){

    taxableAmount = taxableAmount * .9;}else

    {taxableAmount = taxableAmount * .95;

    }

    (

  • 8/6/2019 CSharp - Workbook Question Edition

    52/168

    >)

    if (taxableAmount > 100){

    taxableAmount = taxableAmount * .9;

    }

    if (taxableAmount < 10){taxableAmount = 10;

    }else

    {taxableAmount = taxableAmount * .95;

    }

    "#="#"#"##,9:/

    (

    &%()

    switch (country)

    {case "Canada":

    taxRate = .085;

    break;case "USA":

    taxRate = .075;

    break;case "UK":

    taxRate = .09;break;

    default:taxRate = .05;break;

    }

  • 8/6/2019 CSharp - Workbook Question Edition

    53/168

    )0+?6-4.7@

    ()

    if (country == "Canada"){

    taxRate = .085;

    }if (country == "USA"){

    taxRate = .075;}

    if (country == "UK"){

    taxRate = .09;

    }else

    {taxRate = .05;

    }

    ,/,/

    (A

    6)-+.01+B8-.-.(

    ( ,/(342C&"9D7+

  • 8/6/2019 CSharp - Workbook Question Edition

    54/168

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

    {

    MessageBox.Show("List item: " + i.ToString());

    }

    &7(((EEF#(,G/ "#DHH +"#+ &4++(B6"#

    > IJ7D)

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

    MessageBox.Show("List item: " + i.ToString());

    (7)

    int i = 0;while (i < 11)

    {MessageBox.Show("Current Item: " + i.ToString())

    i += 2}MessageBox.Show("Last Item: " + i.ToString())

    (DD,

    "#/,-D.-

  • 8/6/2019 CSharp - Workbook Question Edition

    55/168

    ( "#$% D &'% 7 0G64(

    !"

    8 7 $ 0 -DL6. 7M

    for (int i = 1; i < 10; i++){

    if (i = 4){

    MessageBox.Show("Found FOUR: " + i.ToString());}

    }

    7&,/7+ &&0DGG)

    For (int i = 0; i < lstList.Items.Count; i++)

    {if (lstList.GetSelected[i] == true)

    {

    // More code goes here }

    }

    F

    07 +++

  • 8/6/2019 CSharp - Workbook Question Edition

    56/168

    ####

    $"%!

    ()

    !"#$

    &'()!

    ()

    %

    D3+64()

    %!$

    *!!&!

    ( ,(

    L/

    )@@@@G@@@NONP>=

    )@@@@G@@@NONO>=

  • 8/6/2019 CSharp - Workbook Question Edition

    57/168

    ++++

    ,(

    $ 42

    ">D@>

    K>D@>

    QD42+ *

  • 8/6/2019 CSharp - Workbook Question Edition

    58/168

    ----

    *

    C)

    '#$

    0 '4R6KN#S4T & @)

    D@" '@K 'F@Q 5@M

    L,"M/DD'F M ," H Q/ ' '%(B ' 2U

  • 8/6/2019 CSharp - Workbook Question Edition

    59/168

    !

    "####!$!%!!#!

    & !

    & & &!

    ' ()* +(, - +

  • 8/6/2019 CSharp - Workbook Question Edition

    60/168

  • 8/6/2019 CSharp - Workbook Question Edition

    61/168

    22%9:%2!!42264!!!#!2

    28!!%$%/!2!6%%2!%!!!

    #$%&

    $3

    # ;# '!# 26# 49:# 59*):

    $9: $

    !3

    private void Form1_Load(object sender, System.EventArgs e)

    23

    # $3# 2!3

  • 8/6/2019 CSharp - Workbook Question Edition

    62/168

    / %!!/!

    / / =*>/

    int performCalculation(int x, int y){

    }

    4%/!%2/!$! ! ! !.'!!

    ; %

    .2!!2/!

    $2/!2/!

    5$!/!

    !%$!*!/

    int performCalculation(int x, int y){

    int z;

    z = x * y + x;return z;

    }

    4%!/!%$!/ $/!/0//$%

  • 8/6/2019 CSharp - Workbook Question Edition

    63/168

    9:

    4!!%!%/%%

    '!"

    ?/

    private void Form1_Load(object sender, System.EventArgs e){

    int myValue = 0;myValue = performCalculation(6, 5);

    MessageBox.Show(myValue.ToString());

    }

    4% /!% /%9:$/

    $!

    $$ 9@: /

    *AB;3

    z = x * y + x;

  • 8/6/2019 CSharp - Workbook Question Edition

    64/168

    5)

    %3

    myValue = performCalculation();

    2/9/%!:

    *%3

    myValue = performCalculation("Bob" ,6);

    2$%!) *D *D

  • 8/6/2019 CSharp - Workbook Question Edition

    65/168

    *!%'.

    performCalculation(6, 5);

    2 *!!!!!

    2!?

    private void Form1_Load(object sender, System.EventArgs e)

    {int myValue = 0;

    performCalculation(6, 5);

    }

    void performCalculation(int x, int y)

    {int z;z = x * y + x;

    MessageBox.Show(myValue.ToString());}

    $!/*./!5!2#

    ;!!!!"%9:!# ! ! ! =!>%#

    (!)

    23

  • 8/6/2019 CSharp - Workbook Question Edition

    66/168

    public class Form1 : System.Windows.Forms.Form{

    // your methods go here.

    }

    *!%!

    !"

    !./03

    public class Form1 : System.Windows.Forms.Form

    {

    private void Form1_Load(object sender, System.EventArgs e)

    {int x = 6;int y = 5;

    performCalculation();

    MessageBox.Show(z.ToString());

    }

    void performCalculation()

    {int z;

    z = x * y + x;}

    9:%*.

    ?*9:$ ! *

    .!/! !! !% ! $ ! ! !!!2!*%3

  • 8/6/2019 CSharp - Workbook Question Edition

    67/168

    ****

    public class Form1 : System.Windows.Forms.Form{

    int x = 6;

    int y = 5;

    int z;

    private void Form1_Load(object sender, System.EventArgs e)

    {performCalculation();

    MessageBox.Show(z.ToString());}

    void performCalculation(){

    z = x * y + x;

    }

    $%!!=!>!=7!>!

    ! ! !.!!!%!9:!!9!: 2%%!!!

  • 8/6/2019 CSharp - Workbook Question Edition

    68/168

    ++++

    (,!

    $!3

    !"#$%&!"#$%&'()

    -)!./'

    $!3

    !"#$%&!!'!

    ');+$3

    !"#$%&!*

    /$$%&

    !% #! 3 89:

    1

    &%!% "1!!

    !*

  • 8/6/2019 CSharp - Workbook Question Edition

    69/168

  • 8/6/2019 CSharp - Workbook Question Edition

    70/168

    ****

    ('

    )

    7!

  • 8/6/2019 CSharp - Workbook Question Edition

    71/168

    ****

    3/!

    1 +(

    .

    G.

    C?!.

  • 8/6/2019 CSharp - Workbook Question Edition

    72/168

    ****

    1!

    "3

    !"#$%&!"#$%&'("

    *05+72;GH+I

    $9J:!3

    # *!(!!!

    # *!!# *$

    $KGH

    # ,$!

  • 8/6/2019 CSharp - Workbook Question Edition

    73/168

    !

    !" #$ % & &

    #' &

    ()%!

    *!(' )+

  • 8/6/2019 CSharp - Workbook Question Edition

    74/168

    , #

    !,-.

    "#

    /0 # 1 &23 #!

    0&4!$2.53&*

    %##&&%1

    - ! # %#%

    #%!##%!!&*1&6!67# &

    1&&/& & 8 % #

    &

    %!!6& ! $ & &

  • 8/6/2019 CSharp - Workbook Question Edition

    75/168

    $$$$

    237918:%!!%

    :;#4#&:?#-%,)#"##1

    @&&&& &0%#9A

    class Car

    {

    }

    %91

  • 8/6/2019 CSharp - Workbook Question Edition

    76/168

  • 8/6/2019 CSharp - Workbook Question Edition

    77/168

    Car myCar;

    myCar = new Car();

    string location;

    myCar.Make = "Oldsmobile";

    myLocation = myCar.Drive();

    %!4 - - &C&&D& ! D' E

    %B--&-#

    --)-#1%-23D2-3#-#

    %0 % &--)BF

    &-.&-4!&4B!

    %0#

  • 8/6/2019 CSharp - Workbook Question Edition

    78/168

    &&&&

    '(

    ,!%& %&"

    AGGGGGGGHB=IJ

    AGGGG

    GGGHBHCIJ

  • 8/6/2019 CSharp - Workbook Question Edition

    79/168

    ))))

    *+

    '!!*!(

    -

    #

    #

    *

  • 8/6/2019 CSharp - Workbook Question Edition

    80/168

    &,&,&,&,

    -#

    $ *!(

    B-I

    5*!-#

    KI

  • 8/6/2019 CSharp - Workbook Question Edition

    81/168

    !"#

    $% & '# !( '#

  • 8/6/2019 CSharp - Workbook Question Edition

    82/168

    !)

    public class Car

    {

    private string _make;

    private string _model;

    public string Make{

    get { return _make; }set { _make = value; }

    }

    public string Drive()

    {if (_make=="Oldsmobile")

    { return "Chicago";

    }

    else{

    return "Toledo";

    }}

    }

    *+**#

    ,"*#**

    * * * $-**'&"."')

    **

    / # '#)0&10

  • 8/6/2019 CSharp - Workbook Question Edition

    83/168

    /*

    22&1

    22'&1'

    !*3*)

    +"3456*,

    public class Car

    {

    ...

    public int ElapsedMileage;

    ...

    }

    +"**3*3,

    Car myCar = new Car();

    if (myCar.ElapsedMileage == 0){

    myCar.ElapsedMileage = 10000000;}

    "+,#*

    &**7 % 89 $*$+::,*

    !'*)

  • 8/6/2019 CSharp - Workbook Question Edition

    84/168

  • 8/6/2019 CSharp - Workbook Question Edition

    85/168

    # = - * "

    &)

    Car myCar = new Car();if (myCar.ElapsedMileage == 0)

    {myCar.ElapsedMileage = 10000000;

    }

    7#*7$=''.*

    +,'289999999+,

    )

    MessageBox.Show(myCar.ElapsedMileage.ToString());

    5 * > 8-*"**89

    ?**)

    _elapsedValue = value;

    $@A>B+@A,*6*6*6+@&%A#,

    "-*$6***6!6*#)

  • 8/6/2019 CSharp - Workbook Question Edition

    86/168

    public string Drive(){

    if (make=="Oldsmobile"){

    return "Chicago";}else

    {

    return "Toledo";}

    }

    **+.**63*,-*:**#:7**3# * " :':?*3?&*" 80!5>$)

    C+,6*

    *

  • 8/6/2019 CSharp - Workbook Question Edition

    87/168

    !!!!

    "#

    )

    public string Drive()

    {

    if (make=="Oldsmobile"){

    return "Chicago";}

    else{

    return "Toledo";}

    }

    public string Drive(string _make){

    if (_make=="Oldsmobile"){

    return "Chicago";}

    else

    {return "Toledo";

    }

    }

    "$-$0'**

    *:&: 3D$>$-*&-*. # #&+, 3 * *#**

  • 8/6/2019 CSharp - Workbook Question Edition

    88/168

    $%&

    "*)

    !"#$%&'

    '"()%*

    "*)

    (!

    %&;'")

    ()%&'

    +'#

    /#"**** +"*5**,

    )EEE*E4EEE080F0G>2

    )EEE*E4EEE080F0H>2

    )EEE*E4EEE080F0I>2

    )EEE*E4EEE0F0H0G>2

  • 8/6/2019 CSharp - Workbook Question Edition

    89/168

    ,,,,

    -.

    *##'#

    ?

    &

  • 8/6/2019 CSharp - Workbook Question Edition

    90/168

    ,/,/,/,/

    0)

    . D '#

    8$**>$>

    G$>

    I$**>

  • 8/6/2019 CSharp - Workbook Question Edition

    91/168

    ,,,,

    +%

    7#)

    !"*%&'

    3!8G'J

    .*)

    ; !; " ;

    +'*,

    "*#

    #*#**.**#*#&*3

  • 8/6/2019 CSharp - Workbook Question Edition

    92/168

  • 8/6/2019 CSharp - Workbook Question Edition

    93/168

    !"#!

    0"!1!!!!$! 0&"

    2

    public class Car

    {

    public Car()

    {

    make = "Unknown";

    model = "Unknown";

    elapsedMileage = 0;

    }

    . . .

    }

    3" * ! 34 &"!05,2

    myCar = new Car();

    6 6,67. & ! !"81"""!9":!

    4 ;1

  • 8/6/2019 CSharp - Workbook Question Edition

    94/168

    %.* ?+6*%2 ' 1 !

    $"#!

    ."!!+ ! "!

    12

    public Car(string make, string model, int elapsedMileage)

    {

    make = _make;

    model = _model;

    elapsedMileage = _elapsedMileage;

    }

    *2

    myCar = new Car("Nissan", "Altima", 31000);

    %&

    . "" "" @ * ! ! !"!!

    "!!A*!?A."A!A*"""! * ! )%*+!"""!""""!""!"4? "" ! " ! ! "" ! * )%*+(")%*+

  • 8/6/2019 CSharp - Workbook Question Edition

    95/168

    ! " !

    6!&)%*+(" + ( ! ! ! & #B 4 &

    >!"!")%*+!2

    myCar = null;

    3!"""A"ACC"

    6)%*+&!4

    .!!"2

    myCar = null;

    B!!")%*+"!6!"3!C""""C

    !"!

    "2

    %.*?+6*%2#"""(!!8

    !"%!

    !!"!"" ! " * #!#$ "! 9D:2

  • 8/6/2019 CSharp - Workbook Question Edition

    96/168

    ''''

    ~Car()

    {

    . . .

    }

    3!!""@74")%*+!&!!

    !#@!!!47!2

    7""!!")!"""!""4",E

    7""*!4 ! ! ""4,FG

    7!""

    7 " ! 6""?96?:!;

  • 8/6/2019 CSharp - Workbook Question Edition

    97/168

    ((((

    )&*

    *2

    +$,!#

    *2

    0-.)%*"2

    !"#$

    -+"

    '"!&"*""! 9*

    !"!B"!"!":

    "2HHH!HCH"HH""5I5I"@J

    "2HHH!HCH"HH""5I5"@J

  • 8/6/2019 CSharp - Workbook Question Edition

    98/168

    ....

    %

    #!&&"%&+

    #

    /!

    6 8 %&+

    %&"(

    !@

    K!#@

  • 8/6/2019 CSharp - Workbook Question Edition

    99/168

    -

    7&2

    2L%()L%()5KM%&

    ,K%N2

    o 6";& ; &8

    &&"!"!;

  • 8/6/2019 CSharp - Workbook Question Edition

    100/168

    !"#$%"""%

    &'(

    &"'( &

    )

    #* )+, ')- .

  • 8/6/2019 CSharp - Workbook Question Edition

    101/168

    '""/0","

    1 2 /# 3445 67 , +0 '

    ( ' '(

    $

    ')8 9,):!9,):9:"# ; 8 / >48;-':0

    '(,"?9(,:";7@,"88"" 88 "

  • 8/6/2019 CSharp - Workbook Question Edition

    102/168

    ',8/*6',A=A,BB0;/+;!;0"'),A'+;+;

    " "

    C87'(""*+'

    @'8""/0 ; '("

    $

    6=)D""D

    ;+6$

    $EE"EE5FF!7GEE4H5>FIG33J

    ,$C""

    !"#$

    '(,"; C"!C"(;)"";)""(;)""""";"

    5444B

  • 8/6/2019 CSharp - Workbook Question Edition

    103/168

    7$

    + +- +-+, +;C +' +#"# 2 ;"IK$

    System.Data.SqlClient.SqlConnection con;

    #L(;"!;$

    System.Data.SqlClient.SqlCommand cmd;

    System.Data.SqlClient.SqlDataReader dr;

    "$

    using System.Data.SqlClient;

    ...

    SqlConnection con;

    SqlCommand cmd;

    SqlDataReader dr;

    !$%&!

    #*+'"+!9D:9D:

  • 8/6/2019 CSharp - Workbook Question Edition

    104/168

    9,:8"9D,:

    #M"'(,"+(@7"('("L#9:/!0(9D(:9+#(-((:

    "8""/-""0"""

    @!D!D,DN

  • 8/6/2019 CSharp - Workbook Question Edition

    105/168

    ''''

    ;"'

    !()"$

    !!"#$%&'

    *+%!")

    "$

    (!)#$

    (*+'$

    (!)*('

  • 8/6/2019 CSharp - Workbook Question Edition

    106/168

    ,,,,

    -))*).

    &"""" /"O

    ""0

    $EEE"EMPE"E8EEE

    $EEE"E8EEEQJQ3MP

    ("M$

    $EEE"EMPE"E8EEE

    /0

    -"')

    '(,"

    '()/,)0

    )

  • 8/6/2019 CSharp - Workbook Question Edition

    107/168

    1111

    2%

    ')

    I #,A M

    3#M

    51!M

    G;2!"M

  • 8/6/2019 CSharp - Workbook Question Edition

    108/168

    ! "

    # # # #

    ! " " # $ " ! % "

    $% &'( " )* "

  • 8/6/2019 CSharp - Workbook Question Edition

    109/168

    &&&&

    '()

    + ! ,

    )-

    string[] cars = new string[5];

    cars[0] = "Oldsmobile";

    cars[1] = "Nissan";

    cars[2] = "Pontiac";

    cars[3] = "Ford";

    cars[4] = "Plymouth";

    $."

    /01

    "+"

    (2 " 3 4"5

    6"-

    MessageBox.Show(cars[2]);

  • 8/6/2019 CSharp - Workbook Question Edition

    110/168

    '"*(

    $74-

    for (int current = 0; current < cars.Length; current++)

    {

    MessageBox.Show(cars[current]);

    }

    )'0

    -$

    8

    MessageBox.Show(cars[5]);

    9%:3;89!"

    :?@@464@457)%'

    +),*

    9 , 8 "A"-

    string[,] cars = new string[5, 2];

    cars[0, 0] = "Oldsmobile Cutlas Supreme";

    cars[0, 1] = "Oldsmobile 98";

    cars[1, 0] = "Nissan Altima";

    cars[1, 1] = "Nissan Xtera";

    cars[2, 0] = "Pontiac Firebird";

  • 8/6/2019 CSharp - Workbook Question Edition

    111/168

    $AB C""B

    (A-

    string[,,] cars = new string[5, 2, 3];

    cars[0, 0, 0] = "Oldsmobile Cutlas Supreme - 1985";

    cars[0, 0, 1] = "Oldsmobile Cutlas Supreme - 1986";

    cars[0, 0, 2] = "Oldsmobile Cutlas Supreme - 1987";

    cars[0, 1, 0] = "Oldsmobile 98 - 1982";

    cars[0, 1, 1] = "Oldsmobile 98 - 1983";

    cars[0, 1, 2] = "Oldsmobile 98 - 1984";

    cars[1, 0, 0] = "Nissan Altima - 2001";

    cars[1, 0, 1] = "Nissan Altima - 2002";

    '

  • 8/6/2019 CSharp - Workbook Question Edition

    112/168

    +?9: " D ?9: E ' ?9: 4 50"D4A5E

    -.(

    -

    /0")1(

    -

    !%'-

    !"

    !2((/(

    9" DBE

    -FFFFAFFFG7@8H

  • 8/6/2019 CSharp - Workbook Question Edition

    113/168

  • 8/6/2019 CSharp - Workbook Question Edition

    114/168

    6$A,8

    !2

    I"-

    #

    ($!

  • 8/6/2019 CSharp - Workbook Question Edition

    115/168

    !"

    #$ %!& %' ()

  • 8/6/2019 CSharp - Workbook Question Edition

    116/168

    !"

    #*+#+,-.*++/0

    1 2324

    1,Bob Tabor,Visual Basic Programmer

    2,Dan Starr,C++ Programmer

    /3/5 6 7!&/7!&389

    7:;//

    4

    %

  • 8/6/2019 CSharp - Workbook Question Edition

    117/168

  • 8/6/2019 CSharp - Workbook Question Edition

    118/168

    ''''

    StreamReader sr = new StreamReader(@"c:\test.txt");

    string line;

    string contents = "";bool finished = false;

    while (!finished)

    {line = sr.ReadLine();

    if (line == null){

    finished = true;

    }else

    {contents += line;

    }

    }

    /F1" .!%A%(0+ .-:; :F;0":;

    :;:#,#;

    (!

    55/1!.0

    A G @A7(!.07(

  • 8/6/2019 CSharp - Workbook Question Edition

    119/168

    ))))

    StreamReader sr = new StreamReader(@"c:\test.txt");

    string line;string[] lineValues;

    ArrayList contents = new ArrayList();bool finished = false;

    while (!finished)

    {line = sr.ReadLine();if (line == null){

    finished = true;}

    else

    {lineValues = line.Split(',');

    contents.Add(lineValues);}

    }

    +7(77(4

    for (int counter = 0; counter < contents.Count; counter++)

    {lineValues = (string[])contents[counter];

    MessageBox.Show(lineValues[0] + " - "+ lineValues[1] + " - "+ lineValues[2]);

    }

    7(/$7.HI0&7($*!/:; &JA1

  • 8/6/2019 CSharp - Workbook Question Edition

    120/168

    ****

    #!%!#%(#(/

    4StreamWriter sw = new StreamWriter(@"c:\test2.txt");

    for (int counter = 0; counter < contents.Count; counter++){

    lineValues = (string[])contents[counter];sw.WriteLine(lineValues[0] + " - " +

    lineValues[1] + " - " +

    lineValues[2]);}

    !,

    !##(

    +

    7/!#&/!% 2 /&.0!%!#

    sr.Close();sw.Close();

    $

    /!"4

    '5

    12

    1!# 2

  • 8/6/2019 CSharp - Workbook Question Edition

    121/168

    ,-%

    /4

    !"#$%&'

    &./0,+%

    /4

    ()!

    1$!+/

    4

    ()*+(&'

    %%&%

    / ./,0

    43333BK335333

    4333/%*3737!&

    43333BK335333

    43333BK335333

  • 8/6/2019 CSharp - Workbook Question Edition

    122/168

    '%

    7!&

    7(

    10

    7 F %

    G%.0

    !"

    L@B

  • 8/6/2019 CSharp - Workbook Question Edition

    123/168

    2222

    8#7(7B

    ,

    D4

    4MN+LMN+L

  • 8/6/2019 CSharp - Workbook Question Edition

    124/168

    !" #!$%&% ! '()

    *$ " "

    ""

    *

    +, ! '

    '- )+ * ./

  • 8/6/2019 CSharp - Workbook Question Edition

    125/168

    0 ( " ( $ " "

    1 " 0"2"0))/.)!34"5)67"756 5" 6

    "1 " )" *"

    0*!8

    '9 :% .. 8

  • 8/6/2019 CSharp - Workbook Question Edition

    126/168

    !!!!

    '% % 8

    -("0*':

  • 8/6/2019 CSharp - Workbook Question Edition

    127/168

    ("+;;5+("+&*6 -56

    ( " % !:8

    -.&

    56"*'"*!5602"% & "

    &

    *" "5$6""1"

  • 8/6/2019 CSharp - Workbook Question Edition

    128/168

    """"

    5 ( & ?6

    #$%

    (" " "*""!++("@A("+($7 4 + +( +( " ++(A

    &)/.")/.)/. *"+7*8

    ).+B!0+':)C4#4

    +4"18

    D@+)+C4=4")C4(.4ECFGHI3

    '"%$'J " 4 "

    '

    "")/.)! ! !

  • 8/6/2019 CSharp - Workbook Question Edition

    129/168

    ((((

    +2(" " "" !"

    2(" $!" " ! !"5)/.)!656

    ("4!+

    $48

    !=++ $ 9

    !=++$9 * )/.+").+"D@"().44

    !=+$2+$ * (

    )*$+

    8

  • 8/6/2019 CSharp - Workbook Question Edition

    130/168

    ,,,,

    #-./*0

    8

    !"

    -)"8

    "!#$!

    1#

    D* 5&6

    %0))0@

  • 8/6/2019 CSharp - Workbook Question Edition

    131/168

    *"**

    +

    )/.

  • 8/6/2019 CSharp - Workbook Question Edition

    132/168

    &/

    7 " *

    ;'*K

    L"+"+

    $K'K

    M'+$*

    K

    H'$%K

  • 8/6/2019 CSharp - Workbook Question Edition

    133/168

    !"# # $%&"

    '( )"*+*#

    %+

    !

    ,- )"% '() ./ '(

  • 8/6/2019 CSharp - Workbook Question Edition

    134/168

    """"

    #$

    "#!0'1#2

    3#,43!0'11532

    1"52#"%3"%"#

    52%6"3#"

    1"2414(%'.'1 % 1 , ("

    17#)"$&"1

    %)"4%#'%1

    ! % %'.'1 81'0%')1'.'1'5#7(#%!#% %3 #"#

    " % 9+. 1 :%;%",%""

  • 8/6/2019 CSharp - Workbook Question Edition

    135/168

    %%%%

    1 ) % 1),6.'#1%#"5"# # 1"#"

    &'('#

    1 ( 5 *+* "

    1 7 *+* " % # , *+*8

    )(*

    0

  • 8/6/2019 CSharp - Workbook Question Edition

    136/168

    ++++

    ,&-

    1"2

    ./0)

    12

    !"##!

    4-%0'1

    "2

    "!!$!

    1.

    (1#

    $1#=&

    2>>>>?@>>>>>

  • 8/6/2019 CSharp - Workbook Question Edition

    137/168

    2222

    ("('()

    *

    %

    +

    *+*

  • 8/6/2019 CSharp - Workbook Question Edition

    138/168

  • 8/6/2019 CSharp - Workbook Question Edition

    139/168

    ! "#

    $%

    $%&'# ()*+

    !"

    #,-.*/*

    # # $ % # & ' #

    0 1( $%1

  • 8/6/2019 CSharp - Workbook Question Edition

    140/168

    ()()()()

    ,#!#*2*.%*3452*#

    (6 % %7 .!$4#

    '*

    ")",

    ()38(5 ()38(5

    ()&'%()&'*3593535

    :&;&'&'%*)$0$1

  • 8/6/2019 CSharp - Workbook Question Edition

    141/168

  • 8/6/2019 CSharp - Workbook Question Edition

    142/168

    ((((

    0-

    9% 325

    ,EEEEFAEEEEE(+

    1

    %%$%1

    +

  • 8/6/2019 CSharp - Workbook Question Edition

    143/168

    !!"#$%

    & & !

    '(

    )* +' +& ,-

  • 8/6/2019 CSharp - Workbook Question Edition

    144/168

    . / *#$0

    $ / / $ $ " #$%"0% $ #$+%

    1#1$

    0

    2 2 "

  • 8/6/2019 CSharp - Workbook Question Edition

    145/168

    !!!!

    "##"

    '( ! ! 34

    0int result;

    result = int.Parse(txtNumber1.Text) *

    int.Parse(txtNumber2.Text);

    MessageBox.Show(result.ToString());

    2340

    int result;

    try

    {

    result = int.Parse(txtNumber1.Text) *

    int.Parse(txtNumber2.Text);

    MessageBox.Show(result.ToString());

    }

    catch (Exception ex)

    {

    MessageBox.Show("There was a problem with your entry. " +

    "Please make sure you enter numeric values only.",

    "Input Problem",

    MessageBoxButtons.OK,

    MessageBoxIcon.Error);

    }

    $ 34 34 $

    $#$%$5 #6$ #7$ 1+ 8"#$ % ' , "'

    %19%0

  • 8/6/2019 CSharp - Workbook Question Edition

    146/168

    $$$$

    $"2,1%

    :'0

    int result;

    try

    {

    result = int.Parse(txtNumber1.Text) *

    int.Parse(txtNumber2.Text);

    MessageBox.Show(result.ToString());

    }

    catch (FormatException ex)

    {

    MessageBox.Show("There was a problem with your entry. " +

    "Please make sure you enter numeric values only.",

    "Input Problem",

    MessageBoxButtons.OK,MessageBoxIcon.Error);

    }

    catch (Exception ex)

    {

    MessageBox.Show("There was a problem. " +

    "Please note what you did and contact " +

    "tech support.",

    "General Problem",

    MessageBoxButtons.OK,

    MessageBoxIcon.Error);

    }

    $$' % %#*+

  • 8/6/2019 CSharp - Workbook Question Edition

    147/168

    %%%%

    &'(

    $ '0

    int result;

    OleDbConnection con = new OleDbConnection();

    con.ConnectionString = "Some connection string goes here";

    con.Open();

    try

    {

    result = int.Parse(txtNumber1.Text) *

    int.Parse(txtNumber2.Text);

    MessageBox.Show(result.ToString());

    }

    catch (FormatException ex){

    MessageBox.Show("There was a problem with your entry. " +

    "Please make sure you enter numeric values only.",

    "Input Problem",

    MessageBoxButtons.OK,

    MessageBoxIcon.Error);

    }

    finally

    {

    con.Close();

    }

    $ #1.2$$+)!2$!2;;#

    $ 9

  • 8/6/2019 CSharp - Workbook Question Edition

    148/168

    ))))

    *+

    ,'$' )

  • 8/6/2019 CSharp - Workbook Question Edition

    149/168

    0000

    "1

    ) ' 0

    68 :3478 :

    $ !1,&

    ?8 :@8 :

    )>'

    $$;"3A4903: 4 $ !1,&!2*$"

    1& " """

  • 8/6/2019 CSharp - Workbook Question Edition

    150/168

    !!!!

    {

    throw new Exception("Only numbers please.");

    }

    2$1+

    "0

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    $314

  • 8/6/2019 CSharp - Workbook Question Edition

    151/168

    !!!!

    2

    $0

    !"#

    341#"*

    $0

    $%&

    %*#$0

    $%'($!"#

    3

    $ 5$98

    0BBBB>CBBBBB$

  • 8/6/2019 CSharp - Workbook Question Edition

    152/168

    !!!!

    5/

    &+

    !

    '

    6#2 A +

    6 ) > ) >)>

  • 8/6/2019 CSharp - Workbook Question Edition

    153/168

    !!!!

    7)>

    ?)>

    "

    :0

    %"!

    0

    0BBBB>CBBB=BB

    D)!#:E

    $

  • 8/6/2019 CSharp - Workbook Question Edition

    154/168

    !

    ! "#

  • 8/6/2019 CSharp - Workbook Question Edition

    155/168

    !

    $%

    "#$%&!

    $%

    !"#

    &'$%

    "!$%!

    '(")'*%!+%,-

    !,%$!%

    !%.%!+%

    /!!"!

    ($ )$*+

    ,

    %--***--./--0--*-1!12131*1

  • 8/6/2019 CSharp - Workbook Question Edition

    156/168

    0000

    1

    3 4 *

    5!6.

    7.6.

    8".

  • 8/6/2019 CSharp - Workbook Question Edition

    157/168

    2222

    /%

    $ *"59 * )" 75,

    3

    $

  • 8/6/2019 CSharp - Workbook Question Edition

    158/168

  • 8/6/2019 CSharp - Workbook Question Edition

    159/168

    !"#!$%#&'

    "" $"%""12/3 ! " " "

    $4("(5$4$"(54"5$!"$/

    $""62/78$$" " $ " "$($4!$"62/5!$9*:$ " $ ""/,

    "$$

    "%$""4%5

    (#!$%#&)#*"

    ""$$45.. "! , !,!($$;-&"%'$!" $ " % "$"%"+"$$"%

    .

    ;$ . )$"$"1$

  • 8/6/2019 CSharp - Workbook Question Edition

    160/168

    +,+,+,+,

    =>>>>::::$?@:=

    >>>::::::

    6$

  • 8/6/2019 CSharp - Workbook Question Edition

    161/168

    ++++

    ($

    "$"$$"$*-/&$$4?5 *-/$ ""$12/$$

    D$!$$($""""$$$"$"$45E"$

    $ $ FGGH 4 $2$

    5$"'"'!$)"'$"$ $ " ! ! " " $""45" (

  • 8/6/2019 CSharp - Workbook Question Edition

    162/168

  • 8/6/2019 CSharp - Workbook Question Edition

    163/168

    ++++

    $$,+$"$"($12/"$C+ ( ,+ 12/

    ,$.+"$(

    $$$"$(

    $ $$ 4"5 $)$()$)$("$)$"$"#)$( " %) $ $ "#)$("$.+$&$$

    !"##0#(#'

    /!"$"%$E+ 12/ "12/ " "

  • 8/6/2019 CSharp - Workbook Question Edition

    164/168

    ++++

    , . '$$.

    %.12/%( " " $ " " $""$1&$$$(J> 12/ 3 J! ) ;"' 2 $KF7"8$"12/:3

    ."$.+"%!)$$""%!.+$$$"%%

    -"10

    +$$.+;$ $ " ( B/ "%B/%%B/DB/E"1+B/$"$78""+$$($("($

    "%+!!("(C $ " %! "' " $"@%78 " $ ! $"@"+ 45"$/=K

    !"2'

    $$("%" ." 1 $ /!(/!" " K>>> /,$$

  • 8/6/2019 CSharp - Workbook Question Edition

    165/168

    ++++

    !"3

    "$

  • 8/6/2019 CSharp - Workbook Question Edition

    166/168

  • 8/6/2019 CSharp - Workbook Question Edition

    167/168

    +6+6+6+6

    7

    $ (-

    F$$$""'"'?

    ="'"?

    K1.$$$12/$?

  • 8/6/2019 CSharp - Workbook Question Edition

    168/168

    2#

    D$( $ $5