Code Review Flashcards

(112 cards)

1
Q

What property is being used to change the background color for every element?

A
  1. BackColor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What property is being used to set the element’s variable name?

A
  1. Name ( in Design )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List out 5 values for image size mode

A
  1. Zoom
  2. StretchImage
  3. Normal
  4. AutoSize
  5. CenterImage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List out 2 values for the Visible property

A
  1. True
  2. False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to change the element text shown at the UI?

A
  1. Change the text at the Text property
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List out 9 properties for Text Alignment

A
  1. Top Left
  2. Top Center
  3. Top Right
  4. Middle Left
  5. Middle Center
  6. Middle Right
  7. Bottom Left
  8. Bottom Center
  9. Bottom Right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What property is set to ensure that the elements doesn’t relocate when the windows expand or shrink?

A
  1. Anchor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What property is being used to change the text color?

A
  1. ForeColor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What property in TextBox is used to hide the letters input?

A
  1. PasswordChar
  • Example
    When set to *
    Input 1234 will turn **
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List out 5 types of events

A
  1. Activate Events
    • Selects item on screen
  2. Changed Events
    • Modified control property
  3. Focus Events
    • Control gets or loses focus
  4. Key Events
    • Interact with keyboard
  5. Mouse Events
    • Interact with mouse
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to show the dialog box the shows the message when a button is pressed?

