(New Object).Method

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.) 

0
Mark
2/25/2010 7:27:47 AM
dotnet.languages.vb 830 articles. 0 followers. Follow

6 Replies
965 Views

Similar Articles

[PageSpeed] 23

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.
0
Phill
2/25/2010 12:01:22 PM
> 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

 


0
Patrice
2/25/2010 12:11:40 PM
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.) 
> 
> .
> 
0
Utf
2/25/2010 1:27:01 PM
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
 


0
Patrice
2/25/2010 4:06:35 PM
On 2010-02-25, Patrice <http> wrote:
> 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...
>

I can almost agreee - BUT...  I find my self doing this sort of thing as well.
For instance, I have a security class that does all kinds of things - but
occasionally, I only want to call one method... 

Dim usr As EUser = New SecurityLogic(connectionString).GetUser (identity)

I mean, you could set a variable to hold the instance, but why?  

-- 
Tom Shelton
0
Tom
2/25/2010 5:20:44 PM
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.

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

"Patrice" <http://www.chez.com/scribe/> wrote in message 
news:eCFjxOhtKHA.6140@TK2MSFTNGP05.phx.gbl...
>> 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
> 

0
Mark
2/25/2010 11:52:37 PM
Reply:

Similar Artilces:

Must buy new computer...Ideas?
Hello to all! Finally my over 10-1/2 year old Dell desktop's extreme slowness has seriously gotten on my nerves and I'm now looking for a new one. (Surprisingly, it's working pretty well otherwise...) Maybe this isn't the right forum? But beside the usual things like RAM and HD GBs, are there things I should be sure to have...or be careful NOT to have on a desktop? After n ot looking into computers for over a decade I feel like a Martian in the Earth. lol! Also, is it possible to transfer to the new one ALL my stuff, including programs, etc.? Do you thin...

Default properties for new folders
How does one set the default properties applied when creating new email folders? Currently, when I create a new folder, it is created with the AutoPreview turned on by default. I'd like to change that do that it is left off by default. It's something of a pain to create a new folder and then go in to customize it's properties. You can do this by editing the Messages view; View-> Arrange By-> Current View-> Define Views... You might want to define a new view yourself and apply that view on first use of that folder. -- Robert Sparnaaij [MVP-Outlook] www.howto-o...

Form/SubForm error while reference the object
I am a novice at Access and do not understand the VB codes. I have created a form (Software Register) with a subform (Licence No). I have created a one to many relationship between Software_ID_No in both forms. The Link Child and Link Master fields are populated with Software_ID_No. My subform (Licence No) now is linked to the main form (software Register) and moves with that form. What I want to do is have the Software_ID_No in the main form populate the Software_ID_No in the subform, and also put it into the Licence No Table. If I enter the number manually, it works. When I c...

New Email Messages
I just installed Outlook 2007 on my new PC and for some reason the new messages I received can only be received when I press the SEND/RECEIVE BUTTON. My Outlook at work receives these new mail messages as they are received. So how can I configure the system to make that happen? -- The Trail Hiker!! There are two settings within the accounts 1) Send on starting 2) Auto send/receive every xx minutes - set this for 10 minutes "hiker0531" <hiker0531@aol.com> wrote in message news:87259C7A-5C07-4846-BF61-DE3F98345D21@microsoft.com... >I just installed Outlook 2007 on my n...

VBA Sort method w/more than 3 Keys
How can I sort a worksheet in VBA on more than 3 columns? Same way as manually. Sort multiple times. Sort by the 3 most minor columns first, then the next 3 most minor, ..., until you get to the 3 most major. KenRoy wrote: > > How can I sort a worksheet in VBA on more than 3 columns? -- Dave Peterson ...

Prevent new comments on old posts in sharepoint blogs
How do I stop comments on certain posts in a SharePoint blog? I have removed permissions to the specific post to the lowest possible access and people can STILL post a comment. If I uncheck one more item in the list of permissions, the users can no longer see the post. Any suggestions? ...

Unable to open "New" window to write email.
I am using Microsoft Outlook in Office 2003. It has been working great until today. When I click "New" to write a new email I get an error window : The messaging interface has returned an unknown error. If the problem persist, Restart Outlook. I have tried this several times, even restarted the computer to no avail. Anyone have a solution???? ...

