Hi All

This is test code

'=========================================================
Public Type Test1
t As String
i As Integer
End Type
Public Type Test2
t As String
i As Integer
l As Long
End Type

Public Tests1() As Test1
Public Tests2() As Test2

Public Function init()
ReDim Tests1(0) As Test1
ReDim Tests2(0) As Test2
End Function

'============================================
Public Function AddElement(ByRef tt As Variant, ByRef t())
Dim i As Long
Dim nArr() As Variant
ReDim nArr(0) As Variant
nArr = t

i = UBound(t) + 1
ReDim Preserve t(i)
t(i) = tt
End Function
''====================================================
'
'this function is not work. is there any way to make same job?



Public Function TestFunc()
Dim nt As Test1
Dim nt1 As Test2
Dim v As Variant
Call AddElement(nt, Tests1())

Tests2 = AddElement(nt1, Tests2())

End Function

'=========================================================
How to do make template function for AddElement ?


Thanks

Re: How to make template function for array?? by Amrit

Amrit
Sun Oct 12 11:26:02 CDT 2008

OH,
is this not possible in VB?



"Amrit" <cadd@wlink.com.np> wrote in message
news:uqQjvz9KJHA.1160@TK2MSFTNGP04.phx.gbl...
> Hi All
>
> This is test code
>
> '=========================================================
> Public Type Test1
> t As String
> i As Integer
> End Type
> Public Type Test2
> t As String
> i As Integer
> l As Long
> End Type
>
> Public Tests1() As Test1
> Public Tests2() As Test2
>
> Public Function init()
> ReDim Tests1(0) As Test1
> ReDim Tests2(0) As Test2
> End Function
>
> '============================================
> Public Function AddElement(ByRef tt As Variant, ByRef t())
> Dim i As Long
> Dim nArr() As Variant
> ReDim nArr(0) As Variant
> nArr = t
>
> i = UBound(t) + 1
> ReDim Preserve t(i)
> t(i) = tt
> End Function
> ''====================================================
> '
> 'this function is not work. is there any way to make same job?
>
>
>
> Public Function TestFunc()
> Dim nt As Test1
> Dim nt1 As Test2
> Dim v As Variant
> Call AddElement(nt, Tests1())
>
> Tests2 = AddElement(nt1, Tests2())
>
> End Function
>
> '=========================================================
> How to do make template function for AddElement ?
>
>
> Thanks
>



Re: How to make template function for array?? by Larry

Larry
Sun Oct 12 14:20:18 CDT 2008


"Amrit" <cadd@wlink.com.np> wrote
> OH,
> is this not possible in VB?

If your UDT's were classes, and your arrays were collection
objects, a template would be possible....

LFS



Re: How to make template function for array?? by Ralph

Ralph
Sun Oct 12 14:05:12 CDT 2008


"Amrit" <cadd@wlink.com.np> wrote in message
news:e9yQQdILJHA.4708@TK2MSFTNGP02.phx.gbl...
> OH,
> is this not possible in VB?
>

> > How to do make template function for AddElement ?

A 'template' is a code contruct which allows one to provide generic
instructions to handle any given type. However, any such construction is
resolved to a specific datatype at compile time. VB doesn't prove any
programming mechanism to do that.

You could do something similar by using "parameter polymorphism" by adding
an additional type identifier element to each of the types.
Public Type Test1
t_type As E_TESTTYPE ' eTestOne
t As String
i As Integer
End Type
You could then conditionally test the types within your routines to perform
the action required.

You can also convert the Types to classes and provide an Add method.

It really comes down to what are you are trying to do. Why do you feel you
need a generic "AddElement"?

-ralph



Re: How to make template function for array?? by Bill

Bill
Sun Oct 12 20:31:52 CDT 2008

Hi Armit,

The problem is the use of UDT's. You have to put them as a public type in
an acitveX class then you can pass them as variant. But you will also need
to change your arrays to being arrays of variants.
Personally I would just write two different functions as that would perform
much better than resorting to variants everywhere.



