From: Mark Hurd on
Is there a simpler acceptable syntax for
(New Class).Method
than

With New Class
.Method
End With

In VB.NET you can use Call
Call (New Class).Method
but this syntax is not acceptable in VB6 (with or without the () at the
end).

FYI I am looking for VB6 and VB.NET answers, if they are different.
(Using Call in VB.NET is "acceptable", I'm just wondering if there is an
alternative syntax I haven't thought of.)

BTW Note that if you define an "identity" function:
Function Identity(ByVal C As Class) As Class
Identity = C
End Function

(And in VB.NET you can use Identity(Of T)(ByVal O As T) As T: Return O)

you can then say
Identity(New Class).Method
in both languages.

--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

Further info:

Commented out lines give syntax errors.

VB.NET (.WL is an Extension that calls Console.WriteLine, and this was
tested using the Snippet Compiler):

Sub RunSnippet()
with New String(New Char(){"a"c,"b"c,"0"c})
.WL()
end with
'(New String(New Char(){"a"c,"b"c,"1"c})).WL
'(New String(New Char(){"a"c,"b"c,"2"c})).WL()
Call (New String(New Char(){"a"c,"b"c,"3"c})).WL()
Identity(New String(New Char(){"a"c,"b"c,"4"c})).WL
Identity(New String(New Char(){"a"c,"b"c,"5"c})).WL()
DirectCast(New String(New Char(){"a"c,"b"c,"6"c}),String).WL
End Sub

private function Identity(of T)(o as T)as T
return o
end function


VB6:

Sub Main()
With New Form1
.TestFun
End With
'(New Form1).TestFun
'Call (New Form1).TestFun()
Dim i As Integer
'i=(new Form1).TestFun()
Dim f As Form1
Set f = New Form1
f.TestFun
i = f.TestFun
Identity(New Form1).TestFun
Call Identity(New Form1).TestFun
i = Identity(New Form1).TestFun()

End Sub

Private Function Identity(f As Form1) As Form1
Identity = f
End Function

Form1 contains:

Public Function TestFun() As Integer
:
End Function

(Yes I could have used a Class, it was just a test that evolved.)

From: Phill W. on
On 25/02/2010 07:27, Mark Hurd wrote:
> Is there a simpler acceptable syntax for
> (New Class).Method
> than
>
> With New Class
> .Method
> End With

[VB.Net]
Does it /have/ to be an instance method?
If you want a method that can be invoked directly from a class name,
create it as a Shared method:

Class Class1
Public *Shared* Function Method() as SomeType
End Class

Then, for anywhere else, you can call

Class1.Method

If you want a Real World example, have a look at String.Join.

> FYI I am looking for VB6 and VB.NET answers, if they are different.

[VB6]
IIRC, VB6 didn't support shared methods.

A common, if clunky, substitute would be to have a Module containing a
method that returns an instance of the class, something like:

[Module1.bas]
Public Function New_Class1() as Class1
Set New_Class1 = New Class1
End Function

then

Call New_Class1().Method()

HTH,
Phill W.
From: Patrice on
> with New String(New Char(){"a"c,"b"c,"0"c})
> .WL()
> end with

Some more context could help. It could be just Console.WriteLine("ab0") or
perhaps CStr("ab0").WL()? The first one could be the same in VB6 (create an
autoinstanciating Console calls that does what you want).

In this particular example, my personal preference would be still to
consider that I have a console object I'm writing to rather than telling a
particular datatype it has a method to write to the console (or perhaps a
stream writer that could redirect to the console to keep something
general)...

A bit hard to give a more accurate advice without knowing what is your
overall goal beyond the syntax issue...

--
Patrice




From: AMercer on
I look forward to someone telling us when
(new class(...)).method
is legal and when it is not. I use it some, and I can't predict what cases
VB (.net) will accept/reject. They may all be legal if 'class' is a separate
compilation from the code containing its call - my confusing cases are all
with a class and its use being compiled together.

This situation is a good rationale for my favorite VB expansion/pipedream,
namely adding old C style macros (#define). Perhaps the pathologies of poor
or over use of macros could be designed out, and thus perhaps with VB macros
we could have all gain and no pain, and thus the world will be a better
place. I'm sold, how about you?

"Mark Hurd" wrote:

> Is there a simpler acceptable syntax for
> (New Class).Method
> than
>
> With New Class
> .Method
> End With
>
> In VB.NET you can use Call
> Call (New Class).Method
> but this syntax is not acceptable in VB6 (with or without the () at the
> end).
>
> FYI I am looking for VB6 and VB.NET answers, if they are different.
> (Using Call in VB.NET is "acceptable", I'm just wondering if there is an
> alternative syntax I haven't thought of.)
>
> BTW Note that if you define an "identity" function:
> Function Identity(ByVal C As Class) As Class
> Identity = C
> End Function
>
> (And in VB.NET you can use Identity(Of T)(ByVal O As T) As T: Return O)
>
> you can then say
> Identity(New Class).Method
> in both languages.
>
> --
> Regards,
> Mark Hurd, B.Sc.(Ma.) (Hons.)
>
> Further info:
>
> Commented out lines give syntax errors.
>
> VB.NET (.WL is an Extension that calls Console.WriteLine, and this was
> tested using the Snippet Compiler):
>
> Sub RunSnippet()
> with New String(New Char(){"a"c,"b"c,"0"c})
> .WL()
> end with
> '(New String(New Char(){"a"c,"b"c,"1"c})).WL
> '(New String(New Char(){"a"c,"b"c,"2"c})).WL()
> Call (New String(New Char(){"a"c,"b"c,"3"c})).WL()
> Identity(New String(New Char(){"a"c,"b"c,"4"c})).WL
> Identity(New String(New Char(){"a"c,"b"c,"5"c})).WL()
> DirectCast(New String(New Char(){"a"c,"b"c,"6"c}),String).WL
> End Sub
>
> private function Identity(of T)(o as T)as T
> return o
> end function
>
>
> VB6:
>
> Sub Main()
> With New Form1
> .TestFun
> End With
> '(New Form1).TestFun
> 'Call (New Form1).TestFun()
> Dim i As Integer
> 'i=(new Form1).TestFun()
> Dim f As Form1
> Set f = New Form1
> f.TestFun
> i = f.TestFun
> Identity(New Form1).TestFun
> Call Identity(New Form1).TestFun
> i = Identity(New Form1).TestFun()
>
> End Sub
>
> Private Function Identity(f As Form1) As Form1
> Identity = f
> End Function
>
> Form1 contains:
>
> Public Function TestFun() As Integer
> :
> End Function
>
> (Yes I could have used a Class, it was just a test that evolved.)
>
> .
>
From: Patrice on
Adn when not legal the error message is ?

Is this the "Access of shared member, constant member, enum member or
nested type through an instance; qualifying expression will not be
evaluated." warning ?

IMO calling a method without needing to keep the instance sound like a
design issue in the class (i.e. it should have a static method). In the
worst case, it could be just wrapped in a utility class...

--
Patrice