473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what are generic Subs and Functions for?

While experimenting with the Feb CTP Edition of VB 2005, I came across
"generic procedures". You can write:

Public Class Foo

Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)

End Sub
End Class

I know what generic classes are for, but a generic Sub? Does anybody know
why they are here? I found nothing about it in the help, it is just
explained as a valid syntax for the Sub statement.

Urs


Nov 21 '05 #1
6 2755
Urs,
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here?
They allow you to define a method (sub or function) that behaves
polymorphically across types. Just as generic classes allow you to define
classes that behave polymorphically across types...

Consider:

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function

Which allows you to use the above IIf in Whidbey (aka VS.NET 2005, due out
later in 2005) with Option Strict On, without needing
to cast the return value.

Dim x, y, r As Integer
r = IIf(x < y, x, y)

Dim a, b, c As String
r = IIf(a Is Nothing, a, b)

NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
specifically needed...

I can see defining a similar routine for Choose, something like:

' VS.NET 2005 syntax
Public Function Choose(Of T)(ByVal index As Integer, _
ParamArray ByVal choice() As T) As T
Return choice(index-1)
End Function

Again you can use the above Choose with Option Strict On without needing to
cast the return value! (see below)

Dim r As String = Choose(x, "Happy", "Sad", "Indifferent")

I can also see, Min and Max:

' VS.NET 2005 syntax
Public Function Min(Of T As IComparable(Of T))(ByVal value1 As T, ByVal
value2 As T) As T
If value1.CompareTo(value2) <= 0 Then
Return value1
Else
Return value2
End If
End Function

' VS.NET 2005 syntax
Public Function Max(Of T As IComparable(Of T))(ByVal value1 As T, ByVal
value2 As T) As T
If value1.CompareTo(value2) >= 0 Then
Return value1
Else
Return value2
End If
End Function

NOTE: Min & Max need the constraint that says the type given can compare
themselves with others, the IComparable(Of T) prevents boxing...

Dim r As Integer = Min(1, 2)
Dim r As Double = Max(1.5, 2.5)

Hope this helps
Jay


"Urs Eichmann" <xx@yy.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
| While experimenting with the Feb CTP Edition of VB 2005, I came across
| "generic procedures". You can write:
|
| Public Class Foo
|
| Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)
|
| End Sub
|
|
| End Class
|
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here? I found nothing about it in the help, it is just
| explained as a valid syntax for the Sub statement.
|
| Urs
|
|
|
|
Nov 21 '05 #2
Jay,
Urs,
| I know what generic classes are for, but a generic Sub? Does anybody know
| why they are here?
They allow you to define a method (sub or function) that behaves
polymorphically across types. Just as generic classes allow you to define
classes that behave polymorphically across types...

Consider:

' VS.NET 2005 syntax
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function
thanks for the explanation. I guess I can also write

Public Function ToArray(Of T)(vList as IList) As T()
return directcast(vList.ToArray(T),T())
End Function

instead of the horrible

dim a() as MyClass= DirectCast(vList.ToArray(gettype(MyClass)),MyClass ())

I use today to convert an IList to an array. Now I guess I can just use

dim a() as MyClass = ToArray(Of MyClass)(vList)

I look forward to it already :-)

NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
specifically needed...
is that new? 2003 doesn't do it

I can see defining a similar routine for Choose, something like:

' VS.NET 2005 syntax
Public Function Choose(Of T)(ByVal index As Integer, _
ParamArray ByVal choice() As T) As T
Return choice(index-1)
End Function


Boah, is somebody still using Choose? :-)

Thanks for your help
Urs
Nov 21 '05 #3
Urs,
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vList.ToArray(T),T())
| End Function
I have not tried (I'm in the middle of rebuilding my test machine).

I'm not sure if VB and/or the Framework will allow the DirectCast or not.
There are a number of rules on what is allowed & what is not allowed, I
don't remember them all...

I would mostly expect it would be allowed.

However! As you may know with the new Generic list classes your conversion
is "unneeded". ;-)

| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
| is that new? 2003 doesn't do it

Yes its new as Generics are new... it is closely related to overloading
methods. In C# its called Type Inference & is explained here, I don't see
the VB definition.

http://msdn2.microsoft.com/library/t...us,vs.80).aspx

Here is a VB swap sample (based on the above):

Public Module UtilityModule

Public Sub Swap(Of T)(ByRef lhs As T, ByRef rhs As T)
Dim temp As T
temp = lhs
lhs = rhs
rhs = temp
End Sub

End Module

Then you can call Swap with either:

Dim a As Integer = 1
Dim b As Integer = 2

Swap(Of Integer)(a, b) ' explicitly call Swap(Of Integer)
Swap(a, b) ' implicitly call Swap(Of Integer)
'as Integer was inferred from the parameters.

| Boah, is somebody still using Choose? :-)
I use it where it makes sense to use it! It makes sense to use it where you
have a list of values to choose from. Granted I don't need it very often...
I know in one program I overloaded Choose with a dozen type safe versions,
using Choose(Of T) will reduce it to a single version...

FWIW: I find Choose to be more useful then the Switch function, as its easy
to create a type safe Choose, however its hard (impossible?) to make a type
safe Switch function...

Hope this helps
Jay