"Amrit" <cadd@wlink.com.np> wrote in message
news:uqQjvz9KJHA.1160@TK2MSFTNGP04.phx.gbl...
> Hi All
>
> This is test code
>
> '=========================================================
> Public Type Test1
> t As String
> i As Integer
> End Type
> Public Type Test2
> t As String
> i As Integer
> l As Long
> End Type
>
> Public Tests1() As Test1
> Public Tests2() As Test2
>
> Public Function init()
> ReDim Tests1(0) As Test1
> ReDim Tests2(0) As Test2
> End Function
>
> '============================================
> Public Function AddElement(ByRef tt As Variant, ByRef t())
> Dim i As Long
> Dim nArr() As Variant
> ReDim nArr(0) As Variant
> nArr = t
>
> i = UBound(t) + 1
> ReDim Preserve t(i)
> t(i) = tt
> End Function
> ''====================================================
> '
> 'this function is not work. is there any way to make same job?
>
>
>
> Public Function TestFunc()
> Dim nt As Test1
> Dim nt1 As Test2
> Dim v As Variant
> Call AddElement(nt, Tests1())
>
> Tests2 = AddElement(nt1, Tests2())
>
> End Function
>
> '=========================================================
> How to do make template function for AddElement ?
>
>
> Thanks
>


Re: How to make template function for array?? by Bill

Bill
Sun Oct 12 20:20:52 CDT 2008


"Ralph" <nt_consulting64@yahoo.com> wrote in message
news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>
> A 'template' is a code contruct which allows one to provide generic
> instructions to handle any given type. However, any such construction is
> resolved to a specific datatype at compile time.


Not necessarily. Generics in the CLR are resolved at runtime and hence can
be used with dynamic type generation.


Re: How to make template function for array?? by dot

dot
Sun Oct 12 20:55:40 CDT 2008

On Mon, 13 Oct 2008 12:20:52 +1100, "Bill McCarthy" <Bill@localhost.com>
wrote:
in <#35pBONLJHA.3080@TK2MSFTNGP06.phx.gbl>

>
>"Ralph" <nt_consulting64@yahoo.com> wrote in message
>news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>>
>> A 'template' is a code contruct which allows one to provide generic
>> instructions to handle any given type. However, any such construction is
>> resolved to a specific datatype at compile time.
>
>
>Not necessarily. Generics in the CLR are resolved at runtime and hence can
>be used with dynamic type generation.

<TROLL> This has nothing to do with VB6 and is therefore completely
irrelevant and off-topic in this group.

You have been asked before to please stop your harassment, bullying,
intimidation, obsessive behavior and fixation on poster's hair and other
qualities. Please cease immediately your harassment, bullying, and
intimidating behavior and, if you can't help simply don't read the
posts.
</TROLL>

Re: How to make template function for array?? by Bill

Bill
Sun Oct 12 21:00:52 CDT 2008


"dot nyet" <no.dotnet.4me@nospam.com> wrote in message
news:qs95f41e7nhs8qsb8elbtb9lfneb7tp3i5@4ax.com...

<yawn />


Re: How to make template function for array?? by Amrit

Amrit
Mon Oct 13 02:15:10 CDT 2008


Hi Ralph
thanks for reply

> It really comes down to what are you are trying to do. Why do you feel you
> need a generic "AddElement"?

I have lot of datatype and i store in Array of datatype. and i have to write
same function for AllDatatype like add remove item get set etc....
instate of writing a lot same function for many datatype. i want to reduct
my code.

if i have Template function then i can use this function for all datatype.

i try to do this program in VC++ but i there i get problem in global
veriable access.

> You could do something similar by using "parameter polymorphism" by adding
> an additional type identifier element to each of the types.
> Public Type Test1
> t_type As E_TESTTYPE ' eTestOne
> t As String
> i As Integer
> End Type
> You could then conditionally test the types within your routines to
> perform
> the action required.

this really i did not understand. How to apply? and what should be in
E_TESTTYPE

> You can also convert the Types to classes and provide an Add method.
now i am doing in this way.
but i have to write all function for each datatype.



