HighTechTalks DotNet Forums  

Function Output

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Function Output in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Reza Azimi
 
Posts: n/a

Default Function Output - 01-26-2006 , 01:09 PM






Hello everyone,

Is there a way so that I can have different classes for output of a function?

For instance, I have a function that returns a class and this class can be
changed according to the arguments that the function receives.

The idea is something like this:
‘VB code
Private function SelectClass( WhichClass as Boolean) as class
If WhichClass =true then return Class1 else return Class2

I know that I cannot define a function that returns “class” and I need to
specify my class name and keep in mind that I need to use what the function
returns in rest of my project.

That would be like:

SelectedClass=selectclass(True)
SelectedClass.Test=10

I would appreciate any advice.


Reply With Quote
  #2  
Old   
Reza Azimi
 
Posts: n/a

Default Re: Function Output - 01-26-2006 , 03:39 PM






Thank you for your answer.

The problem is that I won’t be able to find out which type (in this case
class) is going to be selected in the function.
I mean which one
Dim C1 as new Class1
C1=SelectClass(true)
Or
Dim C2 as new Class2
C2=SelectClass(false)

The other problem is that the structures of these classes are completely
different. So, I can’t come up with a specific format.


"Mattias Sjögren" wrote:

Quote:
SelectedClass=selectclass(True)
SelectedClass.Test=10

For this to compile (with Option Strict On) you have to declare
SelectedClass as a type that has a Test property. Why don't you make
SelectClass return that type instead?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Reply With Quote
  #3  
Old   
Jevon
 
Posts: n/a

Default Re: Function Output - 01-27-2006 , 09:00 AM



You say that the "structures of these classes are completely different" and
you "can't come up with a specific format."
In that case, taking your previous example:

Quote:
SelectedClass=selectclass(True)
SelectedClass.Test=10
How do you know that .Text exists on both classes? What if it's only on one?

I think what you want to look at is interfaces and polymorphism although
from your description even these might not be suitable in which case you'd
need to restructure your solution.

A (very simple) interface example follows:
Module Test
Public Interface ITester
Property Test(ByVal newValue As Int32) As Int32
End Interface

Public Class TypeOne
Implements ITester
Public Property Test(ByVal newValue As Integer) As Integer
Implements ITester.Test
Get
End Get
Set(ByVal Value As Integer)
End Set
End Property
End Class

Public Class TypeTwo
Implements ITester
Public Property Test(ByVal newValue As Integer) As Integer
Implements ITester.Test
Get
End Get
Set(ByVal Value As Integer)
End Set
End Property
End Class

Sub Main()
Dim mySelectClass As ITester = SelectClass(True)
mySelectClass.Test = 10
End Sub

Private Function SelectClass(ByRef WhichClass As Boolean) As ITester
If WhichClass = True Then
Return New TypeOne
Else
Return New TypeTwo
End If
End Function
End Module




Jevon


"Reza Azimi" <reza.a.azimi (AT) gmail (DOT) com.(Anti-spam)> wrote

Quote:
Thank you for your answer.

The problem is that I won't be able to find out which type (in this case
class) is going to be selected in the function.
I mean which one
Dim C1 as new Class1
C1=SelectClass(true)
Or
Dim C2 as new Class2
C2=SelectClass(false)

The other problem is that the structures of these classes are completely
different. So, I can't come up with a specific format.


"Mattias Sjgren" wrote:

SelectedClass=selectclass(True)
SelectedClass.Test=10

For this to compile (with Option Strict On) you have to declare
SelectedClass as a type that has a Test property. Why don't you make
SelectClass return that type instead?


Mattias

--
Mattias Sjgren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.