Wrox Programmer Forums
|
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2007, 04:05 PM
Authorized User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Function in VB

can a function return more than one value in VB also.
like
form load()
 x=sample()
end sub
public function sample()as integer
  dim i as integer
  dim str as string
  i=1
  str="VB"
  return i

  'how to return i and str both
end function

 
Old February 1st, 2007, 04:10 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You can't do this. Split it out into 2 different functions or you could retuan an array list that contains both values.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old February 3rd, 2007, 12:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When I find the need to return more than 1 value from a single function call, I approach the solution similar to the way .Net events pass arguments around (with the System.EventArgs class). I employ a return argument class. Just create a class with the items you wish to return and return an instance of the class.

I often use this technique for methods that need to return data but could also result in a failed attempt. Throwing exceptions is more expensive and you typically know what the expected errors/failures might be, so you can define that into the return argument class. Using a discrete class provides strong typing to your return data that you can't get from an object array.

I usually define a base return argument class for some basic return values:
- Success/Fail boolean
- an exception for thrown exceptions

Then I extend it to make it specific to the methods that will return it (similar to how the framework extends the EventArgs class to be specific to the event delegates that will pass it).

For certain return arg classes, I'll create and include an error code enumeration and even some data of the necessary type.

-Peter
 
Old February 4th, 2007, 11:04 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Add a parameter to your function for the second return value.
Set it in the body of your function.
Use it in the calling code.

Woody Z
http://www.learntoprogramnow.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
conver VB function to c# Hughesie78 C# 11 April 25th, 2008 05:08 AM
vb.net 2005 function wymiller .NET Framework 2.0 2 August 12th, 2007 12:23 PM
Need some help with a VB function crowsfan31 Beginning VB 6 1 June 19th, 2006 02:36 PM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
Function Iff in VB 6.0 daninoj Beginning VB 6 2 January 17th, 2005 01:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.