Danimal
Danimal

Reputation: 23

Need Explanation of VB.Net Code

In the below example code from a tutorial, I understand how the majority of it works. There are a few things I do not understand, though. Please help me to understand the purpose of some of these characters and the purpose of them. I will explain below what I don't understand.

Module paramByref
Sub swap(ByRef x As Integer, ByRef y As Integer)
  Dim temp As Integer
  temp = x ' save the value of x 
  x = y    ' put y into x 
  y = temp 'put temp into y 
End Sub
Sub Main()
  ' local variable definition 
  Dim a As Integer = 100
  Dim b As Integer = 200
  Console.WriteLine("Before swap, value of a : {0}", a)
  Console.WriteLine("Before swap, value of b : {0}", b)
  ' calling a function to swap the values '
  swap(a, b)
  Console.WriteLine("After swap, value of a : {0}", a)
  Console.WriteLine("After swap, value of b : {0}", b)
  Console.ReadLine()
End Sub
End Module

What I don't understand is why the need for x, y, and temp. why not just keep declaration of a and b and swap the values?

Also, on the Console.WriteLine, I understand the {0} is an nth reference or index position, but the comma suggests to output something else. Is it saying to output the value of a in the 0 index? If so, there can only be one zero index, so why does the next line reference the value of the zero index? Or am I all wrong on this? Any explanation would be greatly appreciated...please dumb your answer down for this newbie.

Upvotes: 0

Views: 100

Answers (3)

Nico Schertler
Nico Schertler

Reputation: 32637

a and b are variables that are only visible in method Main(). They cannot be accessed from a different method. On the other hand, x, y, and temp are variables that are only visible in method swap(). More precisely, x and y are the method's parameters. The keyword ByRef means that the parameters are references to variables. I.e. when you call swap(a, b), x becomes a reference to variable a and y becomes a reference to variable b. You can work with references like with other variables. The difference is that when you change x in swap(), a in Main will change accordingly.

The Console.WriteLine() method takes a string parameter and an arbitrary number of additional parameters. The {0} is an index into the list of those additional parameters. E.g. Console.WriteLine("{0} {1} {2}", 100, 200, 300) would output 100 200 300. and Console.WriteLine("{2} {1} {0}", 100, 200, 300) would output 300 200 100.

Upvotes: 0

user4039065
user4039065

Reputation:

You need a temp to store the value from the first var so that you do not lose it when you overwrite it with the value from the second var. Once the second var's value has been transferred into the first var, the value stored in temp (from the original first var) can be transferred into the second var (overwriting the original value).

This is a common method when creating custom sorting routines that shuffle the values from various positions and ranks in an array around.

Upvotes: 0

Dan Drews
Dan Drews

Reputation: 1976

The reason for x, y, and temp is to demonstrate how to do swapping. Your question asks

why not just keep declaration of a and b and swap the values

In this situation where you aren't doing any incrementing, sure you could just afterward say b = 100 and a = 200 but if you were doing this say in a loop and constantly swapping them, if you were to do

a = b
b = a

you would have a=100, b=100 because you set the value of b = a which is 100, then set a as the same value so it is still 100. That's why you need the temp value.

With regards to the console.writeLine, you are absolutely correct that it is indexing the 0th index, so in the first example, it is a, however it can be reused in the next line, because it is a totally separate line of code. Each of those console.writeLines can exist independently, so the values are indexed on a line by line basis

Upvotes: 1

Related Questions