"Ralph" <nt_consulting64@yahoo.com> wrote in message
news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>
> "Amrit" <cadd@wlink.com.np> wrote in message
> news:e9yQQdILJHA.4708@TK2MSFTNGP02.phx.gbl...
>> OH,
>> is this not possible in VB?
>>
>
>> > How to do make template function for AddElement ?
>
> A 'template' is a code contruct which allows one to provide generic
> instructions to handle any given type. However, any such construction is
> resolved to a specific datatype at compile time. VB doesn't prove any
> programming mechanism to do that.
>

>

>

>
> -ralph
>
>



Re: How to make template function for array?? by Amrit

Amrit
Mon Oct 13 02:17:45 CDT 2008


"Bill McCarthy" <Bill@localhost.com> wrote in message
news:%2335pBONLJHA.3080@TK2MSFTNGP06.phx.gbl...
>
> "Ralph" <nt_consulting64@yahoo.com> wrote in message
> news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>>
>> A 'template' is a code contruct which allows one to provide generic
>> instructions to handle any given type. However, any such construction is
>> resolved to a specific datatype at compile time.
>
>
> Not necessarily. Generics in the CLR are resolved at runtime and hence
> can be used with dynamic type generation.

How ? and Where the CLR in VB6? i think this is for DotNet only.



Re: How to make template function for array?? by Schmidt

Schmidt
Mon Oct 13 02:54:23 CDT 2008


"Amrit" <cadd@wlink.com.np> schrieb im Newsbeitrag
news:uqQjvz9KJHA.1160@TK2MSFTNGP04.phx.gbl...

> How to do make template function for AddElement ?

The following works for me (note, that even your
init-function is not needed anymore, since VB
initializes the safearray-struct of a given type,
when passed to a Variant-Parameter - that non-
redimed Arr shows then that typical behaviour of
having an LBound of 0 and an UBound of -1).

'***Into a module
Public Tests1() As Test1
Public Tests2() As Test2

Public Function AddElement(NewEntry, Arr)
Dim NewUB As Long
NewUB = UBound(Arr) + 1

ReDim Preserve Arr(NewUB)
Arr(NewUB) = NewEntry
End Function

Public Function TestFunc()
Dim T1 As Test1, T2 As Test2
AddElement T1, Tests1
AddElement T2, Tests2
End Function
'-----------------------------------------

To make this work, you will only have to place your
TypeDefs in a Public Class of an ActiveX-Dll (no
other Code in this Dll-Project).

Name the Class for example IArrTypeDefs
and put your type-defs there:
Public Type Test1
t As String
i As Integer
End Type
Public Type Test2
t As String
i As Integer
l As Long
End Type

That's it - now name the Project e.g. ArrTypeDefs
and compile to ArrTypeDefs.dll

Then place a reference to ArrTypeDefs in your main-
project.
Could be, that you will have to deploy this small
AX-Dll together with your solution (registering it
at your target-machines).

But nonetheless that solution seems elegant enough.

Olaf



Re: How to make template function for array?? by Amrit

Amrit
Mon Oct 13 03:05:12 CDT 2008

Hi

I know this
Collection is slow for loop with many if.




"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:ejaO$9JLJHA.3080@TK2MSFTNGP06.phx.gbl...
>
> "Amrit" <cadd@wlink.com.np> wrote
>> OH,
>> is this not possible in VB?
>
> If your UDT's were classes, and your arrays were collection
> objects, a template would be possible....
>
> LFS
>
>






Re: How to make template function for array?? by Amrit

Amrit
Mon Oct 13 03:21:21 CDT 2008


O! Yea Thanks a lot.

but will it work insice Class. ( I Will Try)

Thanks any way
Amrit



