Returning various data types in a single function?

A

Andy B.

I have a situation where I need to return different types of data from a
function depending on what happened with code execution. I will give a
little example of what I need to do. I use the entity framework for most of
my data access but I wouldn't think that makes a difference on return types.
I will use this example on the method AddNewsArticle.

1. The function starts to run.
2. Chheck to see that all parameters are not nothing. If they are trap
argumentException.Message and return it. Otherwise continue.
3. Create the NewsArticle object and attempt to add it to the database. If
anything fails, return the sqlException.Message. Otherwise continue.
4. If the Create NewsArticle and add it to the database succeeds, return the
number (Integer) from ObjectContext.SaveChanges().

Of course logging would be involved as well but for now I at least need to
let the user know why things happened/didn't happen. Is there a way to do
this without turning explicit/option strict off and not always return
strings (convert all return data types to a string)?
 
C

Cor Ligthert[MVP]

Andy,

As you see how that is done by the dataadapter.fill, then you see that the
object (can be overloaded a dataset, a datatable of a datarow)

This sub fills the given object with data.
You can make from this a function with a return type.
You are surely not the only one who does it then in that way.

(That is exactly why the name is fill, to distinct it from a kind of get)

Cor
 
A

Armin Zingler

Andy said:
I have a situation where I need to return different types of data
from a function depending on what happened with code execution. I
will give a little example of what I need to do. I use the entity
framework

I read it the 2nd time here but I still don't know what the "entity
framework" is.
for most of my data access but I wouldn't think that makes
a difference on return types. I will use this example on the method
AddNewsArticle.
1. The function starts to run.
2. Chheck to see that all parameters are not nothing. If they are trap
argumentException.Message and return it. Otherwise continue.
3. Create the NewsArticle object and attempt to add it to the
database. If anything fails, return the sqlException.Message.
Otherwise continue.
4. If the Create NewsArticle and add it to the
database succeeds,
return the number (Integer) from ObjectContext.SaveChanges().

Of course logging would be involved as well but for now I at least
need to let the user know why things happened/didn't happen. Is there
a way to do this without turning explicit/option strict off and not
always return strings (convert all return data types to a string)?

As you know, a function has exactly one return type. As I wouldn't declare
it As Object, there are (at least)
two possible solutions:

- If you don't do anything else but handle the sqlException, you can leave
exception handling up to the calling function which will then even be able
to get the full exception object, not only the message.

- Make the function return value a Boolean. True if successful, False
otherwise. Return the exception and the Integer values via ByRef parameters.
So, depending on the function value, the caller can pick the right returned
value.


Armin
 
F

Family Tree Mike

Andy B. said:
I have a situation where I need to return different types of data from a
function depending on what happened with code execution. I will give a
little example of what I need to do. I use the entity framework for most of
my data access but I wouldn't think that makes a difference on return
types. I will use this example on the method AddNewsArticle.

1. The function starts to run.
2. Chheck to see that all parameters are not nothing. If they are trap
argumentException.Message and return it. Otherwise continue.
3. Create the NewsArticle object and attempt to add it to the database. If
anything fails, return the sqlException.Message. Otherwise continue.
4. If the Create NewsArticle and add it to the database succeeds, return
the number (Integer) from ObjectContext.SaveChanges().

Of course logging would be involved as well but for now I at least need to
let the user know why things happened/didn't happen. Is there a way to do
this without turning explicit/option strict off and not always return
strings (convert all return data types to a string)?


It still sounds to me like you have one return type (int), and two
conditions where an exception would be thrown by your function.
 
A

Andy B.

"- Make the function return value a Boolean. True if successful, False
otherwise. Return the exception and the Integer values via ByRef parameters.
So, depending on the function value, the caller can pick the right returned
value."

Do you have a simple example of how this would work? sounds like something I
would look into. BTW, if you want to know what entity framework is, go here:
http://msdn.microsoft.com/en-us/library/bb399572.aspx



Armin
 
A

Armin Zingler

Andy said:
"- Make the function return value a Boolean. True if successful, False
otherwise. Return the exception and the Integer values via ByRef
parameters. So, depending on the function value, the caller can pick
the right returned value."

Do you have a simple example of how this would work? sounds like
something I would look into.

function blah( _
byval in as whatever, _
byref exout as exception, _
byref out as integer) _
as boolean

try

out = work
return true
catch ex
exout = ex
return false
end try

end function


Though, as already said, if there is not more information than "ex", I'd not
even catch the exception. Otherwise, you can raise your own BlahException
carrying the actual exception as it's InnerException.

BTW, if you want to know what entity
framework is, go here:
http://msdn.microsoft.com/en-us/library/bb399572.aspx

Thx.


Armin
 
C

Cor Ligthert[MVP]

What has the entitiy framework to do with basic (not the lanugage)
programming?

I used the tableadapter because of the fact that everybody who starts with
the entitity framework has in my idea at least basic knowledge of adonet.

There is not much more basic in adonet then a dataadapter.

Cor
 
A

Andy B.

"function blah( _
ByVal in as whatever, _
ByRef exOut as exception, _
ByRef out as integer) _
as boolean

