Transcripts

Coding 101 36 (Transcript)

Shannon Morse: On this episode of Coding 101 – Collections! All sorts of them.

Netcasts you love, from people you trust. This is TWIT. Bandwidth for Coding 101 is provided by CacheFly at cachefly.com. This episode of Coding 101 is brought to you by Square Space - the all in one platform that makes it fast and easy to create your own professional website or online portfolio. Visit our website for a free 2 week trial and 10 % off. Go to squarespace.com and use the offer code CODING.

And by Lynda.com, an easy and affordable way to help you learn.  Instantly stream thousands of courses created by experts on business software web development, graphic design and more. For a free trial visit Lynda.com/c101. That’s Lynda.com/c101.

Robert Ballecer: Welcome to Coding 101. It’s the Twit show where we let you into the wonderful world of the code monkey. I’m father Robert Ballecer.

Shannon: And I’m Shannon Morse and for the next 30 minutes we’re going to get you all learned up on everything you need to know to be an awesome code warrior.

Fr. Robert: Absolutely, code warriors are the place to be. But before we get there you know what I want?

Shannon: What do you want?

Fr. Robert: I want some “Snubs compiled”.

Shannon: Oh do you now?

Fr. Robert: I do, I’m hankering for some.

Shannon: Well I guess it’s a good thing I did some Snubs compiling then. I have a great viewer example from our Google Plus community today. So if you remember last week we showed you a great example from Lou our awesome code warrior and it was super, super fun. He basically asked us to do some changes and make the view model of it a little bit different. So you could add a background or do whatever you want. Joe over in our Google Plus community posted not only a great description of how to get this working on your computer but he also sent us a perfect example of his code. Let me go ahead and open this up. Just to let you know he just sent over a couple of different files, just the main window and that’s pretty much all he changed. So what I did is I took Lou’s copy and I just copied and pasted it over to this page and I just didn’t change anything else. If I go ahead and click on the project file and open that… Anytime you want to open a project file from anybody else just hit the cs project file down here and it’ll open up for you. Click ok and it’s just warning you, don’t open up any code from anyone you’re not aware of.

Fr. Robert: When it’s our computer we’re going to do what we want.

Shannon: That’s right, we are. We have the regular code throughout here and then down here is the main window so this is what he changed. I’m going to go ahead and run it for you so you can actually see his changes compared to what Lou showed us last week. He changed this up at the top a little bit, he made the lines a little bit narrower so you can fit more information down here. When I hit retrieve he has everything centered over to the left side as opposed to just centered down the middle. When you click on one you’ll notice down at the bottom you also have these really cool options.

Fr. Robert: Oh ok, so he’s added the play controls! I love it.

Shannon: Yes, he put in a stop, a play and a pause. There’s also a nice little scroll button here. This doesn’t work though and Joe said in the Google Plus community, if you can change this you go right ahead because right now it doesn’t scroll anywhere, it just continues back to the top as normal. All the other ones work perfectly, you can play and pause and stop and it’ll bring you right back to the beginning. Really cool example and I love what he did with it.

Fr. Robert: It’s a perfect example of why we’re building it this way. If we were just writing this as a structured program – get rid of all the… and everything is one big chunk of code, changing the interface and going through all the code to figure out…

Shannon: It would be really hard.

 Fr. Robert: But with this we know because the view model is in the back and the model is in the middle and the view is in the front and all we have to do is change that first layer and voila it looks like a completely different program.

Shannon: I just wanted to show off his example this week and of course if you guys have examples or if you’re kind of stuck on your own code you can always join us over in the Google Plus community. It’s bit.ly/twitcoding101.

Fr. Robert: That’s fantastic. You know what? With all these codes, all the software that’s coming into us from our audience…and Joe thank you very much for sending us the examples, that great. I think that sometimes people need a place where maybe they could present their work. Not just in our google plus group but out in the real world of people who don’t always code. I just wish there was an easy way to do that.

Shannon: I think I have a place for you Padre.

Fr. Robert: What’s that?

