(New Object).Method

  • Thread starter Thread starter Mark Hurd
  • Start date Start date
M

Mark Hurd

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.)
 
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.
 
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?
 
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...
 
The actual scenario is due to C# code converted to VB.NET by
SharpDevelop. The code is the Microsoft ASP.NET provider toolkit sample
code. It includes about a dozen calls like this:

(new SqlCommand("COMMIT TRANSACTION",
holder.Connection)).ExecuteNonQuery();

the SharpDevelop converter left them the same without the semicolon and
I found the following works and is acceptable

Call (New SqlCommand("COMMIT TRANSACTION",
holder.Connection)).ExecuteNonQuery()

But the query to these newsgroups was about the general syntax.
 
Back
Top