try

out = work
return true
catch ex
exOut = ex
return false
end try

end function"

Ok. Now that we have something like that done, how do you get out or exOut
out of there since it returns true/false?
 
C

Cor Ligthert[MVP]

\\\
private sub shower
dim a as object = 0
dim b = false
b as result myprocedure(a)
'it will show that is now false
'a = 1 and b = true
end sub
private function myprocedure(a) as boolean
a = 1
return true
end function
///
`
 
A

Andy B.

I still don't understand I guess. Here is a better example that is probably
easier for me to take in. I have a method that takes a string called Title.
I take this string and compare it with all of the Titles in the database. If
it does already exist, return true and make the Guid from the row available
to the calling code. If that Title doesn't already exist, return false. This
way when the method returns true I can get the guid I need. If it returns
false I know not to bother with getting a guid.
 
A

Armin Zingler

Andy said:
"function blah( _
ByVal in as whatever, _
ByRef exOut as exception, _
ByRef out as integer) _
as boolean

try

out = work
return true
catch ex
exOut = ex
return false
end try

end function"

Ok. Now that we have something like that done, how do you get out or
exOut out of there since it returns true/false?

out and exOut are declared ByRef.


Armin
 
M

Martin H.

Hello Andy,

if it not really required to get a Function like call, then you could
overload subs.

e.g.
Private Sub Foo (ByRef Bar As String)
'Do something
End Sub

Private Sub Foo (ByRef Bar As Object)
'Do something
End Sub

Or if you want a function to tell you if it was successful.

Private Function Foo (ByRef Bar As String) As Boolean
'Do something
End Function

Private Function Foo (ByRef Bar As Object) As Boolean
'Do something
End Function

Best Regards,

Martin
 
M

Martin H.

Hello Patrice,

A function can't decide to change its own return type (the best you could do
would be to use Generics).

Well, yes and no.

You are correct that the return type has to be fixed. However, you could
return an object which then returns the type you want to.


Best regards,

Martin
 
A

Armin Zingler

Martin said:
Hello Andy,

if it not really required to get a Function like call, then you could
overload subs.

e.g.
Private Sub Foo (ByRef Bar As String)
'Do something
End Sub

Private Sub Foo (ByRef Bar As Object)
'Do something
End Sub

Or if you want a function to tell you if it was successful.

Private Function Foo (ByRef Bar As String) As Boolean
'Do something
End Function

Private Function Foo (ByRef Bar As Object) As Boolean
'Do something
End Function


Well,,,, which function has to be called? This would have to be decided
inside the function - too late.


Armin
 
B

Branco Medeiros

Cor said:
\\\
private sub shower
dim a as object = 0
dim b = false
b as result myprocedure(a)
'it will show that is now false
'a = 1 and b = true
end sub
private function myprocedure(a) as boolean
    a = 1
    return true
end function
<snip>

Please could you explain what does the line "b as result myprocedure
(a)" does? Is it VB?

Regards,

Branco.
 
M

Martin H.

Hello Armin,
Well,,,, which function has to be called? This would have to be decided
inside the function - too late.

Why? The parameter is of a different type, so the type of the variable
in handed over to Foo defines.

Let's say the code is like this:

Private Sub Mei()
Dim TST As String = ""
Dim retVal As Boolean

retVal=Foo(TST)
End Sub

Private Sub SSP()
Dim TW As New Collection
Dim retVal As Boolean

retVal=Foo(TW)
End Sub


In Sub Mei, the variable TST is of type string, so it
would access "Private Function Foo (ByRef Bar As String) As Boolean".

In Sub SSP, the variable TW is a collection and therefore, the function
"Private Function Foo (ByRef Bar As Object) As Boolean" is being accessed.

Best regards,

Martin
 
F

Family Tree Mike

Andy B. said:
I still don't understand I guess. Here is a better example that is probably
easier for me to take in. I have a method that takes a string called Title.
I take this string and compare it with all of the Titles in the database.
If it does already exist, return true and make the Guid from the row
available to the calling code. If that Title doesn't already exist, return
false. This way when the method returns true I can get the guid I need. If
it returns false I know not to bother with getting a guid.


Like this?

Module Module1
Function Tester(ByVal s As String, ByRef g As Guid) As Boolean
If (s = "ABC") Then
g = Guid.NewGuid
Return True
End If

Return False
End Function
Sub Main()
Dim g As Guid

If (Tester("ABC", g)) Then
Console.Out.WriteLine("Test 1: " + g.ToString())
End If

If (Tester("DEF", g)) Then
Console.Out.WriteLine("Test 2: " + g.ToString())
End If

Console.Out.WriteLine("Done.")
Console.In.ReadLine()
End Sub
End Module
 
A

Armin Zingler

Martin said:
Hello Armin,


Why? The parameter is of a different type, so the type of the variable
in handed over to Foo defines.


Let's say the code is like this:

Private Sub Mei()
Dim TST As String = ""
Dim retVal As Boolean

retVal=Foo(TST)
End Sub

retVal=Foo(TST)
retVal=Foo(TST)

The first time, retVal is a String, the second time an Integer.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top