Shannon: Squarespace.com, it is the all in one place to get everything that you need to make an excellent website whether you need a portfolio or you need a business account or you just want to show off a wedding website like I did you can use squarespace.com. This episode is brought to you by squarespace.com. They’re the all in one platform; they make it easy to create your own business, professional website or your online portfolio. They are constantly improving their platform, they have new features, new designs and even better support and they have 25 beautiful templates for you to start out with. They recently added a logo creator tool. This is pretty cool. It’s a basic tool for individuals and small businesses with limited resources to create a really simply identity for themselves. So you can go in there and create your own logo. It’s so cool and super great if you don’t know photo shop like me. It’s also very easy to use but if you do want some help square space is always available. They have a live chat, they have email support 24 hours a day, 7 days a week. Plus there is a complete re-design customer help site so you get really easy access to help, self-help articles and tons of video work-shops as well and they have the availability for e-commerce. So say you want to sell your stuff online, you can do that through Square Space as well. They’re now available for all subscription plan levels including the ability to accept donations too which is really great for non-profits or if you have a wedding website and you want to set up a cash wedding registries or school fund drives. So pretty much anything that you want. Padre you could use it.

Fr. Robert: Yay!

Shannon: And they’re inexpensive. It just starts at $8 a month and they include a free domain name if you sign up for a year. That’s what I did and I think it’s the best choice. It’s a really good price and its excellent, they’re mobile ready – I can’t forget that…the new square space metric app for iPhone and iPad allows you to check site stats like page previews, unique visitors, social media followers so you can really figure out where all your viewership is coming from and you can fix it along the way too. So if you’re getting more viewers from Twitter you can always go over to Facebook and start to grow that business over there as well. With the blog app you can make text updates, you can tap and drag images to change layouts and you can monitor comments on the go. Even their code is gorgeous on the back end, so not just the templates but their code too. So if you are a coder you can really get down and dirty in there too. We all know that square space looks beautiful on the outside but what’s also amazing is that all the code is beautiful too. Square space takes just as much pride in their back end code as they do in their front end design and hosting is included. Yes, that’s right, hosting is included and that is one of the best parts. They take care of all the hosting so you don’t have to. Now this is pretty cool. As a special promotion for our Coding 101 audience…everybody listen up…square space is giving away a full year of its most premium level service. That’s valued at more than $288 to a randomly selected listener. All you have to do is Tweet “better websites for all” with the hashtag squarespacecoding to be considered. Again that is “better websites for all” with hashtag “squarespacecoding” to be considered. If you currently have a square space site too then post your site URL and we might talk about it in a future episode. Now start with a free 2 week trial with no credit card required and start building your website and when you decide to sign up for square space make sure you use the offer code “coding” to get 10% off and to show your support of coding 101. Of course we thank Square Space for their support of Coding 101 because remember it starts with your new square space website.

Fr. Robert: Do it, do it now. Alright now let’s go ahead and take a jaunt to the ivory tower.

Shannon: Let’s have some fun! I’m ready.

Fr. Robert: We’re going to talk about a little something that some people are going to think it’s taking a step back but it’s actually taking a step forward in what we know about C sharp. We’re going to talk about collections.

Shannon: I kind of remember collections from…was it python?

Fr. Robert: We were actually combining different data types and such. Now there is actually in the chat room they mentioned that some people thought collections were housekeeping routines. In early programming you actually had to make sure that you had routines that would clean up and use variables and procedures because you didn’t want that to keep sucking away memory.

Shannon: Well that makes sense.

Fr. Robert: Now that’s garbage collection, that’s completely different and I’m not going to say it’s completely solved in C sharp, it’s not automatic, but it’s mostly automatic. C sharp does a really good job and doing all that housecleaning and back up on its own although you do still have to program it correctly. We are talking about a different kind of collection. Collections of objects, collections of values.

Shannon: Oh I see.

Fr. Robert: When we are programming in C sharp or any object programming often times we want to create a group of different objects, methods, values and procedures. It’s what we talked about with classes, that’s the whole idea. You create the blueprint for something that combines methods and variables and data and code at the same time which can then be instantiated into individual objects.

Shannon: So would you say collection is kind of like a blue print?

Fr. Robert: It’s a blue print but it’s more than that because you’re specifically collecting certain things where as classes deal with code mostly. When we start talking about collections we’re collecting different data objects.

Shannon: Interesting.

Fr. Robert: It’s interesting but also its import to sort out in your mind when you’re doing object oriented programming because we want to think of things as objects. For example this object is our data on sales prices and this object is our data on customers and this object is our data on calendar and we want to be able to tie together some of the objects that even though they may not be the same information they’re kind of close together. You want those in the same general area so you make a collection.

Shannon: Oh I see, so it’s easier to find all of that information because it’s stored in that collection as opposed to being all over the place.

Fr. Robert: Yes and again it’s just the idea of reinforcing an organized hierarchy. Organization is good and its key in object oriented programming. If you’re not organized it means that the next person who comes around has no idea what you’ve done. So when we start talking about collections there is a built in collection for us. It’s an array. Arrays are actually a collection.

