Returning various data types in a single function?

M

Martin H.

Hello Armin,

I just read the OP again.
OK. So if one function has to be able to return 1...n values,
I would make the return value either a class or a structure.

Public Structure MyReturnStructure
Public sRet As String
Public iRet As Integer
End Structure

Public Function Foo() As MyReturnStructure
Dim retVal As MyReturnStructure
'Do something

Return retVal
End Function

Best regards,

Martin
 
A

Andy B.

Fixed one of my problems. Just return a Guid of the row needed or an empty
guid if no row exists.
 
A

Andy B.

The following method is up for conversation on the multiple return values
subject. It is my final version of one of the multi data type return value
problems I have been having.


'*** Check to see if a NewsArticle exists with the given title.
public function ArticleExists(ByVal Title as String) as Guid

'*** get all of the NewsArticles with the given title. There should only be
one but get them all to be sure.
dim DuplicateArticles as ObjectQuery(Of NewsArticles = _
DBContext.NewsArticles.Where("it.Title = @Title", new
ObjectParameter("Title", Title))

'*** Count the results to see what needs to be done.
if(DuplicateArticles.Count() = 0) Then
'*** No duplicates are found so return an empty Guid.
dim EmptyID as Guid = Guid.NewGuid()
EmptyID = Guid.Empty
return EmptyID
else
'*** there was a matching title so return its PK
return DuplicateArticles.First().ID
End if
end function

'*** Later in some other code...
if(ArticleExists(TitleTextBox.Text) = Nothing) Then
'*** Do whatever if the match doesn't exist. Probably add the object for the
first time.
else
'*** Update the existing one since there was a match
end if

'*** ...
 
F

Family Tree Mike

Andy B. said:
The following method is up for conversation on the multiple return values
subject. It is my final version of one of the multi data type return value
problems I have been having.
....
'*** Count the results to see what needs to be done.
if(DuplicateArticles.Count() = 0) Then
'*** No duplicates are found so return an empty Guid.
dim EmptyID as Guid = Guid.NewGuid()
EmptyID = Guid.Empty
return EmptyID
else ....

This can be reduced to:

If (DuplicateArticles.Count() = 0) then
return Guid.Empty
else
 
H

harryandrewswbsyscouk

You need VS 2008 and SP1 to use this in VS

HTH

Michel

Michel,

I have VS2008 SP1 but what next? Is EF supposed to show up as a
"Visual Studio installed template" when you Create New Project? If so,
I don't see it showing up.

Hawb
 
M

Michel Posseth [MCP]

Hello
I have VS2008 SP1 but what next? Is EF supposed to show up as a
"Visual Studio installed template" when you Create New Project? If so,
I don't see it showing up.


Almost , it is a item template within a project , so project explore , right
click add item , New item , and select "Ado.Net entity data model"

Here is some reading material the first link contains tutorials

http://msdn.microsoft.com/en-us/library/bb399567.aspx

http://blogs.msdn.com/adonet/default.aspx

HTH

Michel



<[email protected]> schreef in bericht
You need VS 2008 and SP1 to use this in VS

HTH

Michel

Michel,

I have VS2008 SP1 but what next? Is EF supposed to show up as a
"Visual Studio installed template" when you Create New Project? If so,
I don't see it showing up.

Hawb
 
J

James Hahn

Keep it simple. Return a structure of Int and String (or Int and Object if
you want to have the error object). Test the String for empty (or Object
for Nothing) to see whether or not the integer is valid or you need to raise
the exception.
 
A

Andy B.

Not going to return exceptions. Just catch them in a try...catch block and
log them in the application logs.
 
A

Andy B.

Right click the project and go to add...>new item. In the list of object
templates choose EntityDataModel.
You need VS 2008 and SP1 to use this in VS

HTH

Michel

Michel,

I have VS2008 SP1 but what next? Is EF supposed to show up as a
"Visual Studio installed template" when you Create New Project? If so,
I don't see it showing up.

Hawb
 

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