Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Using .NET Interops in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Using .NET Interops in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-12-22

    Table of Contents:
  • Using .NET Interops in VBScript
  • Sorting Arrays in VBScript
  • Working with Strings
  • More with StringBuilder

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using .NET Interops in VBScript - Working with Strings


    (Page 3 of 4 )

    .NET interops can also be used to do some advanced string manipulation.  One example of this is the ability to produce formatted strings.  Formatted strings are dynamically generated strings.  Take this classic VBScript example.

    arrOne = Array(1, "loneliest")

    arrTwo = Array(2, "happiest")

    WScript.Echo arrOne(0) & " is a " & arrOne(1) & " number"

    WScript.Echo arrTwo(0) & " is a " & arrTwo(1) & " number"

    This code uses some array data to dynamically construct string output.  This is the same technique you've seen used over and over again to produce an output like the following:

    1 is a loneliest number
    2 is a happiest number

    A formatted string would be one whole string that contained markers where the array data could be inserted without having the mess of concatenating several strings into one.

    Set sb = CreateObject("System.Text.StringBuilder")

    sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(1, "loneliest")

    sb.AppendFormat_5 Nothing, "{0} is a {1} number" & vbCrLf, Array(2, "happiest")

    WScript.Echo sb.ToString()

    Here we're using a single string of the form "{0} is a {1} number."  Each number in brackets indicates a marker where a substring is to be inserted.  The substrings are found in the array provided.

    Once all substitutions have been made, they are added to the StringBuilder object using the AppendFormat_5 method.  As it turns out, the AppendFormat method of the StringBuilder object is an overloaded method.  This means that there are several different methods with the same name that accept different types of parameters.  Since VBScript does not support overloaded methods, each of these overloads are exposed by the COM object as separate, numbered methods.  In this case, we're using the fifth overloaded AppendFormat method.

    The StringBuilder object then provides a ToString method that returns a single string that has been constructed.

    More Windows Scripting Articles
    More By Nilpo


       · Thank you for these articles. I have been looking for a way to access the...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT