Learn Programming with VB.NET - 05 - By Value v. By Reference

Hussein Nasser · Beginner ·🏗️ Systems Design & Architecture ·10y ago

Key Takeaways

This video teaches programming concepts in VB.NET, focusing on the difference between by value and by reference, and how they apply to variables and objects in the language. It covers key concepts such as memory allocation, variable assignment, and object references.

Full Transcript

what's up yo this is a send also from my geometry and as you can see I'm coming up with a cold but I have to record this episode it's been a week already but excuse my voice you guys but I'll try my best maybe this episode will be shorter I have everything ready my pills got my heart drank let's see how it goes I hope I don't collapse in the middle of the video all right if I did call someone all right that's it so in the previous episode we injected our beads application with more functionality right want to go and check have a pizza system guys finally I'm gonna end world hunger yes we're recording alright it's taking a long time just because this is the first time things are not encashed yet writing system this is a crazy stuff the first time we open up or anything so still young god first time you open a program the the windows or the breathing system has to go and fetch it from the hard drive alright mine is it mrs. D so it's faster but the first time you do that there are a lot of dll's and find a reason dependent season I have to go look all over the places and once they are cache they go to faster memory location you know in the memory which can be retrieved faster than that they go cached they are not dead and the hard drive okay this is not our topic all right sorry all right so this is what we did we added buttons oh you know labels oh nice that's what we did right you'll see yeah it's disabled by default we know how to do that we just create new pizza add cheese I don't you yeah and then you say calculate my price and nice add more chicken portions more chicken I don't know who will add this much chicken portion to their pizza but I know I don't judge people right so please I would guess a lot of people ID along with cheese right so yeah yeah that's a 22 yeah they're all the work here by the way there's sizes for the pizzas we didn't do that no all right there so the crust and all these things that we hard-coded we didn't change any of those now coming to those what I want to talk about today is actually a very interesting topic so remember now we're gonna add chicken right but you keep clicking these chickens never ran out right you can just go to infinity just clicking nothing Finity actually so there's a number there teacher there's a maximum number but uh but yeah that's a problem right the pizza plays have a storage of limited chicken portions right so if you keep adding and adding and adding yes you have to have you have to write out eventually right so we were gonna introduce something called the inventory so the inventory is basically how much chicken you have how much meat you have how much onion imma cheese etc and then when you click you add the cheese pork into your new pizza you deduct one cheese portion from the inventory simple mathematics right we thought we have been told this since we were kids now we have to teach the computers to do it alright alright so this topic will open up some concept of programming language that we need to discuss okay first of all is the by value and by reference and variables in general so what all what I'm talking about and the second one is the conditions so you need to talk about conditions if statements right if the inventory is ran out same as told he'll P tells whoever making the base of the egg we ran out of cheese go bring some cheese I don't know so there's an if statement right so if the inventory of cheese is zero then you do that message else you keep detecting yeah you can this is I'm just speaking in a negative language right and this is exactly how you write code and vb.net that's all how I like BB it's actually very similar to the human language you read the code and you understand it this anyone can read the code and actually understand it well in c-sharp not so much Nazi jobs like more a machine language in my opinion Slyke very machine oriented you know there's curly brackets and all stuff alright let's go back so can I introduce first that by value and by reference what are these things so let's do that so when you go here let's go to the load right on my function you know I'm gonna do some experiments here let's make a space here another alright our code here so let's say I declare a variable of type integer okay other information integer and we said that variable to five all right what is happening when I execute that and save let's say message books all right so when I start we've done this guy's right so this is a form build so this code will execute when you load the form when you start your program load the form it's five right I said five to I my message box I show me the show me that value right now what's happening in the background all right so what's happening think of this as your think of this as your entire RAM all right that's your RAM I know let me show you more I'm here I don't know if I can so my RAM here and this machine is to hunt my god for your scent well 16gb I have 16 G but for this machine for this virtual machine I think I have four okay poor jeebies a lot right so your program takes me start with you maybe by its kilobytes megabytes who knows you know but let's say this is your program all right it's just for the sake of explanation right mother where's this okay orange this is your program okay this is your pizza bed it's a bit space is going it eats Wow mother of dragons whatever you guys know this is the ear program space okay I'm not gonna lie right now what I wanted actually I'm gonna I need to this is this is very good two hundred really well so what happens that you first very variable on the mess price or sorry today don't wait right all right so you what you did here in the form load you declared a variable I there are a lot of other stuff but I'm not gonna show it right but this is the stuff that we did shut up Alexa this is what do you get wood this is a faulty product just speaks by itself it stupid Alex alright nobody called you alright so what happens that when you declare a variable okay so alright so when you declare a variable there's a space reserved for you right in the memory for a new replication memory and for example this variables I and that's your location right so I is this value and then you put five in that so I becomes 5 right that is a very important lesson by the way terms of how things are working so this is what happens right so I has a value of 5 so what happen if I did this now dim J as integer and then I say J is equal to I what will happen is that the the program will dimension another variable of type J and then so that's the steps exactly right so it will dimension the J that's the first thing why diamonds should I mentor it will reserve a memory education exactly the same memory location I didn't speak about the sizes now for the memory occasion coming very soon it's in basically invites if you have guests so J has nothing this is this statement right we jump and we put the value of I remember from the right to the left all right so put the value of I in J so what happens so say what's value of a five foot five inside J so that's what happens some poor so but you know it is right this is an integer these are integers and integer have values by default so if I'm not mistaken integers are okay it's 32-bit guys I'm not gonna explain every single thing but one byte is eight bits row so one two three four one two three four right so eight bits so if the first bit is one that's the value of one alright second bit is one there's only one and zeros right so this is 2 this is 1 so think of this as that this is 1 because this is 2 to the power 0 2 to the power 1 2 to the power 3 2 to the power 4 to the power 5 to the and so on so 2 to the power 1 is 2 right so what do you think this is that's right because this is 2 to the power 2 2 to the power 1 because this is the location right one location the location number 0 location number one murder 2 and so on so this is 2 number 2 to power 1 plus 1 which is 2 to the power 0 and this will be 3 and so so you can guess that this is 2 to the power 0 1 2 3 4 5 6 7 to the power 7 and 2 to the power 7 is we need the scientific thing 2 to the power services I think 1 to 8 yeah so this is 1 to 8 the value of decimal of 1 to 8 that's just one bite right so one you can put the maximum number you can put in one bite is actually 2 5 5 you can you can look it up alright so if you sum all these values it will be 2 5 5 just take it for me you can do it do it now if you want don't be too fine enough so that's one part so you can put a new one bite alright in 8 bits all right you can put 255 but we're talking about 32 bits that's the integer did you have 32 a bit self so 8 byte so if it's 8 bar it takes 2 5 6 so if you come to 0 and 2 table or 32 is what let's do that so integers can carry up to to the power 32 which is this number let's copy it so if you put this here okay constant expression not representable and type why because let's make minus 1 hmm doesn't like it so this is a very large number and in the integer itself so let's make it so definitely it's not 32-bit I will just figure out right because you could not put this number constant expression not a representable anytime it's just very weird but but you get the idea right I don't think it's a 32-bit it's much much less so let's try - to the power 16 maybe no I don't think this is a very small number so they keep adding one until alright so it breaks at some point but I don't know which number breaks it really that's really weird I know it's not our topic so but there are limits right so obviously maybe it's 2 to the power 3 24 I don't remember really maybe this is the number it depends from a language to another language yeah I can I can't tell guys yeah we need to tell you to get another episode for this but there are maximum but it's obviously between dogs right so it's not 32 a bit it's it's much less because we will put 30 too bad we got deserve okay not our topic so we know that each this has a space but this is not our topic but our topic is the back by value and / verses by reference so what happens that when we said I is equal to J or J is equal I the value of I the value of I went into J right and then it was copied into J so J has its own location eyes as its own location so so by message books let's do I is equal to O there is equal to okay and all right so I this is this will just display the value of a and the value of J right don't try and make this short episode because I'm not really nothing yeah well alright so I recall it then and we said let's do a message I is equal 5 j is equal 5 right because they have the same value so what happens when I change J to 7 and then display that so what happens that I set the value of 5 to I I set the value of I to J and message about they are the same and then I change J so J is now 7 and I what will I be to remain 5 right because every single one of them now has its own location and when you change it you change the value of that so that's a very important thing so why did we talk about this because variables like integer long double strings works as a value variable so if you change if you assign 1 if you create a few dimension a new one it's always gonna reserve and you memory location for that particular variable all right however so if you change any value that particular location will change right fair enough makes sense right because every time you declare a variable you reserve a memory location capisce all right so what happens when that's our eyes on J's right so it's just commend those out all right hmm let's declare two pizzas here Pizza one as it's a acquiring your pizza and then I declared them pizza to as pizza but this pizza is recorded Pizza one right so whatever I declared two variables now no two objects that's call them Pizza one is a new pizza pizza - is pizza one right so I'm gonna say pizza one and meats portion 200 okay can I do that or I can do that because we said that this radon all right let's do let's just add a variable here that's for fun okay I'm gonna add a variable here just for fun we gotta call it test and then we're gonna remove just okay all right so what I want to do here is I'm gonna set the pizza one the test is equal to 100 okay and then I'm gonna say message box pizza one door test I'm gonna say pizza one equal the stock value let's just call it object or object one and then object to is equal to is to test what are you trying to reach I'm saying okay what are we trying to reach soon my children so very so no I'm not doing that obviously sick all right so what are we doing here all right and that's sort of default value for the test to be five right so by default whenever you declare a new object don't know whenever you declare a new object Pizza one you it will be five that test will be five if I change the pizza one test its hundred right so according to our theory here well those two are in separate location so if I change this than that they should be as different right let's see what happens booyah what is this they both have the same value that's insane I just changed that but I see the pizza one is hundred when I when I when I query the value of test and pizza one it's hundred which is I change it but when I cried the value of pizza - it's also 100 what happened why would I change Pizza one pizza two changed that is odd right this is called the boy reference approach so so let's draw a Picasso here all right so what happens here my friends are when we declared our first Pizza Pizza one there is let's call it p1 and then will you collect p2 nothing is created only let's think of it as just a point right so small point was created here and a small point was created but no memory was allocated for those so in said pizza one when we sit equal new pizza new is actually tells the program or application operating system please allocate some memory for me for this variable and set that memory location to this variable so that's what happened so what happened is that somewhere here get a piece somewhere here all right here for example we created these values and there are a lot of stuff right and what happens there by default it's five right that this is five but I there are other values correct and what happens is that p1 points to that location all right that's the first statement the second statement pizza - as Pizza equal pizza so when we did the smooth integers J has created its own location but p2 in this case with objects with classes it doesn't work this way because it's very expensive to create another location and points to it so what happens that s what points to the same location yes this is what happens so when you set pizza to equal to pizza one they their values not be copied they were just merely points to the same location so when p1 change that value 200 remember that's what we did right and then we queried p1 which is pointing to this location tells me hundred and then we query p2 and give me the value of this and it's also hundred because they both point to the same location and that's very important right so this is by reference and that's by value all right guys so we explained that I thought I'd have more time to explain the conditions and again now we learned that we are ready to set in the next episode I'm going to explain the inventory concept but bear this in mind when you're dealing with objects what when we deal with object and you assign objects to each other they point to the same location so be careful with that when you deal with native variables they simply create their own the kitchen the reason is that the pizzas of the class the compiler does not know the size of this class and obviously can't calculate it but it's very hard work to do that and imagine a view if you create new and you assign them they will be pointed to the same location okay but questions that what if I want P to to to point to another location it has its own pizza simple don't do that just simply say a call new pizza this way these guys will have different values let's prove that okay now we created pinzon it's 5:00 remember pizza - it's 5:00 they are the same but they are in different location because we created we explicitly told the or s to give me two different memory allocation for those each one so this guy we change peas the one but we don't repeat pizza - so we run that and object one is 100 object two is five all right guys that's it for me today I'm very sick can I see you on the next one all right you you stay awesome and I gotta see you the next one I discussed in inventory conditions all these nasty stuff right good I love it alright guys you stay awesome and goodbye

