Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 564 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Return Multiple Values(or Objects) in VB.NET

Return Multiple Values(or Objects) in VB.NET

1 vote(s)
Rating: 5 out of 5
Article posted by Dotnetrajanikanth on 7/17/2012 | Views: 2232 | Category: ASP.NET | Level: Beginner | Points: 200 red flag

Advertisements

Advertisements
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.

Download


 Download source code for Return Multiple Values(or Objects) in VB.NET


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/out
http://stackoverflow.com/questions/4358742/is-there-a-vb-net-equivalent-of-c-sharp-out-parameters

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Latest Articles from Dotnetrajanikanth

About Rohith Gopi

Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Wednesday, April 18, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

This article describes how to save an image file into the database and show them in the GridView.

Here is the code for populating a DropDown based on selection of another drop down list in Details view control of ASP .NET , in this example i've added two DropDowns in DetailsView control using TemplateField and InsertItemTemplate The Second DropDown (ddlProducts) is getting populated based on Category selected in Category DropDown , For this i've used SelectedIndexChanged event and findControl method to find the control and sqldataSource is used as datasource for the dropdowns and DetailView

In this article we will look how we can use “Microsoft.VisualBasic” namespace to do single line and double line depreciation calculation.

In this article I am fetching all Gmail contacts in Gridview . Many time we required our mail id contact list. In article I am trying to do this . All contacts are fetched in Gridview with name, mailed, home no, phone no etc.

Here i have tried to discuss Master Page handling concept in details.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 12:17:49 PM