Skip to content

Commit 9be0bbe

Browse files
committed
Finished updates with the overloading methods section
1 parent 267965f commit 9be0bbe

File tree

1 file changed

+188
-29
lines changed

1 file changed

+188
-29
lines changed

notebooks/0103-MethodsEvents.ipynb

Lines changed: 188 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,103 @@
3131
},
3232
{
3333
"cell_type": "code",
34-
"execution_count": 6,
34+
"execution_count": 1,
3535
"metadata": {},
3636
"outputs": [
3737
{
3838
"data": {
3939
"text/html": [
40-
"0.95"
40+
"\r\n",
41+
"<div>\r\n",
42+
" <div id='dotnet-interactive-this-cell-26992.b1b9e0ebe05f4c528245537894ac84a4' style='display: none'>\r\n",
43+
" The below script needs to be able to find the current output cell; this is an easy method to get it.\r\n",
44+
" </div>\r\n",
45+
" <script type='text/javascript'>\r\n",
46+
"// ensure `require` is available globally\r\n",
47+
"if (typeof require !== typeof Function || typeof require.config !== typeof Function) {\r\n",
48+
" let require_script = document.createElement('script');\r\n",
49+
" require_script.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js');\r\n",
50+
" require_script.setAttribute('type', 'text/javascript');\r\n",
51+
" require_script.onload = function () {\r\n",
52+
" loadDotnetInteractiveApi();\r\n",
53+
" };\r\n",
54+
"\r\n",
55+
" document.getElementsByTagName('head')[0].appendChild(require_script);\r\n",
56+
"}\r\n",
57+
"else {\r\n",
58+
" loadDotnetInteractiveApi();\r\n",
59+
"}\r\n",
60+
"\r\n",
61+
"async function probeAddresses(probingAddresses) {\r\n",
62+
" function timeout(ms, promise) {\r\n",
63+
" return new Promise(function (resolve, reject) {\r\n",
64+
" setTimeout(function () {\r\n",
65+
" reject(new Error('timeout'))\r\n",
66+
" }, ms)\r\n",
67+
" promise.then(resolve, reject)\r\n",
68+
" })\r\n",
69+
" }\r\n",
70+
"\r\n",
71+
" if (Array.isArray(probingAddresses)) {\r\n",
72+
" for (let i = 0; i < probingAddresses.length; i++) {\r\n",
73+
"\r\n",
74+
" let rootUrl = probingAddresses[i];\r\n",
75+
"\r\n",
76+
" if (!rootUrl.endsWith('/')) {\r\n",
77+
" rootUrl = `${rootUrl}/`;\r\n",
78+
" }\r\n",
79+
"\r\n",
80+
" try {\r\n",
81+
" let response = await timeout(1000, fetch(`${rootUrl}discovery`, {\r\n",
82+
" method: 'POST',\r\n",
83+
" cache: 'no-cache',\r\n",
84+
" mode: 'cors',\r\n",
85+
" timeout: 1000,\r\n",
86+
" headers: {\r\n",
87+
" 'Content-Type': 'text/plain'\r\n",
88+
" },\r\n",
89+
" body: probingAddresses[i]\r\n",
90+
" }));\r\n",
91+
"\r\n",
92+
" if (response.status == 200) {\r\n",
93+
" return rootUrl;\r\n",
94+
" }\r\n",
95+
" }\r\n",
96+
" catch (e) { }\r\n",
97+
" }\r\n",
98+
" }\r\n",
99+
"}\r\n",
100+
"\r\n",
101+
"function loadDotnetInteractiveApi() {\r\n",
102+
" probeAddresses([\"http://192.168.1.71:1002/\", \"http://172.29.80.1:1002/\", \"http://127.0.0.1:1002/\"])\r\n",
103+
" .then((root) => {\r\n",
104+
" // use probing to find host url and api resources\r\n",
105+
" // load interactive helpers and language services\r\n",
106+
" let dotnet_require = require.config({\r\n",
107+
" context: '26992.b1b9e0ebe05f4c528245537894ac84a4',\r\n",
108+
" paths: {\r\n",
109+
" 'dotnet-interactive': `${root}resources`\r\n",
110+
" }\r\n",
111+
" }) || require;\r\n",
112+
" if (!window.dotnet_require) {\r\n",
113+
" window.dotnet_require = dotnet_require;\r\n",
114+
" }\r\n",
115+
" \r\n",
116+
" dotnet_require([\r\n",
117+
" 'dotnet-interactive/dotnet-interactive'\r\n",
118+
" ],\r\n",
119+
" function (dotnet) {\r\n",
120+
" dotnet.init(window);\r\n",
121+
" },\r\n",
122+
" function (error) {\r\n",
123+
" console.log(error);\r\n",
124+
" }\r\n",
125+
" );\r\n",
126+
" })\r\n",
127+
" .catch(error => {console.log(error);});\r\n",
128+
" }\r\n",
129+
" </script>\r\n",
130+
"</div>"
41131
]
42132
},
43133
"metadata": {},
@@ -46,7 +136,7 @@
46136
{
47137
"data": {
48138
"text/html": [
49-
"0.80"
139+
"0.95"
50140
]
51141
},
52142
"metadata": {},
@@ -81,12 +171,6 @@
81171
" return 0.95m;\n",
82172
" }\n",
83173
" \n",
84-
" public decimal CalculateGradeForClass(string className, short yearEnrolled) {\n",
85-
" \n",
86-
" return 0.80m;\n",
87-
" \n",
88-
" }\n",
89-
" \n",
90174
" public class Grade {\n",
91175
" \n",
92176
" public string NameOfClass { get; set; }\n",
@@ -97,7 +181,6 @@
97181
"\n",
98182
"var s = new Student();\n",
99183
"display(s.CalculateGradeForClass(\"Quantum Physics\"));\n",
100-
"display(s.CalculateGradeForClass(\"Quantum Physics\", 2019));\n",
101184
"\n",
102185
"var g = new Student.Grade();\n",
103186
"display(g);\n"
@@ -111,16 +194,83 @@
111194
"\n",
112195
"### Overloading Methods\n",
113196
"\n",
114-
"TO DO\n",
197+
"Just like we can provide multiple constructors, as shown in Session 2, we can also provide multiple methods with the same name and different parameters and return types. This is called **Method Overloading** and can be very valuable to provide alternate interactions with your class that have the same intention. \n",
198+
"\n",
199+
"Let's revisit the `CalculateGradeForClass` method in our last example. Typically, we would _assume_ that we are calculating the grade for a student for the current semester or year. What if we wanted to go back and recalculate for a different time? A different method with a similar signature, but additionally accepting a year parameter could be added to provide additional context. "
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 2,
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"data": {
209+
"text/plain": [
210+
"Grade for this year: 0.80"
211+
]
212+
},
213+
"metadata": {},
214+
"output_type": "display_data"
215+
},
216+
{
217+
"data": {
218+
"text/plain": [
219+
"Grade for 2019: 0.80"
220+
]
221+
},
222+
"metadata": {},
223+
"output_type": "display_data"
224+
}
225+
],
226+
"source": [
227+
"class Student {\n",
228+
" \n",
229+
" public void Enroll() {\n",
230+
" // do enrollment steps\n",
231+
" }\n",
232+
"\n",
233+
" // This signature assumes calculating the grade for a class THIS YEAR\n",
234+
" public decimal CalculateGradeForClass(string className) {\n",
235+
" \n",
236+
" // Let's delegate the calculation to the OTHER implementation of CalculateGradeForClass and pass this year\n",
237+
" return CalculateGradeForClass(className, DateTime.Now.Year);\n",
238+
" \n",
239+
" }\n",
240+
" \n",
241+
" public decimal CalculateGradeForClass(string className, int yearEnrolled) {\n",
242+
" \n",
243+
" // grade calculation code\n",
244+
" \n",
245+
" return 0.80m;\n",
246+
" \n",
247+
" }\n",
248+
" \n",
249+
" public class Grade {\n",
250+
" \n",
251+
" public string NameOfClass { get; set; }\n",
252+
" \n",
253+
" }\n",
254+
" \n",
255+
"}\n",
115256
"\n",
257+
"var s = new Student();\n",
258+
"display(\"Grade for this year: \" + s.CalculateGradeForClass(\"Quantum Physics\"));\n",
259+
"display(\"Grade for 2019: \" + s.CalculateGradeForClass(\"Quantum Physics\", 2019));"
260+
]
261+
},
262+
{
263+
"cell_type": "markdown",
264+
"metadata": {},
265+
"source": [
116266
"### The Magic params Parameter and Optional Parameters\n",
117267
"\n",
118268
"There is a special 'catch-all' `params` keyword you can use in your methods that will catch any arguments that are passed in to the method. Let's update our student with the ability to calculate grade point average:"
119269
]
120270
},
121271
{
122272
"cell_type": "code",
123-
"execution_count": 4,
273+
"execution_count": 3,
124274
"metadata": {},
125275
"outputs": [
126276
{
@@ -233,7 +383,7 @@
233383
},
234384
{
235385
"cell_type": "code",
236-
"execution_count": 18,
386+
"execution_count": 4,
237387
"metadata": {},
238388
"outputs": [
239389
{
@@ -381,7 +531,7 @@
381531
},
382532
{
383533
"cell_type": "code",
384-
"execution_count": 28,
534+
"execution_count": 5,
385535
"metadata": {},
386536
"outputs": [
387537
{
@@ -476,13 +626,22 @@
476626
},
477627
{
478628
"cell_type": "code",
479-
"execution_count": 5,
629+
"execution_count": 6,
480630
"metadata": {},
481631
"outputs": [
632+
{
633+
"data": {
634+
"text/plain": [
635+
"Inside of DoSomething myValue is: 11"
636+
]
637+
},
638+
"metadata": {},
639+
"output_type": "display_data"
640+
},
482641
{
483642
"data": {
484643
"text/html": [
485-
"<div class=\"dni-plaintext\">11</div>"
644+
"11"
486645
]
487646
},
488647
"metadata": {},
@@ -506,7 +665,7 @@
506665
},
507666
{
508667
"cell_type": "code",
509-
"execution_count": 31,
668+
"execution_count": 7,
510669
"metadata": {},
511670
"outputs": [
512671
{
@@ -575,7 +734,7 @@
575734
},
576735
{
577736
"cell_type": "code",
578-
"execution_count": 34,
737+
"execution_count": 8,
579738
"metadata": {},
580739
"outputs": [
581740
{
@@ -633,7 +792,7 @@
633792
},
634793
{
635794
"cell_type": "code",
636-
"execution_count": 39,
795+
"execution_count": 9,
637796
"metadata": {},
638797
"outputs": [
639798
{
@@ -662,7 +821,7 @@
662821
},
663822
{
664823
"cell_type": "code",
665-
"execution_count": 8,
824+
"execution_count": 10,
666825
"metadata": {},
667826
"outputs": [
668827
{
@@ -692,7 +851,7 @@
692851
},
693852
{
694853
"cell_type": "code",
695-
"execution_count": 44,
854+
"execution_count": 11,
696855
"metadata": {},
697856
"outputs": [
698857
{
@@ -743,7 +902,7 @@
743902
},
744903
{
745904
"cell_type": "code",
746-
"execution_count": 57,
905+
"execution_count": 12,
747906
"metadata": {},
748907
"outputs": [
749908
{
@@ -837,7 +996,7 @@
837996
},
838997
{
839998
"cell_type": "code",
840-
"execution_count": 46,
999+
"execution_count": 13,
8411000
"metadata": {},
8421001
"outputs": [
8431002
{
@@ -887,7 +1046,7 @@
8871046
},
8881047
{
8891048
"cell_type": "code",
890-
"execution_count": 89,
1049+
"execution_count": 14,
8911050
"metadata": {},
8921051
"outputs": [
8931052
{
@@ -934,7 +1093,7 @@
9341093
},
9351094
{
9361095
"cell_type": "code",
937-
"execution_count": 91,
1096+
"execution_count": 15,
9381097
"metadata": {},
9391098
"outputs": [
9401099
{
@@ -1005,7 +1164,7 @@
10051164
},
10061165
{
10071166
"cell_type": "code",
1008-
"execution_count": 2,
1167+
"execution_count": 16,
10091168
"metadata": {},
10101169
"outputs": [
10111170
{
@@ -1052,7 +1211,7 @@
10521211
},
10531212
{
10541213
"cell_type": "code",
1055-
"execution_count": 3,
1214+
"execution_count": 17,
10561215
"metadata": {},
10571216
"outputs": [
10581217
{
@@ -1103,7 +1262,7 @@
11031262
},
11041263
{
11051264
"cell_type": "code",
1106-
"execution_count": 4,
1265+
"execution_count": 18,
11071266
"metadata": {},
11081267
"outputs": [
11091268
{
@@ -1132,7 +1291,7 @@
11321291
},
11331292
{
11341293
"cell_type": "code",
1135-
"execution_count": 5,
1294+
"execution_count": 19,
11361295
"metadata": {},
11371296
"outputs": [
11381297
{

0 commit comments

Comments
 (0)