New Location/New setup
Currently I'm downsizing our office, and moving to a new location. I'm running MS Server2003 as well as Exchange 2003. Once moved, I will no longer require the server (since I am setting up a home office). Are there currently any steps or procedures available to correctly shutdown one office...and basically setup a home office with no exchange server. Also I will be removing the current pc's from the Domain, and setting up a few in a workgroup. I would like to receive emails from the other employees for time being until I completely cancel this email account. I already have a...

A Short Video on Adding/Editing New Customers
If capturing customers at the POS is important to you, take a couple of minutes and watch this video. This application also allows for edit of Global customer information in HQ installations. http://www.retail-pos.com/ReverseLookup.wmv Contact us for detailed info and demo. sales(at)retail-pos.com www.retail-pos.com It would be nice if you could encode the videos with a more common codec, and not in wmv format. This requires the GTM3 codec (GoToMeeting). Windows Media Player 11 doesn't automatically download the codec, and I'm not going through the trouble to download and in...

How do I activate the new mail button
When I start Outlook, the "New", "forward", "reply" and "reply to all" buttons are not active which means I can not send mail. I have no problems receiving mail. Also, on web pages with email links, when I click on them, the Outlook application is not launched which is almost certainly related to this. I think I'm missing something very simple here! Are you in a mail enabled folder? Do you have an active email account installed? --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert...

Style available all sheets new or old
I need a custom style that is available in all spreadsheets, new or old. I can set style and save it in book.xlt in xlstart, but that only works for new workbooks, and doesn't work with old spreadsheets when I call them up. I want to be able to select a group of cells, or column or row and apply a setting for negative numbers to have brackets. I am using Excel 2000. I even tried saving the style in personal.xls, but that doesn't work as it is hidden. Do I have to use a macro? And if so what would it be? Thanks One way: Public Sub MergeStyles() ActiveWorkbook....

Sub Form Assigning New Values to Old Records
I am using a subform within a form to have users update certain fields in the record where the rest remiain locked. On the Main form when the list box (which is bound to a query) is used to select the record set to display in the subform it sometimes takes an old record and assigns a new value to the record. For example it changes the Period field from 9 to 10. Taking a record created in period 9 and assigning a period ten to it which is the selection I make in the list box. Addtionally, it looks like it is taking the first record in the related table to assign this new value to. ...

MS Publisher 2003 Design Gallery
I have created my own design gallery of button and field images within Publisher's design gallery to use within my publications. The issue is that I can only see My Objects gallery when I have opened and use the publication that was used to copy these images into My Objects, I cannot see any of my objects when I open a new publication or a template which I wish to use. How can I make my form button and field objects appear for use in any publication by accessing them from My Objects or another way? Also, how do I share the objects I have created with others for use as a standardised...

Adding new pages
Any thing I do on new pages happens simultaneously on any previous page. Can someone help me please? Can you be more specific? What stencils are you using and what are you trying? John... Visio MVP Need stencils or ideas? http://www.mvps.org/visio/3rdparty.htm Need VBA examples? http://www.mvps.org/visio/VBA.htm Common Visio Questions http://www.mvps.org/visio/common_questions.htm "Joanie" <anonymous@discussions.microsoft.com> wrote in message news:4C3642E4-4F2B-4A65-8C00-E9041CE74A37@microsoft.com... > Any thing I do on new pages happens simultaneously on any previ...

Which table will refresh by adding new Account / Contact
Hi, I am new to CRM and trying to update CRM Accounts and Contacts from our Sales Management system. I am using Micorosft Bulk Import utility. I am in preparation of CVS files but not sure what columns do I need in CVS file to complete one Account/Contact record. Could anybody please help. I am .NET expert developer, please let me guide If I could develop CRM by using .NET not CVS Regards Hi Adnan, Microsoft do provide CRM SDK for CRM enhancment using .net. You can download from following url http://www.microsoft.com/downloads/details.aspx?familyid=9C178B68-3A06-4898-BC83-BD14B74308C5...

New messages comes up in Deleted folder
Sometimes when I receive new messages the go to the Deleted folder instead of the Inbox. The messages are from random people...not just one sender. What is the problem? Thanks Check your message rules/filters. "key word" in new mail subject line of body is probably being tagged and filtered to deleted items folder. Regards C. >-----Original Message----- >Sometimes when I receive new messages the go to the >Deleted folder instead of the Inbox. The messages are >from random people...not just one sender. What is the >problem? Thanks >. > ...