Shannon: I remember arrays!

Fr. Robert: We talked about it for pearl, we talked about it for python, and we never actually got around to it the first time we did C sharp.

Shannon: Well there’s so much to talk about.

Fr. Robert: There is so much to talk about. Go ahead and go to my screen if you would. I’m showing you what a super basic program that deals with arrays looks like. So this is the console window, so you pull it up and now I’ve got 2 different types of array declarations here. The first one in the little brackets – what that’s doing is its creating an array but this is what’s called a dynamic array.

Shannon: What’s a dynamic?

Fr. Robert: It doesn’t have a set length.

Shannon: Oh. So you can make it keep on going forever?

Fr. Robert: Well yes and no. It’s a really bad practice; you wouldn’t want it to go on forever. It would eventually suck up all the memory you had in your system. But in the old time when I first started programming it was very clear line delineated. Arrays are static, you set the size of your array and your array will always be that size. Which is actually good programming because when you’re programming and you’re making an array even if it’s going to be in collections you want to know how big it’s going to be and how much it could possibly encompass and you program for that value. What I can do here is I can go ahead and make it of an indeterminate size. Let’s say I’m going to have an array called Twit array of integers but I’m not specifying how big this thing is going to be. I don’t like doing it that way so I do it this way. Does this look familiar?

Shannon: Yes it does.

Fr. Robert: It’s like when I’m declaring a class right?

Shannon: So what you’re saying is that this array called c101array is going to have 10 different objects in it?

Fr. Robert: Exactly. This is now a defined array so it’s going to have 10 cells, 10 elements that I can use. I’m making sure that it’s an array of integers by doing this, called c101array and then this looks just like when I instantiate a class, because this is actually an object. An array should be considered an object. What is in the object? Inside the object is all the values that are contained within the elements of that array.

Shannon: So in this case you have 10 different values and the first one starts with zero. I remember that.

Fr. Robert: Yes. Let’s say I have an array that is 10 elements long right? But arrays start at zero so if you notice when I start assigning values to the elements of the array I go from 0-9 which comes out to 10. That’s all it is, this is a super simple program and if I run this it should just count from 1-10 on new lines and that’s what it does. Counts from 1-10 and is very easy to use and very simple. It explains 1 very important thing and that is what arrays do. If we look at the code all an array is doing is saying ok; make myself an array which means I’m going to have a single identifier. That identifier is going to be called c101array and then in that array I’m going to have 10 spaces, 10 elements, 10 cells, 10 things. In this particular I’ve told it it’s going to have 10 integers.

Shannon: Could that be strings or floats or whatever?

Fr. Robert: Right I could change it. Right here I’ve declared them to be int. So I’m telling it that integers are going to be here. This could also be string, this could be float or bowl or double. All those things can be here so I can make an array of pretty much anything.

Shannon: Cool!

Fr. Robert: The nice this is it now allows me to collect data in 1 neat spot. When we start collecting data I could have something like Shannon guest list and I could have an index that points to different guests in the guest list and each one will be a string. So it’s an array of strings, each with a name of that guest which means that each guest now can be searched by its index number.

Shannon: I got it.

Fr. Robert: That’s the simplest of all collections, it’s really easy to do and it built into C sharp. I would tell people go ahead and create an array and we’ll use what we know about 4 loops. I used one actually – go ahead and look in the code. There’s just one quick example of the 4 loops. It’s super simple. I just did an advance encounter from 0-9 because its less than 10 so it stops at 9 and each time it loops it goes ahead and increases the counter by 1 and then it just writes the name of the array which is the identifier and then the index is the counter. So it just increments from 0-9. Go ahead and see if you can write a function that fills the array and then one function that prints the array.

Shannon: Oh ok.

Fr. Robert: Easy to do right? Ok now let’s step a little bit away from arrays because arrays are great but arrays have one big limitation. Actually they have a couple limitations but the one I always focus on is they’re not great at storing objects of different type.

Shannon: That makes sense, because if you had one int and one string…

Fr. Robert: You can’t put them in the same string.

Shannon: You’d get confused.