"Schmidt" <sss@online.de> wrote in message
news:O$Tz1jQLJHA.5232@TK2MSFTNGP02.phx.gbl...
>
> "Amrit" <cadd@wlink.com.np> schrieb im Newsbeitrag
> news:uqQjvz9KJHA.1160@TK2MSFTNGP04.phx.gbl...
>
>> How to do make template function for AddElement ?
>
> The following works for me (note, that even your
> init-function is not needed anymore, since VB
> initializes the safearray-struct of a given type,
> when passed to a Variant-Parameter - that non-
> redimed Arr shows then that typical behaviour of
> having an LBound of 0 and an UBound of -1).
>
> '***Into a module
> Public Tests1() As Test1
> Public Tests2() As Test2
>
> Public Function AddElement(NewEntry, Arr)
> Dim NewUB As Long
> NewUB = UBound(Arr) + 1
>
> ReDim Preserve Arr(NewUB)
> Arr(NewUB) = NewEntry
> End Function
>
> Public Function TestFunc()
> Dim T1 As Test1, T2 As Test2
> AddElement T1, Tests1
> AddElement T2, Tests2
> End Function
> '-----------------------------------------
>
> To make this work, you will only have to place your
> TypeDefs in a Public Class of an ActiveX-Dll (no
> other Code in this Dll-Project).
>
> Name the Class for example IArrTypeDefs
> and put your type-defs there:
> Public Type Test1
> t As String
> i As Integer
> End Type
> Public Type Test2
> t As String
> i As Integer
> l As Long
> End Type
>
> That's it - now name the Project e.g. ArrTypeDefs
> and compile to ArrTypeDefs.dll
>
> Then place a reference to ArrTypeDefs in your main-
> project.
> Could be, that you will have to deploy this small
> AX-Dll together with your solution (registering it
> at your target-machines).
>
> But nonetheless that solution seems elegant enough.
>
> Olaf
>
>




Re: How to make template function for array?? by Schmidt

Schmidt
Mon Oct 13 03:42:18 CDT 2008


"Amrit" <cadd@wlink.com.np> schrieb im Newsbeitrag
news:e1MNpyQLJHA.3080@TK2MSFTNGP06.phx.gbl...

> but will it work insice Class. ( I Will Try)
Yes, think that the generic functions would also
work, if you place them Public inside the class
as methods.

Olaf



Re: How to make template function for array?? by Bill

Bill
Tue Oct 14 05:48:58 CDT 2008


"Amrit" <cadd@wlink.com.np> wrote in message
news:uRWvhPQLJHA.6000@TK2MSFTNGP04.phx.gbl...
>
> "Bill McCarthy" <Bill@localhost.com> wrote in message
> news:%2335pBONLJHA.3080@TK2MSFTNGP06.phx.gbl...
>>
>> "Ralph" <nt_consulting64@yahoo.com> wrote in message
>> news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>>>
>>> A 'template' is a code contruct which allows one to provide generic
>>> instructions to handle any given type. However, any such construction is
>>> resolved to a specific datatype at compile time.
>>
>>
>> Not necessarily. Generics in the CLR are resolved at runtime and hence
>> can be used with dynamic type generation.
>
> How ? and Where the CLR in VB6? i think this is for DotNet only.

Right, CLR is the "Common Language Runtime" or in other words the runtime
engine for dotnet. Ralph was referring to generics such as C++'s templates
which are a compile time only thing. I'm not sure why he decided to post
about languages other than VB, but his claim was wrong, especially in
regards to modern day VB.

In any case, see my earlier reply to your post which describes using an
activex dll to gain variance with the user defined types.


Re: How to make template function for array?? by Amrit

Amrit
Tue Oct 14 06:16:50 CDT 2008

Yap.

Thanks