New User has admin rights
I just created a new user and assigned her the role of Sales Manager. This role is working correctly for other users with it assigned, but she has admin rights? This is the only role she has assigned.... Any ideas? TIA Metcalf ...

New trend, new business, new year a new beginning
New trend, at start of new year. "If you want things to work different, you must try a different...whatever..." Does that seem obvious? Brighten up with this "reading" and attack the money issue with more gusto! I am finishing my year 2005 with some regrets and some joy. For some the year passed has not been "tip top" in their view. But I believe it has just been very well as the page I shall bring you that has this statement which I believe wholeheartedly: "THINGS ARE UNFOLDING AS IT SHOULD" (paraphrased) I invite you to read the whole "poem&quo...

Creating new row in other table
Dear All, Please teach me, how to make a new row/list in other table by entering number in other table. Sample, In the table A and in the field "Details", I put the number 2 and it automatically creates two rows in table B. Can this be done in MS Access? Thank you for your help. Regards, Maulwy Why? What business need are you attempting to solve with this approach? It's generally considered not very good database design to be creating 'empty' rows in a second table. Why do you want empty rows? And if the number you enter in TableA is a 'limit&...

cannot insert objects or equations
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel After installing a new hard disk, I cannot insert an equation or other object (i.e. spreadsheet). The &quot;Insert equation&quot; menu item is grayed out, and no objects appear in the Insert Object's &quot;Object Type&quot; box. I have uninstalled Office, reinstalled it, and installed updates. What can I do to make it work again? This can get very ugly, depending on how you installed the hard disk in the first place. The Apple "Migration Utility" seems to make an unholy mess o...

AP
What would be the best tool to add a new field in the AP invoice screen to deploy to a client? Extender? Modifier? the ideal would be to add a field for "Job No." that is text info so the user can get some info out on certain jobs that they are paying bills for. thanks -- Linda W. Linda, You can use any tool, really, to accomplish this: - Modifier with VBA. You can add the fields to the window with Modifier, then use VBA and ADO or DUOS to store and retrieve the data. - Modifier with Dexterity. You can add the fields with Modifier, then trigger off the form with Dex...

Button "New Account" in Sub-Accountsview opens Parent account
When I open an account, and then go to the subaccounts of that account, I click on the Button "New Account" to create a new subaccount for this account. Instead of opening a new account form, the application opens the original Account form. Anybody an idea how this is possible? Regards, Sanne groenhof -- MSCRM, hosted as ASP, and configured for your business. Sanne, Have you installed any specific toolbars in your Internet Explorer? Like google toolbar/msn toolbar? -- Rob Bakkers, Avanade Netherlands "Sanne" wrote: > When I open an account, and then go to...

new to old
hi, A web designing company has just done us a website, and part of it includes the database of our stock. They have written/created it on access xp, but we have only got and old version of access. Can we open/use/update files written in access xp on an older version of access? thanks very much jethro Hi Jethro, It depends. By default Access 2002 (XP) uses the Access 2000 file format, so if you have Access 2000 you may be able to use the database as is. However, if the database is in the form of an mde file created by Access 2002 it will be in Access 2002 format. Otherwise (i.e. if t...

New Accounts on Exchange 2003
This week I entered two new people and the reciepent policy is not providing the necessary email addresses. Of course without those, the GAL does not get updated and no one can email them. I have Exchange 2003 running on Server 2003 with a w2k AD. Any suggestions for jump starting the default reciepent policy? Omer, Thanks for the response. The Recipient Update Service was scheduled for once an hour. I changed it to every 15 minutes. I also tried the "Update Now" feature. For both of these, there were no events generated. I remember setting up trace logs earlier this y...

new columns created from "&nbsp;"
I have a bunch of data in a column and it is "separated" by "&nbsp;". The data was imported from an online database. Is there a way to create columns within the current workbook from where the &nbsp; appears? (much like importing a text file) TIA, Greg Greg Wilker wrote: > I have a bunch of data in a column and it is "separated" by "&nbsp;". > > The data was imported from an online database. > > Is there a way to create columns within the current workbook from where the > &nbsp; appears? (much like importing a te...