Here we will learn how we can return(get) multiple values or objects from a subroutine(I know that subroutine wont return any value) in VB.net code. This functionality is available in C# as IN and OUT parameters.
Introduction
You might have searched "How to return multiple objects in VB.Net" in google and you might have got a lot of Ir-relevant articles. But this is not such an article, this goes to the point. I am going to teach you how to return multiple values in VB.net.
I gave a lot of try (Searching), how to do the stuff but most of the search results turned out to be like using a single object and returning multiple values in it, but that I do not need. So I thought of writing an article here about the same. So at least it will be helpful for sum beginners.
As many of you might be thinking that it would be about a new keyword in .net then you are wrong. So lets continue and see what is it?
Objective
In this article we will learn how to return multiple objects/values in a VB.net subroutine.
Lets Dive In
What you guys are thinking? A new vb.net keyword? a lot of code? no nothing. We are just going to use a very commonly known thing that is
ByRef.
I am going to demonstrate this using a console application as console applications are always good for learning the basics of any programming language.
The console application just does some simple mathematical operations and gives the result.
The main function is like this.
Sub Main()
Console.WriteLine("Enter 2 Numbers")
Dim a As Integer = Console.ReadLine()
Dim b As Integer = Console.ReadLine()
Dim multi As Integer
Dim sum As Integer
Dim subs As Integer
oper(a, b, multi, sum, subs)
Console.WriteLine("Sum Is " & sum)
Console.WriteLine("Substracted Value Is " & subs)
Console.WriteLine("Multiplied Value Is " & multi)
Console.WriteLine()
Console.ReadKey()
End Sub
In the above code you can see that there I have marked a bit of code in red. it is just calling the subroutine for adding multiplying and subtracting 2 numbers. Normally we will pass 2 parameters to a function if we need to manipulate 2. But i am using more than 2 why?? the magic lies there...
So lets take a look at the function "Oper"
Sub oper(ByVal a As Integer, ByVal b As Integer, ByRef Multiplication As Integer, ByRef Sum As Integer, ByRef Substraction As Integer)
Sum = a + b
Multiplication = a * b
Substraction = a - b
End Sub
In code you can see that for other than the first 2 I have used
ByRef. This
ByRef does the magic. As the other three are
ByRef the value of those once changed will be reflected every where.
So if we call the subroutine we are getting 3 return values (Literally) with out using any object or list or array or any other collections. The same is applicable for object also.
Conclusion
Hope this article was helpful to you. For further clarifications just respond to this post. I will get back to you as soon as possible. Hope you enjoyed reading(and understanding) this.
Enjoy Coding
Reference
http://www.dotnetperls.com/outhttp://stackoverflow.com/questions/4358742/is-there-a-vb-net-equivalent-of-c-sharp-out-parameters