Fr. Robert: It just wouldn’t work. Or you’d get some freaky… like for example if you had an array of strings and some of your strings had numbers and you tried to add the numbers together C sharp would not know what you wanted it to do. You can force it but you’d kind of have to hack around. Ultimately what you’d have to do is say this array has integers, this array has strings, this array has – whatever its going to be, and then you would collect them together. So if you’re going to be doing a lot of data and a lot of objects that have the same typed data arrays are great but if you want to collect data of disparate types of data you’re probably going to look at other types. Now let’s go ahead and take a look at some of the other collections. I’m going to have to re-write my console real quick here. This is probably going to mess up because I’m doing it live. So if you look I’ve just changed 1 little thing here. I’ve added system collections generic. This is an include that is not normally done in the console. C sharp actually has a set of classes that they have written and made available for you. You just add this line – that deal specifically with collecting data.

Shannon: This is just one of the many. I’m assuming there are tons of them.

Fr. Robert: Yes there’s many. Actually one of them is just using system collections and then there is systems collections generic. C sharp is c sharp so as you start saying “using system.collections it will tell you all the different ones you can use.

Shannon: I believe we linked to the site to where Microsoft lists all the different ones you can use as well.

Fr. Robert: Right, and again this is part of the fun of object oriented programming. This is just what Joe did at the beginning of the show which is why do I have to rewrite this. If someone’s already written a class that does this I’m just going to deal with…

Shannon: Just grab the class.

Fr. Robert: Right. So the .net framework includes built into its code base a bunch of classes that do collections. There are a couple of different collections that we use. We use arrays of course, we’re also going to use what are called lists. Now lists are like arrays, they are treated the same. It looks slightly different.

Shannon: It sounds the same.

Fr. Robert: It sounds like an array because it’s a list of stuff. An array is an array of stuff.

Shannon: I’m guessing that with lists you can use different types of values?

Fr. Robert: Well lists mainly vary from arrays in that they were designed from the beginning to be dynamic. They were designed to grow and shrink as you needed them to. Whereas arrays you can make them dynamic if you need to but that’s not how they were designed. There are work-a rounds to make it dynamic. So when we’re dealing with lists we’re talking about an automatically dynamically resized list of things and objects. It means that we don’t have to define at the beginning how big the data set is going to be, it would just grow or shrink to fit the data set that’s currently in there.

Shannon: That’s cool.

Fr. Robert: Yes it’s nice. The syntax is a little bit different. I don’t want to go through it because it looks pretty much the same as going with arrays. There are 2 others that are in the base of collections. The first one is called the dictionary and the second one is called the hash.

Shannon: Oh I’ve heard of hashes.

Fr. Robert: Well not as in hash browns. No I know… There are different ways of collecting different types of data. When I talk about a dictionary it’s a dictionary. It allows me to store values and then search for them based on the key words based on those values. Then when I store hashes it allows me to store values and search for them by the hashes. So it’s pretty self-explanatory. Going back to my screen I’m going to show you what a basic hash looks like. Again its super simple, this is like using a dictionary. I’ve got several things stored in here for dictionary. Oh wait, I saved the wrong one. This one is actually broken. I didn’t fix it. That’s ok because this actually still does show the syntax. You’ll notice that the code for this looks a lot like the code I did for an index. I go ahead and instantiate the object, the dictionary, I declare it and then start filling it. Instead of just filling it with numbers like I did with the array of integers in this particular case I’m filling it with a key word and then a value. I can search for it via that key word.

Shannon: Oh!

Fr. Robert: When we start talking about collections what we’re really talking about is how you make the data that is available to your program searchable, organizable and usable by the further parts of the program.

Shannon: Sounds like I would want to use lists!

Fr. Robert: Actually here’s the problem. Programmers tend to get caught up with the stuff they do best. So if you learn arrays by golly you’re going to make everything look like an array. If you learn lists better than anything else then everything becomes a list. You really want to go with the – because there are subtle differences.

Shannon: There are best case scenarios.

Fr. Robert: Right. For example, can I search for the key word of a bunch of stuff in an array? Yes I can, it’s kind of a kludge. I have to go ahead and write something that’s going to read each – of the array and then compare it and then say ok I want this one or I go to the next one. Or I could use a dictionary which is set up to do key word reading.

Shannon: So a dictionary would be the best option for that.

Fr. Robert: Yes. There’s always a best case for which collection you want to use for any particular program you are writing.

Shannon: Ok.

Fr. Robert: Does that make sense?

Shannon: Yes, I can get on board with that.

Fr. Robert: So that’s all the theory but what we have to do is bring in our code warrior and what our code warrior is going to do is show you the code that goes behind taking this idea of collections and actually turns it into something useful.

Shannon: But before we go is there another place I can go somewhere where I can learn more about this?