Original Description

Full OOP playlist https://www.youtube.com/watch?v=3s-bgPg7IWc&list=PLQnljOFTspQX-XZChZg-b06MJ27nAJQS9 Short episode today, we talk about the difference between by value and by reference which set us good for the next episode! Stay awesome -Hussein
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Hussein Nasser · Hussein Nasser · 8 of 60

1 Extending ArcObjects (IGeometry) - 01 - Getting Started
Extending ArcObjects (IGeometry) - 01 - Getting Started
Hussein Nasser
2 Extending ArcObjects  (IGeometry) - 02 - The Document, The Map and The Layers
Extending ArcObjects (IGeometry) - 02 - The Document, The Map and The Layers
Hussein Nasser
3 Channel Update - New Book, New Job, New Videos
Channel Update - New Book, New Job, New Videos
Hussein Nasser
4 Learn Programming with VB.NET - 01 - Getting Started
Learn Programming with VB.NET - 01 - Getting Started
Hussein Nasser
5 Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Learn Programming with VB.NET - 02 - Classes and Objects (Part 1)
Hussein Nasser
6 Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Learn Programming with VB.NET - 03 - Classes and Objects (Part 2)
Hussein Nasser
7 Learn Programming with VB.NET - 04 - User Interface
Learn Programming with VB.NET - 04 - User Interface
Hussein Nasser
Learn Programming with VB.NET - 05 - By Value v. By Reference
Learn Programming with VB.NET - 05 - By Value v. By Reference
Hussein Nasser
9 Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Learn Programming with VB.NET - 06 - Variable size, 32 bit vs 64 bit
Hussein Nasser
10 Learn Programming with VB.NET - 07 - Conditional Statements
Learn Programming with VB.NET - 07 - Conditional Statements
Hussein Nasser
11 Learn Programming with VB.NET - 08 - Inheritance
Learn Programming with VB.NET - 08 - Inheritance
Hussein Nasser
12 Learn Programming with VB.NET - 09 - Strategy Design Pattern
Learn Programming with VB.NET - 09 - Strategy Design Pattern
Hussein Nasser
13 Learn Programming with VB.NET - 10 -  How did I learn programming
Learn Programming with VB.NET - 10 - How did I learn programming
Hussein Nasser
14 IGeometry 2016 Retrospective - Channel Update
IGeometry 2016 Retrospective - Channel Update
Hussein Nasser
15 Javascript by Example - The Vook
Javascript by Example - The Vook
Hussein Nasser
16 Vlog - Keep your servers close and your database closer
Vlog - Keep your servers close and your database closer
Hussein Nasser
17 Vlog - Client/Server Programming Languages
Vlog - Client/Server Programming Languages
Hussein Nasser
18 Javascript By Example L1E01 - Getting Started
Javascript By Example L1E01 - Getting Started
Hussein Nasser
19 Persistent Connections (Pros and Cons)
Persistent Connections (Pros and Cons)
Hussein Nasser
20 Javascript By Example L1E02 - Building the Calculator Interface
Javascript By Example L1E02 - Building the Calculator Interface
Hussein Nasser
21 Happy new Year from IGeometry!
Happy new Year from IGeometry!
Hussein Nasser
22 Synchronous v. Asynchronous
Synchronous v. Asynchronous
Hussein Nasser
23 Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Javascript By Example L1E03 - Displaying the Digits on Calculator Screen
Hussein Nasser
24 Show Your Work. Blog, Vlog, Write, Create and Develop!
Show Your Work. Blog, Vlog, Write, Create and Develop!
Hussein Nasser
25 Relational Database Atomicity Explained By Example
Relational Database Atomicity Explained By Example
Hussein Nasser
26 Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Javascript By Example L1E04 - Operators, All Clear with Arrow Functions
Hussein Nasser
27 What Comes First, User Experience or Software Architecture?
What Comes First, User Experience or Software Architecture?
Hussein Nasser
28 Javascript By Example L1E05 -  Evaluate the Calculator Expressions with eval
Javascript By Example L1E05 - Evaluate the Calculator Expressions with eval
Hussein Nasser
29 Fastest Way to Learn Programming Language or Technology
Fastest Way to Learn Programming Language or Technology
Hussein Nasser
30 Javascript By Example L1E06 -  Fix Leading Zero Bug with Conditions
Javascript By Example L1E06 - Fix Leading Zero Bug with Conditions
Hussein Nasser
31 Stateful vs Stateless Applications (Explained by Example)
Stateful vs Stateless Applications (Explained by Example)
Hussein Nasser
32 Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Javascript By Example L1E07 - Running our Calculator on the Mobile Phone
Hussein Nasser
33 Advice for New Software Engineers and Developers
Advice for New Software Engineers and Developers
Hussein Nasser
34 Why JSON is so Popular?
Why JSON is so Popular?
Hussein Nasser
35 Building Scalable Software - SLA, HS, VS
Building Scalable Software - SLA, HS, VS
Hussein Nasser
36 Vlog (Istanbul) - Datacenter Proximity
Vlog (Istanbul) - Datacenter Proximity
Hussein Nasser
37 Should Software Engineers Learn Bleeding-Edge Technologies?
Should Software Engineers Learn Bleeding-Edge Technologies?
Hussein Nasser
38 Do Developers Build Bad User Interfaces/Experience?
Do Developers Build Bad User Interfaces/Experience?
Hussein Nasser
39 Learn By Doing.
Learn By Doing.
Hussein Nasser
40 I Wrote Bad Front-End Code That Broke Chrome
I Wrote Bad Front-End Code That Broke Chrome
Hussein Nasser
41 My Story
My Story
Hussein Nasser
42 Vlog - Horizontal vs Vertical Scaling
Vlog - Horizontal vs Vertical Scaling
Hussein Nasser
43 Can User Experience Help Build Better Rest API?
Can User Experience Help Build Better Rest API?
Hussein Nasser
44 Reverse engineering Instagram in flight mode
Reverse engineering Instagram in flight mode
Hussein Nasser
45 The Benefits of the 3-Tier Architecture (e.g. REST API)
The Benefits of the 3-Tier Architecture (e.g. REST API)
Hussein Nasser
46 Stateless v. Stateful Architecture (Podcast)
Stateless v. Stateful Architecture (Podcast)
Hussein Nasser
47 The evolution from virtual machines to containers
The evolution from virtual machines to containers
Hussein Nasser
48 Proxy vs. Reverse Proxy (Explained by Example)
Proxy vs. Reverse Proxy (Explained by Example)
Hussein Nasser
49 Canary Deployment (Explained by Example)
Canary Deployment (Explained by Example)
Hussein Nasser
50 No Excuses
No Excuses
Hussein Nasser
51 Synchronous vs Asynchronous Applications (Explained by Example)
Synchronous vs Asynchronous Applications (Explained by Example)
Hussein Nasser
52 What is an Asynchronous service?
What is an Asynchronous service?
Hussein Nasser
53 Difference between Client Polling vs Server Push in Notifications
Difference between Client Polling vs Server Push in Notifications
Hussein Nasser
54 Software vs. Hardware AdBlockers (Explained by Example)
Software vs. Hardware AdBlockers (Explained by Example)
Hussein Nasser
55 HTTP Caching with E-Tags -  (Explained by Example)
HTTP Caching with E-Tags - (Explained by Example)
Hussein Nasser
56 Simple Object Access Protocol Pros and Cons (Explained by Example)
Simple Object Access Protocol Pros and Cons (Explained by Example)
Hussein Nasser
57 Nodejs Express "Hello, World"
Nodejs Express "Hello, World"
Hussein Nasser
58 Reverse Engineering Instagram feed
Reverse Engineering Instagram feed
Hussein Nasser
59 Popup Modal Dialog with Javascript and HTML
Popup Modal Dialog with Javascript and HTML
Hussein Nasser
60 MIME and Media Type sniffing explained and the type of attacks it leads to
MIME and Media Type sniffing explained and the type of attacks it leads to
Hussein Nasser

This video teaches the basics of by value and by reference in VB.NET, including how variables and objects are assigned and referenced in memory. It also introduces inventory management and if statements as concepts. By the end of this lesson, learners will understand how to declare variables, assign values, and use objects in VB.NET.

Key Takeaways
  1. Declare a variable with a specific type
  2. Assign a value to a variable
  3. Assign the value of one variable to another variable using the assignment operator
  4. Create a new object using the 'new' keyword to allocate separate memory locations
  5. Assign objects to each other, resulting in both variables pointing to the same location
💡 Understanding the difference between by value and by reference is crucial in programming, as it affects how variables and objects are stored and referenced in memory.

Related Reads

📰
Modular Monoliths in ASP.NET Core: Microservice Boundaries Without the Distributed Pain
Learn how to implement modular monoliths in ASP.NET Core to achieve microservice boundaries without the complexity of distributed systems
Medium · Programming
📰
The Standoff Nobody Wins: Understanding Deadlock
Learn about deadlock, a situation where two or more processes are blocked, waiting for each other to release resources, and how to avoid it in programming
Medium · Programming
📰
Presentation: Practical Robustness: Going Beyond Memory Safety in Rust
Learn how to use Rust to build failure-proof systems by leveraging ownership, enums, and typestate patterns
InfoQ AI/ML
📰
Integration Digest for June 2026
Learn 4 rules to build an efficient MCP server and improve your system design skills
Dev.to · Stanislav Deviatov
Up next
The Enterprise Software Problem Nobody Talks About
Wholesale Investor
Watch →