"Bill McCarthy" <Bill@localhost.com> wrote in message
news:uEAF%23peLJHA.728@TK2MSFTNGP03.phx.gbl...
>
> "Amrit" <cadd@wlink.com.np> wrote in message
> news:uRWvhPQLJHA.6000@TK2MSFTNGP04.phx.gbl...
>>
>> "Bill McCarthy" <Bill@localhost.com> wrote in message
>> news:%2335pBONLJHA.3080@TK2MSFTNGP06.phx.gbl...
>>>
>>> "Ralph" <nt_consulting64@yahoo.com> wrote in message
>>> news:ev0rWGKLJHA.3412@TK2MSFTNGP05.phx.gbl...
>>>>
>>>> A 'template' is a code contruct which allows one to provide generic
>>>> instructions to handle any given type. However, any such construction
>>>> is
>>>> resolved to a specific datatype at compile time.
>>>
>>>
>>> Not necessarily. Generics in the CLR are resolved at runtime and hence
>>> can be used with dynamic type generation.
>>
>> How ? and Where the CLR in VB6? i think this is for DotNet only.
>
> Right, CLR is the "Common Language Runtime" or in other words the runtime
> engine for dotnet. Ralph was referring to generics such as C++'s
> templates which are a compile time only thing. I'm not sure why he
> decided to post about languages other than VB, but his claim was wrong,
> especially in regards to modern day VB.
>
> In any case, see my earlier reply to your post which describes using an
> activex dll to gain variance with the user defined types.
>



Re: How to make template function for array?? by Ralph

Ralph
Tue Oct 14 07:40:09 CDT 2008


"Bill McCarthy" <Bill@localhost.com> wrote in message
news:uEAF%23peLJHA.728@TK2MSFTNGP03.phx.gbl...
>
> Right, CLR is the "Common Language Runtime" or in other words the runtime
> engine for dotnet. Ralph was referring to generics such as C++'s
templates
> which are a compile time only thing. I'm not sure why he decided to post
> about languages other than VB, but his claim was wrong, especially in
> regards to modern day VB.
>
> In any case, see my earlier reply to your post which describes using an
> activex dll to gain variance with the user defined types.
>

"Template" like most computing terms is overloaded. However, the common
description among profession programmers is of a technique that supplies
information for "compile-time" substitution.
http://en.wikipedia.org/wiki/Template_metaprogramming

Confusion is understandable since templates are part of a continuous glide
into what is commonly referred to as "generic programming"...
http://en.wikipedia.org/wiki/Generic_programming

The technique you are describing and available in dotNet languages is called
"Generics" and achieved from implementing Generic Classes and Generic
Methods. While both templates and generics provide support for parameterized
types they differ in syntax and functionality, and of course the fact that
Generic type substitution information is completed at runtime. That is
likely why they are called "Generics" and not "Templates".

But it is of little matter, since neither construct is available in VB.
Parameter polymorphism can be supported in VB using Variants aptly
demonstrated by Olaf.

-ralph



Re: How to make template function for array?? by Bill

Bill
Tue Oct 14 08:06:48 CDT 2008

Hi Ralph,

"Ralph" <nt_consulting64@yahoo.com> wrote in message
news:%2368%23%23qfLJHA.1500@TK2MSFTNGP06.phx.gbl...
>
> "Bill McCarthy" <Bill@localhost.com> wrote in message
> news:uEAF%23peLJHA.728@TK2MSFTNGP03.phx.gbl...
>>
>> Right, CLR is the "Common Language Runtime" or in other words the runtime
>> engine for dotnet. Ralph was referring to generics such as C++'s
> templates
>> which are a compile time only thing. I'm not sure why he decided to
>> post
>> about languages other than VB, but his claim was wrong, especially in
>> regards to modern day VB.
>>
>> In any case, see my earlier reply to your post which describes using an
>> activex dll to gain variance with the user defined types.
>>
>
> "Template" like most computing terms is overloaded. However, the common
> description among profession programmers is of a technique that supplies
> information for "compile-time" substitution.
> http://en.wikipedia.org/wiki/Template_metaprogramming
>
> Confusion is understandable since templates are part of a continuous glide
> into what is commonly referred to as "generic programming"...
> http://en.wikipedia.org/wiki/Generic_programming
>


Right. And you used both terms "generic instructions" and "generic
AddElement" when referring to the function. Hence the comment.


> The technique you are describing and available in dotNet languages is
> called
> "Generics" and achieved from implementing Generic Classes and Generic
> Methods. While both templates and generics provide support for
> parameterized
> types they differ in syntax and functionality, and of course the fact that
> Generic type substitution information is completed at runtime. That is
> likely why they are called "Generics" and not "Templates".
>

Yep.


> But it is of little matter, since neither construct is available in VB.

Neither is available in VB6 ;)