Fr. Robert: I’m glad you mentioned that because you know Coding 101 is just the surface. If fact we were talking about this before right? You were saying how you’d received an email from people saying why don’t you do it this way, why don’t you do it that way? We know there are so many different ways you can go with coding but all we’re trying to do is get people interested so they can seek out that knowledge on their own. But there has to be a place where they can seek out that knowledge.

Shannon: Where’s that place Padre?

Fr. Robert: I don’t know maybe a place called Lynda.com. That’s right, Lynda.com is your one stop shop repository for knowledge on the internet. Do you want to learn new business skills? Do you want to learn how to communicate, do you want to learn how to program? Do you want to learn how to edit video or photos or put together your latest quad-copter project? Lynda.com has it all. That’s what makes them the repository for information on the internet. They are an easy and affordable way to help you learn. You can instantly stream thousands of sources created by experts on software, web development graphic design and more. Lynda.com works directly with industry experts and software companies to provide timely training often on the same day that new versions of those software packages are released which means if you want to stay current in your skill set Lynda.com is the perfect companion. All courses are produced at the highest quality. These are unlike those homemade videos on YouTube which again I love, that’s where I come from. I started out making home videos but sometimes when you want to learn you don’t want to be distracted by scratchy audio or just lighting that is poor. You want to go someplace where production values actually add to the learning and not detract. Tools include searchable transcripts so that you can find specific problem solving portions of the videos which is great because sometimes you just need a reference and Lynda.com lets you do that. They also allow you to use their software whether you are a beginner or advanced for experience. That’s the nice thing about Lynda.com. You choose where you are and what you have to learn and Lynda.com will find it for you. You can also watch while you’re on the go with Lynda.com apps for iPhone, iPad and Android. They have 1 low monthly price of $25 which gives you unlimited access to over 100,000 video tutorials. Premium members with an annual plan can download courses for their iPhones or iPads or Android devices and watch them offline. This is the perfect way to use Lynda.com as a repository of data because sometimes you’re not connected to the internet but you need the answer. That’s when Lynda.com comes into play. Now Lynda.com also has courses on up and running with sent os Linux practical cyber security the amazon web service, does essential training and “Up and running with symphony 2 for php”. In other words they’re always adding those topics and subjects that are hot. If you want to learn about cyber security it is there. If you want to learn about symphony 2 it’s there. If you want to be up and running with sent os it’s there. For any software that you rely on Lynda.com helps you stay current with their updates and learn the ins and outs to be more efficient and productive. So here’s what we want you to do. We want you to try Lynda.com free for 7 days. Visit Lynda.com/c101 to try Lynda.com free. That is Lynda.com/c101 and we thank Lynda for their support of Coding 101. I think we’ve – what is that…run around the tree?

Shannon: I think we’re ready.

Fr. Robert: Yes we are.

Shannon: I think we’re ready for the code warrior.

Fr. Robert: We are ready for the code warrior. Ladies and gentlemen, as always our master of all things code. It’s Lou Maresca, senior software developer at Microsoft. It’s so good to see you Lou.

Lou Maresca: It’s good to see you guys too.

Fr. Robert: Now tell me, we’ve been messing around with collections a little bit. It’s one of these things that I think it’s almost like learning object oriented programming the first time.

Shannon: It seems a little bit easier.

Fr. Robert: It seems easy but most people are like Shannon who are going to say well why am I going to use that dictionary? Dictionary is so stupid. I’m just going to use an array of words.

Shannon: Show me why I want to use one or the other.

Lou: I think one of the important things is C sharp or any of these other give you data structures like arrays and you can do things easily. I think Padre showed arrays and lists. List is actually a little bit more complex of a type because it’s what we call a generic type in C sharp and we haven’t really gotten into generics. I think in C plus plus they call it templating and I think in even Java they call it generics too. It’s a little bit more complex. One of the one I want to try and talk about is an array list. If you’ve never seen an array list before it’s basically what an array is but its dynamic and lets you kind of expand that as you want to add things. Just as you showed in your code I can kind of show in my code real quick. This is the social network app that we built a couple episodes ago. What I did instead was I added an extra little spot here where we can display everyone’s tweets from my home stream. I bound that to a button click. When I click the refresh button it goes and gets the tweets. It’s going to get the tweets from my special twitter class that I created. I created another method in here called Home Tweets. This is again just using a link to twitter library to get the tweet but really what we want to look at down here is what I’m returning. This is just the new array list. What an array list is, is saying give me everything that I’ve got returned as the results of those tweets and just stick it inside that array list. An array list lets me store pretty much any type. It’s kind of a strange data type because it lets me store pretty much anything. It could be ints or objects or in this case it’s status from a tweet. Sometimes people say do you really want to use that because that can cause the memory to kind of grow and dynamically you don’t know how big it’s going to be at any point in time. The nice thing about an array is you see I have 30 items in that array and I know it’s going to be an integer and so I know exactly how large that’s going to be.