"Urs Eichmann" <ur***@online.nospam> wrote in message
news:OA**************@tk2msftngp13.phx.gbl...
| Jay,
|
| > Urs,
| > | I know what generic classes are for, but a generic Sub? Does anybody
know
| > | why they are here?
| > They allow you to define a method (sub or function) that behaves
| > polymorphically across types. Just as generic classes allow you to
define
| > classes that behave polymorphically across types...
| >
| > Consider:
| >
| > ' VS.NET 2005 syntax
| > Public Function IIf(Of T)(ByVal expression As Boolean, _
| > ByVal truePart As T, ByVal falsePart As T) As T
| > If expression Then
| > Return truePart
| > Else
| > Return falsePart
| > End If
| > End Function
|
| thanks for the explanation. I guess I can also write
|
| Public Function ToArray(Of T)(vList as IList) As T()
| return directcast(vList.ToArray(T),T())
| End Function
|
| instead of the horrible
|
| dim a() as MyClass= DirectCast(vList.ToArray(gettype(MyClass)),MyClass ())
|
| I use today to convert an IList to an array. Now I guess I can just use
|
| dim a() as MyClass = ToArray(Of MyClass)(vList)
|
| I look forward to it already :-)
|
| >
| > NOTE: VB is able to figure out the types, so IIf(Of Integer) is not
| > specifically needed...
|
| is that new? 2003 doesn't do it
|
| >
| > I can see defining a similar routine for Choose, something like:
| >
| > ' VS.NET 2005 syntax
| > Public Function Choose(Of T)(ByVal index As Integer, _
| > ParamArray ByVal choice() As T) As T
| > Return choice(index-1)
| > End Function
|
| Boah, is somebody still using Choose? :-)
|
| Thanks for your help
| Urs
Nov 21 '05 #4
>
| Boah, is somebody still using Choose? :-)
I use it where it makes sense to use it! It makes sense to use it where you
have a list of values to choose from. Granted I don't need it very often...
I know in one program I overloaded Choose with a dozen type safe versions,
using Choose(Of T) will reduce it to a single version...

FWIW: I find Choose to be more useful then the Switch function, as its easy
to create a type safe Choose, however its hard (impossible?) to make a type
safe Switch function...


I use neither because the tend to become lengthy and spaghetti-like. I
always use Select Case. But this is my personal preference, I guess.

urs
Nov 21 '05 #5
Urs,
| I use neither because the tend to become lengthy and spaghetti-like. I
| always use Select Case. But this is my personal preference, I guess.
Odd, Select Case seems to be longer & more "spaghetti-like" then Choose,
especially for the simple cases where I would use choose Choose over Select
Case!

One short & simple line for the Choose
Dim x As Integer = Choose(index, "x", "xxx", "xx", "xxxx")

Where as there are 13 lines for the Select Case.
Dim x As Integer
Select Case index
Case 0
x = "x"
Case 1
x = "xxx"
Case 2
x = "xx"
Case 3
x = "xxxx"
Case Else
Throw New ArgumentOutOfRangeException(...)
End Case

FWIW:

Select Case by itself suggests using the "Replace Condition with
Polymorphism" refactoring
http://www.refactoring.com/catalog/r...ymorphism.html

Both cases (Choose & Select Case) in my example suggests using the
"Substitute Algorithm" refactoring
http://www.refactoring.com/catalog/s...Algorithm.html

Hope this helps
Jay

"Urs Eichmann" <ur***@online.nospam> wrote in message
news:eI*************@tk2msftngp13.phx.gbl...
|>
| > | Boah, is somebody still using Choose? :-)
| > I use it where it makes sense to use it! It makes sense to use it where
you
| > have a list of values to choose from. Granted I don't need it very
often...
| > I know in one program I overloaded Choose with a dozen type safe
versions,
| > using Choose(Of T) will reduce it to a single version...
| >
| > FWIW: I find Choose to be more useful then the Switch function, as its
easy
| > to create a type safe Choose, however its hard (impossible?) to make a
type
| > safe Switch function...
|
| I use neither because the tend to become lengthy and spaghetti-like. I
| always use Select Case. But this is my personal preference, I guess.
|
| urs
Nov 21 '05 #6
Jay,
Odd, Select Case seems to be longer & more "spaghetti-like" then Choose,
especially for the simple cases where I would use choose Choose over Select
Case!


yes, the code is longer, but not "spaghetti-like" because it is clearly
structured. Also, I get intellisense for example when selecting with an
eNum. And it is far more flexible, because you can do things like "Case
0 to 2". And if you have more than 3 or 4 items to choose from, you end
up counting with your fingers to tell which one will be choosed with a
certain index.

So instead of replacing Choose with Select Case sometimes later, I use
Select case in the first place. By using a keyboard shortcut, I don't
even have to type much...

Best regards,
Urs
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Colin Mc Mahon | last post by:
Hi all, First off I know the difference between functions and subs, basically a function returns a value on execution of a code block wheras a sub just executes a code block (more or less). I...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
3
by: jm | last post by:
I have a question about codebehind. I have an upload namespace, class and all in a codebehind. I currently have it so it works with one of my .aspx pages. I want to use it for another .aspx...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
1
by: Sam | last post by:
Hi All Does anyone know if there is a standard format where subs and functions should be placed in a class. I always put them at the very end of my class except my constructor (subs first then...
2
by: Gonzosez | last post by:
I have created several functions and subs in both VB and Java. How can I put these into a file to be called by different pages. Thanks in advance, Gonzo
8
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below....
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.