A

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(“Hello”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What property allows changing the element’s text font, font style, and font size?

A
  1. Font
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

List out 3 values that can be set in the BorderStyle

A
  1. None
  2. FixedSingle
  3. Fixed3D
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which properties can be used to enable auto size method?

A
  1. AutoSize
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an assignment operator?

A
  1. =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to assign the text from label1 to another text Hello?

A
  1. label1.Text = “Hello”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to change the label1 background color to another color ( blue ) after a button is pressed?

A

private void button1_Click(object sender, EventArgs e)
{
label1.BackColor = Color.Blue ;
}

this.BackColor = Color.Red;
// Will change the background color for the current form

this inside a class means the current instance of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to use concatenation?

A
  1. Use the + operator on string value to combine 2 string

“Welcome” + “to C#”

1 + 1 = 2
“1” + “1” = 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

List out how to write comment on
1. Single Line Comment
2. Multiline Comment

A
  1. //
  2. /**/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is identation being used for?

A
  1. Make code more human readable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How to close the current application form when a button is clicked?

A

private void exitButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

List out 3 types of error

A
  1. Syntax Errors
    • Show with jagged line
    • MessageBox.Sh(“Hi)
  2. Run-time Errors
    • Errors when program runs
    • num = 100 / 0
  3. Logic ( Semantic ) Errors
    • age = 2024 + bornYear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to declare variable car to store string Perodua?

A
  1. string car = “Perodua”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to declare variable age with current year ( DateTime ) minus birth year ( txtBornYear ) ?

A

int birthYear = int.Parse(txtBornYear.Text);
static int currentYear = DateTime.Now.Year;

int age = currentYear - birthYear ;

Improvement with Error Checking
int birthYear;
if (int.TryParse(txtBornYear.Text, out birthYear))
{
int currentYear = DateTime.Now.Year;
int age = currentYear - birthYear;
MessageBox.Show(“You are “ + age + “ years old.”);
}
else
{
MessageBox.Show(“Please enter a valid year.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
List out 3 naming conventions for Variable Names
1. The first character must be a letter ( upper / lowercase ) or an underscore ( _ ) 2. Name cannot contain spaces 3. Don't use C# Keywords or Reserved Words
26
How to assign string variable productDesciption value to label productLabel ?
productLabel.Text = productDescription MessageBox.Show(productLabel)
27
Create a variable message then combine "Hello" and "World" and show it in Message Box
string message message = "Hello" + "World" MessageBox.Show(message)
28
Can I access string myName at the method secondButton_Click? ``` private void firstButton_Click(object sender, EventArgs e) { // Local variable string myName; myName = nameTextBox.Text; } private void secondButton_Click(object sender, EventArgs e) { outputLabel.Text = myName; } ```
1. No, because string myName is a **local variable** inside firstButton_Click
29
How to declare firstName, middleName and lastName as a string variable in one line?
string firstName, middleName, lastName; firstName = "Leon"; middleName = "Scott"; lastName = "Kennedy"; MessageBox.Show($"The character you played as in RE4 is : {firstName} {middleName} {lastName}");
30
What is the most important symbol in the exam?
1. The semicolon ( ; ) when the C# code ends string firstName = "Leon"**;**
31
Which value cannot surround by double quote ( string ) ? ( 9 )
1. int 2. double 3. float 4. boolean 5. char 6. array 7. list 8. null 9. decimal
32
What is the final value when an operation involves an int and a double?
1. int is treated as double and the result is double
33
Is an operation involving a double and decimal is allowed?
1. No
34
How to perform 5 + 4 and pass it to message box ?
int x = 5; int y = 4; MessageBox.Show((x+y).ToString());
35
List out 3 methods to convert string to numeric data types
1. int.Parse() 2. double.Parse() 3. decimal.Parse()
36
How to parse label value from string to int ?
int hoursWorked = int.Parse(hoursWorkedTextBox1.Text);
37
What will happen when the hoursWorkedTextBox1 has value of "49.99"? int hoursWorked = int.Parse(hoursWorkedTextBox1.Text);
1. Catch FormatException error * To solve this error, we can use Int.TryParse or double.TryParse ``` double hoursWorked; if (double.TryParse(hoursWorkedTextBox1.Text, out hoursWorked)) { MessageBox.Show("You entered: " + hoursWorked); } else { MessageBox.Show("Please enter a valid number."); } ```
38
What method is being used to display numeric values?
1. .ToString() method
39
How to show int myNumber = 123 to message box?
int myNumber = 123; MessageBox.Show(myNumber.ToString()); * Will get cannot convert from 'int' to 'string' when .ToString() isn't with myNumber in MessageBox
40
Use ToString() method to show number 12.3 with result 12.300
double x = 12.3; MessageBox.Show(x.ToString(**"n3"**)); * Use "n3" inside .ToString()
41
Use ToString() method to show number 123 with result 123.00
int x = 123; MessageBox.Show(x.ToString(**"f2"**)); * Use "f2" inside .ToString()
42
Use ToString() method to show number 123456.0 with result 1.235e+005
double x = 12.3; MessageBox.Show(x.ToString(**"e3"**)); * Use "e3" inside .ToString()
43
Use ToString() method to show number -12.3 with result ($12.3)
double x = 12.3; MessageBox.Show(x.ToString(**"C"**)); * Use "C" inside .ToString()
44
Use ToString() method to show number .234 with result 23.40%
double x = .234; // Same as 0.234 MessageBox.Show(x.ToString(**"P"**)); * Use "P" inside .ToString()
45
What is the difference between GroupBoxes and Panels
1. A panel cannot display a title and does not have a Text property, but a GroupBox supports these 2 properties 2. A panel's border can be specified by its BorderStyle property, while the GroupBox cannot be
46
What property is similar to the Image property of a PictureBox?
1. BackgroundImage
47
List out 3 tabs that Windows Form Application provides for Colors
1. Web 2. System 3. Custom
48
How to create a access key for buttons? I want press Alt + E to close the page
1. Go to Text property and type &Exit 2. Then add code ``` public void exitButton_Click (object sender, EventArgs e ) { this.Close(); } ```
49
List out 2 predefined constants that the Math Class provide
1. Math.PI 2. Math.E
50
How to use math class square root?
1. Math.Sqrt(x);
51
How to use math class power?
1. Math.Pow(x,y) x = 2 , y = 3 will be 2³ = 8
52
What is an exception?
1. Is an unexpected error that happens while a program is running * Will abruptly halt if an exception is not handled by the program
53
What code is used for exception handling?
try {} catch {}
54
How to try parse text box input of age from string into int?
``` try { int age; age = int.Parse(txtAge.Text); } catch { MessageBox.Show("Invalid Value Entered") } ```
55
How to create a constant value of my birth year 2003?
const int myBirthYear = 2003; * a constant variable represents a value that cannot be changed during the program's execution
56
How to declare variables as fields so that every method can use that value?
``` public partial class Form1: { private string name = "Jack"; public Form1() { InitializeComponent(); } private void showNameButton_Click(object sender, EventArgs e) { MessageBox.Show(name); } private void chrisButton_Click(object sender, EventArgs e) { name="Chris"; } } ``` * Think of Fields as **Global Variable** inside this class
57
How to show message box with the requirement below 1. Title : Logout 2. Description : Are you sure ? 3. Icon : Question Mark 4. Button : With Ok and Cancel
``` MessageBox.Show("Are you sure?" , "Logout" , MessageBoxButtons.OkCancel, MessageBoxIcon.Question); ```
58
List out all 6 relational operators
1. > - Greater than 2. < - Less than 3. >= - Greater than or equal to 4. <= - Less than or equal to 5. == - Equal to 6. != - Not equal to
59
Write a program to receive temperature from numeric up down with the conditions 1. >= 40 is Hot 2. < 40 and >= 10 is Ok 3. < 10 is Cold
``` int temp = (int) numTemp.Value; if ( temp >= 40 ) { MessageBox.Show("Hot"); } else if ( temp < 40 && temp >= 10 ) // or ( temp >= 10 ) is enough { MessageBox.Show("Ok"); } else { MessageBox.Show("Cold"); } ```
60
List out 3 logical operator
1. && - And 2. || - Or 3. ! - Not
61
Write a program to receive months from numeric up down with showing the correct month ( 1 - January )
``` int month_num = ( int ) numMonth.Value ; switch ( month_num ) { case 1: MessageBox.Show("January"); break; case 2: MessageBox.Show("February"); break; case 3: MessageBox.Show("March"); break; case 4: MessageBox.Show("April"); break; case 5: MessageBox.Show("May"); break; case 6: MessageBox.Show("June"); break; case 7: MessageBox.Show("July"); break; case 8: MessageBox.Show("August"); break; case 9: MessageBox.Show("September"); break; case 10: MessageBox.Show("October"); break; case 11: MessageBox.Show("November"); break; case 12: MessageBox.Show("December"); break; default: MessageBox.Show("No month found"); break; } ``` * Break statement in every case is a must or it will run all statement at each case * Another method using Array ``` string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int monthIndex = (int)numMonth.Value; // 1 through 12 if (monthIndex >= 1 && monthIndex <= 12) { MessageBox.Show(months[monthIndex - 1]); } else { MessageBox.Show("No month found"); } ```
62
Write a program to compare if 2 name is the same using String.Compare
``` string name1 = "Mary"; string name2 = "Mark"; if ( String.Compare(name1, name2) == 0 ) { MessageBox.Show("Both are the same name") } else { MessageBox.Show("Both name are not the same") } ``` * String.Compare **returns 0** when the two strings are exactly equal. * For case sensitive checking ``` String.Compare(name1, name2, StringComparison.OrdinalIgnoreCase) == 0 ```
63
What is the boolean variable called when is being used inside if statement? ``` bool grandMaster = false; if (grandMaster) { powerLevel += 500; //powerLevel = powerLevel +500; } if (!grandMaster) { powerLevel = 100; } ```
1. Flags
64
Write a program to use int.TryParse to return age from textbox
int age; if ( int.TryParse(inputAge.Text , out age) ) { MessageBox.Show($"You are {age} years old") } else { MessageBox.Show("Invalid age input") } * Also have **double.TryParse()** and **decimal.TryParse()**
65
What is input validation?
1. Process of inspecting data that has been entered into a program to make sure it is valid before it is used if ( textScore >= 0 && textScore <= 100 )
66
What is the difference between Radio Buttons and Check Boxes?
1. Radio buttons can only choose one from several possible choices, while Check Boxes can select multiple options
67
Write a program to check if the radio button male or female is pressed, then return text Male and Female
``` if ( rdrMale.Checked ) { MessageBox.Show("Male"); } else if ( rdrFemale.Checked ) { MessageBox.Show("Female"); } ``` or ``` MessageBox.Show(rdrMale.Text); ```
68
What event is when the checked property changes?
1. CheckedChanged
69
Write a program to add items to list box (listBoxFruits) when the program is first loaded then print out the fruit when user had press the btnShowSelected
``` private void Form1_Load(object sender, EventArgs e) { // Add items to the ListBox when the form loads listBoxFruits.Items.Add("Apple"); listBoxFruits.Items.Add("Banana"); listBoxFruits.Items.Add("Cherry"); listBoxFruits.Items.Add("Durian"); listBoxFruits.Items.Add("Elderberry"); } private void btnShowSelected_Click(object sender, EventArgs e) { // Check if an item is selected if (listBoxFruits.SelectedItem != null) { MessageBox.Show($"You selected: {listBoxFruits.SelectedItem.ToString()}"); } else { MessageBox.Show("Please select a fruit first!"); } } ``` ``` if ( listBoxFruit.SelectedIndex == -1 ) { MessageBox.Show("Nothing Selected") } ``` ``` listBoxFruits.Items.Clear(); ```
70
Write a program to print Hello 5 times
``` for ( int i = 0 ; i < 5 ; i ++ ) { MessageBox.Show("Hello") } ```
71
How to show 1 to 100 using for loop?
``` for ( int i = 1 ; i <= 100 ; i++ ) { MessageBox.Show(i.ToString()); } ```
72
How to show 100 to 1 using for loop?
``` for ( int i = 100 ; i <= 1 ; i-- ) { MessageBox.Show(i.ToString()); } ```
73
List out 2 pretest loops
1. for loop 2. while loop
74
List out posttest loop
1. do-while loop
75
Write a program using while loop to show Hello 5 times
``` int i = 0 ; while ( i < 5 ){ MessageBox.Show("Hello"); i++ ; } ``` Omitting i ++ will be showing **Hello** Infinitely
76
Determine the output for each message box value ( Assume that it has .ToString() method ) ``` int a = 10; int b = 20; MessageBox.Show(a--); //1. b = a; MessageBox.Show(b--); //2. MessageBox.Show(a); //3. MessageBox.Show(b); //4. ```
1. 10 - a-- means show a then only -1 2. 9 - since a is shown, a is then -1 and assigned to b 3. 9 4. 8
77
Determine the output for each message box value ( Assume that it has .ToString() method ) ``` int a = 10; int b = 20; MessageBox.Show(--a); //1. b = a; MessageBox.Show(--b); //2. MessageBox.Show(a); //3. MessageBox.Show(b++); //4. ```
1. 9 2. 8 3. 9 4. 8 - Show b first then only + 1
78
What are the term for below code? 1. count ++ 2. ++ count
1. Postfix Mode 2. Prefix Mode
79
How to show the MessageBox twice using do while loop?
int i = 1 ; do { MessageBox.Show("Hello"); i ++ ; } while ( number <= 2 ) ;
80
Write a void method that calculate age by current year - birth year
``` private void myButton_Click(object sender, EventArgs e) { ReturnAge(); } private void ReturnAge(){ const int currentYear = DateTime.Now.Year; try { int birthYear = int.Parse(txtYear.Text); int age = currentYear - birthYear; MessageBox.Show(age.ToString()); } catch(Exception e){ MessageBox.Show("Invalid Birth Year") } } ```
81
Write a int return method that calculate age by current year - birth year
``` private void myButton_Click(object sender, EventArgs e) { const int currentYear = DateTime.Now.Year; int birthYear; if ( int.TryParse(txtAge.Text, out birthYear) ) { } else { MessageBox.Show("Invalid Birth Year") return; } int age = ReturnAge(currentYear, birthYear); MessageBox.Show(age.ToString()); } private int ReturnAge(int currentYear, int birthYear){ int age = currentYear - birthYear; return age; } ```
82
List out 2 access modifier for methods
1. private 2. public
83
Answer which is Parameter and Argument for the below code ``` DisplayValue(50); //1. private void DisplayValue(int value) // 2. { MessageBox.Show(value.ToString()); } ``` * I can pass in arguments like this ``` int x = 2; DisplayValue(x); DisplayValue(x * 4); ```
1. Argument 2. Paramter
84
Can I pass double or decimal arguments into int parameters?
1. No
85
Can I pass int arguments into double parameters?
1. Yes
86
Can I pass decimal arguments into double parameters?
1. No
87
Can I pass int arguments into decimal parameters?
1. Yes
88
Can I pass double arguments into decimal parameters
1. No
89
Write a program which shows a method that sum the 2 parameter
``` int sum = SumTwoValue ( 1 , 2 ) // Arguments private int SumTwoValue ( int a , int b ) { // Parameter return a + b ; } ```
90
Write a program which shows a method that sum the 2 parameter, but b has default value of 4
``` // 2 is overiding the default argument int sum = SumTwoValue ( 1 , 2 ) // Arguments , Ans = 3 int sum = SumTwoValue ( 1 ) // Arguments , Ans = 5 private int SumTwoValue ( int a , int b = 4 ) { // Parameter return a + b ; } ```
91
Write a program to return age by currentYear - birthYear with the following conditions 1. void method 2. Using out keywords
``` private void btn1_Click(object sender, EventArgs e) { int age; showAge(out age); MessageBox.Show($"Age : {age}") } private void showAge(out int age) { int currentYear = DateTime.Now.Year; int birthYear = int.Parse( txtYear.Text ) age = currentYear - birthYear } ```
92
Write a program with using reference parameter to return whatever number passed in to 0
int myVar = 99; SetToZero(ref myVar); private void SetToZero(ref int number) { number = 0 ; }
93
Initialize an array with creating a const variable to store the size of the array and add elements into it
``` const int size = 5; int[] numAry = new int[size]; numAry[0] = 1; numAry[1] = 2; numAry[2] = 3; numAry[3] = 4; numAry[4] = 5; // This line will cause an error: numAry[5] = 6; // IndexOutOfRangeException ``` * We can also do the easy way int[] numAry = new int[] {1,2,3,4,5}
94
Loop through number of array with the array given int[] numAry = new int[] {1,2,3,4,5}
int[] numAry = new int[] {1,2,3,4,5} for ( int i = 0 ; i < numAry.Length ; i ++ ) { MessageBox.Show($"{numAry[i]}"); } // or foreach ( int num in numAry ) { MessageBox.Show($"{num}"); }
95
Write a program with the conditions below 1. create size variable to hold the array of the size ( 5 ) 2. use loop to add 5 , 4 , 3 , 2 , 1 into the array
``` const int size = 5; int[] numAry = new int[size]; int assignNum = 5; for ( int i = 0 ; i < numAry.Length ; i++ ) { numAry[i] = assignNum; asignNum--; } ```
96
Create an array and show the length of the array in MessageBox
``` int[] numAry = {1 , 2 , 3 , 4 , 5}; MessageBox.Show(numAry.Length.ToString()); ```
97
How to show the items in array using messagebox with foreach loop?
``` int[] numbers = { 1 , 2 , 3 , 4 , 5 } foreach ( int num in numbers ) { MessageBox.Show(num.ToString()); } ```
98
Write a method that 1. Pass in an array 2. Pass in an integer for the method to search inside the array 3. Add MessageBox inside the method to show if the number is found and show the position
``` int[] numAry = { 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; // Passed in the array and want the method to found 7 inside the array searchAry( numAry , 7) MessageBox.Show("7 is {searchAry().ToString()} inside the array"); private void searchAry( int[] ary , int val ) { bool found = false; int index = 0 ; int position = 0; while ( !found && index < ary.Length) { if ( ary[index] == val ) { found = true; position = index; MessageBox.Show($"{val} is found inside the array, and the position is at {position}"); } index ++ ; } } ```
99
Create a array and sum up the value
``` int[] numAry = {10, 20 , 1, 2 , 3}; int total = 0; foreach ( int val in numAry ) { total += val; } MessageBox.Show(total.ToString()); ```
100
Write a method to compare 2 array and return true or false
``` int[] numAry1 = {1 , 2 , 3 , 4 , 5 }; int[] numAry2 = {1 , 2 , 3 , 4 , 5 , 5 }; bool check = checkAryLength(numAry1, numAry2) ; if ( !check ) { MessageBox.Show("Both array length is not the same"); } else { MessageBox.Show("Both array length is the same") } private bool checkAryLength( int[] ary1 , int[] ary2 ) { return ary1.Length == ary2.Length } ```
101
Create 2 array for 1 with value and 1 without value, then copy array1's value to array2 ( Method )
``` int[] numAry1 = {1 , 2 , 3 , 4 , 5 }; int[] numAry2 = new int[numAry1.Length]; copyAry(numAry1 , numAry2); private void copyAry ( int[] ary1 , int[] ary2 ) { for ( int i = 0 ; i < ary1.Length ; i ++ ) { ary2[i] = ary1[i]; } } ```
102
Write a method to return the highest number inside the array
``` int[] numAry = {10, 20 , 1, 2 , 3}; int highestVal = HighestVal(numAry); MessageBox.Show($"The highest value inside the array is {highestVal}"); private int HighestVal( int[] ary1 ) { int highest = ary1[0]; for ( int i = 0; i < ary1.Length ; i ++ ) { if ( ary1[i] >= highest ) { = ary1[i]; } return lowest; } } ```
103
Write a method to return the lowest number inside the array
``` int[] numAry = {10, 20 , 1, 2 , 3}; int lowestVal = LowestVal(numAry); MessageBox.Show($"The lowest value inside the array is {lowestVal}"); private int LowestVal( int[] ary1 ) { int lowest = ary1[0]; for ( int i = 0; i < ary1.Length ; i ++ ) { if ( ary1[i] <= lowest ) { lowest = ary1[i]; } return lowest; } } ```
104
Create a list ( numList ) with 1. Adding 11 , 28 , 35 seperately 2. Copy list items to lstOutput ( ListBox ) 2. Insert 53 in the second index 3. Remove index 4 items 4. Remove number 11 5. Reverse list items 6. Sort the list in ascending order 7. Find number 28 in list 8. Clear the list
``` List numList = new List(); // 1. Add Items numList.Add(11); numList.Add(28); numList.Add(35); // 2. Copy List Items foreach ( int val in numList ) { lstOutput.Items.Add(val); } // 3. Insert 53 numList.Insert(2,53); // 4. Remove index 4 numList.RemoveAt(4); // 5. Remove number 11 numList.Remove(11); // 6. Reverse the list numList.Reverse(); // 7. Sort the list in Ascending order numList.Sort(); // 8. Find number 28 and return boolean values bool find = numList.Contains(28); // 9. Clear the lit numList.Clear(); ```
105
Write a class named student 1. How to access the class 2. Add Field ( Name , Gender , Grade ) with get and set 3. Add PrintDetails method 4. Add Calculate GCPA method 5. Add constructor 6. Add constructor overload with passing name, gender and grade into it 7. Call object
``` // 7. Call Class , Object Student student1 = new Student("Mark" , "Male" , "80") student1.printDetails(); double cgpa = student1.calculateCGPA(); MessageBox.Show($"CGPA : {cgpa}") class Student { // 2. Add Field private string _name { get; set; } ; private string _gender { get; set; } ; private int _mark { get; set; } ; // 5. Add Constructor public Student() { } // 6. Constructor Overloading public Student ( string name, string gender , int mark ) { _name = name; _gender = gender; _mark = mark; } // 3. Print Details method public void printDetails(){ MessageBox.Show($"Name ; {_name} , Gender : {_gender} , Mark : {_mark}"); } // 4. Calculate CGPA public double calculateCGPA(){ if ( _mark >= 80 ) { return 4.0; } // Other Logic here } } ```
106
What is the rule for constructor?
1. Same name as class 2. No return type 3. Assign value to member field 4. Can have none or many constructor
107
Write a method that pass in class / object as a parameter
``` Student student = new Student(); ShowStudentStatus(student); private void ShowStudentStatus( Student student ) { MessageBox.Show("Details : " + students.DisplayDetails()); } ```
108
What is private field also known as?
1. Backing field * It backs the public field
109
Write a class pet with the conditions below 1. A constructor that empty the backing field 2. Create backing field _name 3. Create a public property Name with get and set accessor 4. Use the class and set the name to Fido
``` class Pet { // 2. private string _name; // 1. public Pet(){ _name = ""; } // 3. public string Name { get { return _name; } set { _name = value; } } } // 4. Pet pet = new Pet(); // Calls the set accessor and sets _name = "Fluffy" pet.Name = "Fido" ; // Calls the get accessor and returns "Fido" string petName = myPet.Name; ``` * private backing field is set to private to protect it from accidental corruption * To create a **Read-Only Properties**, remove the **set accessor**
110
How to create a Read-Only Property?
1. Remove the set accessor inside the public properties public string Name { get { return _name; } }
111
List out the term for the below: 1. Constructor that accepts arguments 2. Multiple versions of the same method
1. Parameterized Constructor 2. Overloaded Methods ``` public void Deposit(decimal amount) { } public void Deposit(double amount) { } // overloaded public void Deposit(int numbers,string name) { } // overloaded public void Deposit(string names) { } // overloaded ```
112
How to show AdminDashbord page with hiding login page?
``` // Access Form Class Dashboard adminDashboard = new Dashboard(staffId , this); // Show the AdminDashboard form adminDashboard.Show(); // Hide the Login Page this.Hide(); ```