Fr. Robert: Lou that’s actually one of the things I think a lot of programmers who have been doing this for a while will remember which is; we used arrays because they used a fixed amount of memory. No matter what you put in it you declared that memory at the very beginning so it took its chunk and it could never grow. It always confuses people when we move from that to a dynamic way of think about allocation of memory or allocation of resources. But that’s what we can do with lists right?

Lou: Exactly. Some people don’t like using lists because you never really know how big it can be because you can store anything you want in there at any point in time. There is another type that’s called a generic list. Generics are a little bit more of a complex concept but basically what that is; is if I say I want a list then I have these special little carrots that I can put in here and I can say I definitely want the type to be string. So I can put whatever I want in a string. Now I’ve got this dynamic list of strings only. Now the list, and it has a default size underneath the coverage. You can actually specify your own size just like in arrays. So if I wanted the size of 20 now it’s a default list of strings at size 20. The run time knows exactly the size that it needs to be when it runs. Then when I start adding things it will again dynamically grow.

Fr. Robert: We’ve got a question from the chat room. Isn’t there a danger of overflow when we start playing with dynamic elements? If you’ve got a list that could expand forever filled with who knows what that could be a security hold right? So what do we do to make sure that doesn’t happen?

Lou: When you put something into a list you have to make sure that… The structure handles that itself so sometimes it’ll say I have a max, I can’t add anymore, I’ve run out of memory. There are exceptions that get thrown when you hit that point but also you want to check too. For instance in my case I’ve got to hold tweets back to make a large list of tweets and I’m just shoving them into the array. I probably should specify how many are in there in the count and then I can go add. So I’m basically sanitizing before I actually add to that list.

Fr. Robert: That makes me think of a bug that was discovered in Android not too long ago where the comment that people were able to read in the code was; I made this variable x length because I’m pretty sure it will never need more space than that. Which if I’m a hacker we end up going ok make it 1 byte longer and I’m overflowing. That’s nice. But yes you’re right, the tool, the compiling, the language actually does a really good job of making sure that doesn’t happen but just because you’re using a dynamically generated object doesn’t mean that you can completely do away with thinking how much memory a particular process or object is going to take.

Lou: There is an even more fundamental program that some data sharks use that I like. They call it a link list and a lot of people in the chat room are talking about it. A lot of these data structures that C sharp uses are basically a link list under the covers and really all a link list is is a node that connects to each other and then you can kind of reiterate through each node as you go along and connect them even more with more and more items as time goes on. The list is just a dynamic version where you can add an integer or string and you can keep adding and adding. It allows you to not only go to an item that you want using an index kind of like an array but also you can go through each one.

Fr. Robert: Lou here’s another question people are going to ask. Again I know arrays really well so I want to stick with them. Why can’t I just make an array that contains other arrays and one that has my integers and one that has my strings, one that has my values? Why wouldn’t I just do it that way? Why would I have to use the lists in order to get all of the benefits of collections?

Lou: You could, that’s one way of doing it. I think the whole key to lists is doing a lot of that work for you. Like if you wanted to have an array that was also a dynamic you would also then every time you wanted to add an item to that array you’d have to go create a new array that was bigger and then copy everything within the first and shove it into the 2nd one.

Shannon: That sounds annoying.

Fr. Robert: It’s annoying and it always leads to errors because there’s always problems. When I was programming in visual base that was the number 1 cause of errors. When people tried to copy something from 1 array to another. Something always got screwed up.

Shannon: Every single time.

Fr. Robert: It’s better if they just grow.

Lou: Another thing too is that there’s a lot of other data structures that we can go into for a long time. Collections library has 100 different collection types but there are some really unique ones like for instance I think another one we should probably talk about is the one called a hash set. A hash set basically like in cryptography a hash set is just like a generated version or string version of something; It’s like a string version of an object basically. It’s unique sometimes, most of the time. That hash could be put into a collection so for instance if I wanted to shove a string into a collection, let’s say a bunch of user names and I never wanted it to have duplicates I could use a hash set because then that guarantees that that hash is unique for every user name and I can store a whole bunch of large sets of user IDs in that hash set and there’ll never be a duplicate one. So that’s another collection.

Shannon: That sounds very familiar to some of the things I’ve learned from security.

Fr. Robert: Yes. They might go hand in hand. That’s so strange.

Shannon: Gee, I wonder why?

Fr. Robert: Now we’ve talk a little bit about it, we need to see it in action. You’ve actually got an example that could show us how you’re doing collections.

Lou: I basically wrote a query to allow me to get a list of tweets from any specific user and then what that does is the actual link to twitter API will return the minimum of 20 at that time and that returns it as a list. In this case I’m just converting it to an array list to kind of show you what an array list can do. Basically it’s just adding up an array list. It’s adding the result in there and then over on my window here I’m actually going to do a 4 loop through every one of those in that array list. There are 2 ways to do it and I don’t’ think we’ve ever gone over a 4 each loop but there are while loops and 4 each loops but this is a 4 loop and so I just basically say in that array list how many are there and I’m going to start from 0 and go up to that. For every one of those you increment by one each time. Then every time I hit one I’m going to go add it to my collection. Another type of collection that C sharp offers and that will basically notify my UI that I have a new tweet.

Shannon: I just got very excited because I understood.

Lou: - Basically collections that every time you add an item to it, it will always notify the ui what is happening and then the ui will go and update itself. Now I have the ui here and if I click down at the bottom to actually refresh…notice now I get all the tweets from my home screen. I get it immediately updated pretty quickly.

Fr. Robert: You loaded all of that onto a list in memory?

Lou: Yes right so this is all in memory right now and these are all the people that I follow. There are other ways to do this again. There are 1001 ways to do this. I could have done a generic list. Another example is what we call a dictionary, so if I just want to be able to get a user’s tweet by the user ID for instance I can create a user dictionary here that basically allows me to add each one of those based off of the user name. Now what I can do is say if Padre was in this I could say user ID “PadreSJ” and I could basically get the tweet for PadreSJ. It would return the tweet for that. So dictionary allows me to kind of look things up based on key values.

Shannon: So it’s basically going to be searching for PadreSJ in each of the strings?

Lou: That’s right. The cool thing about a dictionary is again it’s a data structure that’s complied of basically a half hash underneath the cover because the key is basically values. So Padre’s unique user ID that nobody else has on Twitter. I know that I can add that to the dictionary as a hash value and that could be the key. Then the value is the actual tweet. Then I can go and really quickly find Padre and his tweet using the dictionary.

Fr. Robert: But those are linked together so if you find the key you’re going to find the string that’s attached to the key.

Shannon: That makes sense.

Lou: There are a lot of cool collections that you can use. This code is neat because you can basically use a dictionary or let’s say you wanted to have a little box for a user that just displayed Padre3 or you just displayed as my tweets or something. You could add that to your ui and then you can bind it to something like this that will give me all the tweets that Padre has and that kind of thing.

Fr. Robert: If you haven’t already please get the ui, the API code from Twitter so that you can play with this. This is actually a fantastic piece of code and look how small it is because again he’s using a class that’s doing the work and all the grunt work in the back ground. You can scrape off your favorite tweets, you could scrape off the favorite parts of twitter and you can get similar keys and APIs for Facebook and for Google plus which means if you know how to store things into a list or into a dictionary or into whatever collection that makes the most sense, you now have a collection of usable data. Something that actually makes sense to you so that you can use it.

Lou: The recommended approach is always use generics because like you said it allows you to specify what you’re putting in there – the string and the size and it also allows you to dynamically grow that thing over time so that’s kind of the 2.0 way of doing C sharp. So dictionaries, hash set’s and lent lists and observable collections – these are all different types of generic collections that C sharp offers.

Fr. Robert: It’s a good program. Because if you know that your data set is indeterminable an array is a really bad place to store it because you have no idea whether or not you’re going to overflow that array. Either you’re going to be wasting a lot of space or you’re going to reach that point where hopefully your error will say I can’t fit any more or you get an overflow situation. So if you use one of these generic collections that are designed for this it’s just going to work out a lot better.

Shannon: I bet this is another one of those things that people have already wrote the code for?

Fr. Robert: Yes, we like that. That always makes us feel better. Lou we’re going to make these examples available for all of our users and again thank you very much. But before we end you wanted a little time to talk about something that’s – well a little xamerish. Is that a word anymore?

Lou: That’s right. So there’s a really cool company call Xamerin and they offer this new studio that allows you to download and it works on Mac and works on – they’re trying to get it on Linux too but it works on Windows and Mac right now and OS X. It allows you to build a mobile app using C sharp and then the ui components of Android and iOS and Windows and basically build that app at once and then compile it thereafter. Kind of the cool thing is it’s an extension on top of visual studios, you can do visual studio or you can go and use their studio itself. You can build a project just like we’ve been building with the social app. In fact the social app that I’m going to give you can be converted to a Xamerin project. Because it uses what they call portable class libraries which means it uses only libraries that Xamerin offers and then you can basically move it across the platform. It’s kind of cool stuff. One of the things I want you to check out is go to xamerin.com, download the studio, and load up the social project. You can actually load up the C sharp project in there and then you can try to compile it in there and it’ll use a different compiler and then it’ll ask you what you want to do with it. Do you want to create an android version of this? So you can create an android version of it, you can create iOS version of it. Now in order to edit the iOS version of it you can actually do it within the studio but then you have to load Xamerin studio inside of OSLX in order to compile it and everything. Again it’s a very unique cool way of getting into the other platforms quickly. I was going to show you a demo but I couldn’t get the Xamerin studio license to work. You can get a shared version as a 30 day trial. The free version allows you to use it indefinitely but if you want to compile another platform you’ve got to go and purchase it. The really cool thing is just for testing they allow you to use it. So we have a full license here though.

Fr. Robert: Well if anyone from Xamerin is listening hey Lou wants to show off your software! Hook a code warrior up!

Lou: I do know that the project I’ll give you if you do load it up in there it’s really fast. You load it and boom it’ll immediately tell you yes it’s portable, the UIs are included with the Xamel file that I gave you and then we can go and compile it and it’ll run as a windows phone app and then as an android app.

Fr. Robert: Ladies and Gentlemen Lou Maresca our code warrior. He’s the guy that we go to with questions about C sharp. Senior software developer over at Microsoft and just an all-around good guy. Lou thanks again for being on the show. I love having you on any Twit show. Can you tell the folks where they can find you?

Lou: It’s a pleasure. At twitter.com @LouMM and aboutme.loumm.

Fr. Robert: Fantastic! We will see you next time sir and until then we salute you.

Shannon: That’s a terrible salute.

Fr. Robert: I know, you have to stab it, right.

Shannon: Yes it has to be in the right place.

Fr. Robert: You actually know how to do it. You’ve got military training. I don’t, I’ve just seen it from the cartoon. In any case we know that this was a lot of information.

Shannon: But it’s good information and it’s not as hard I think.

Fr. Robert: Yes this was nice and easy. We’re trying to make sure that you can actually get the lessons. If you want to find any of the information that we gave you, if you want to find the show notes you have to go to our show page. Just go to…what is it Shannon?

Shannon: Twit.tv/code

Fr. Robert: Twit.tv/code and you’ll be able to get not only all of our show notes but also all of our episodes in one single place. It’s because we love you. Also you can find us on iTunes. If you are an iTunes user go ahead and subscribe to the podcast and make sure that you get it each and every single week. Because if you miss a week it gets kind of hard to catch up.

Shannon: But if you’re more of a google fan girl or fan boy like I am you can always go over to YouTube.com/twitcoding101 and subscribe over there. Please do, we really appreciate it. You can also join our google plus community. Again that’s bit.ly/twitcoding101. That’s where you can ask and answer all the questions.

Fr. Robert: All the things.

Shannon: Really good ones too.

Fr. Robert: Yes, the nice thing about that community is that it’s filled with people at different levels of programming.

Shannon: Yes absolutely and no one is going to make fun of you if you ask a question that you think is stupid because no question is stupid.

Fr. Robert: We may make fun of you but it will be in a fun way. Ok I’ll stop. No we won’t make fun of you. Also if you don’t like google plus you can always join us on the twitters. You can find me @padresj. That’s twitter.com/padresj.

Shannon: I’m Snubs on twitter. That’s it.

Fr. Robert: Twitter fail, I just twitter failed. Don’t forget that we do this show live every Thursday at 1:30 PM pacific time. You can find us live at live.twit.tv. If you watch you can see the preshow, all the foibles, the mess ups during the show and also the post show. It’s actually kind of fun. When Leo built this enterprise that we now call Twit he has this idea of making it the “look at how the donuts are made” and you can definitely see our donuts.

Shannon: Maybe yours. You can also join us in the chatroom. Its irc.twit.tv and we take questions from you guys during the show so if you have any questions like does your Coca-Cola say Shannon – no it doesn’t Janet Sku, thank you for asking.

Fr. Robert: Until next time, I’m father Robert Ballecer.

Shannon: I’m Shannon Morse. 

Together: End of line!

All Transcripts posts