Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does vbs support enum? HELP!!! 1

Status
Not open for further replies.

grom2

Programmer
Oct 27, 2005
11
CA
What is the technique for accessing enum's from object that are late bound? Do you have use the equivalent number or is their some way to access the enums? I could also create constants representing the enum's and their vals. How are you doing this?

Thanks,
Glenn
 
Have you tried the For Each instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hello,

The for each instruction? No sure I follow. I have a class from vb.net that has an enumeration. Some of the properties in the class type their accessors against the enumeration. A com wrapper has been added to the class to make it accessible from vbs. Now with those properties low can I reference the enumerations? Can you give me an example?

Thanks,
Glenn
 
Set aObject = CreateObject("dll.module")

For Each aThing In aObject

Msgbox CStr(aThing.Name)
If CInt(aThing.Age) > 18 Then

End If
Set aFavAlbums = aThing.Albums
For Each aAlbum In aThing.Albums
MSgbox aAlbum.Artist
Next
Next
 
missed out the Set aFavAlbums = Nothing before the Next but there you go
 
Hmm not exactly what I'm looking for. I just want to do the following from vbs. Can I use the enum's of the class or do I have to create a set of constants that mimic the enums?

Example

' This is the construct of the sample dll
public class myclass
public enum tooltype
hammer=0
axe=1
end enum
public sub myToolType(byval t as tooltype)
' do something with t
end sub
end class

' I was hoping to use the enum from vbs like this

-----------------------------------------------
dim obj
set obj = createobject("dll.myclass")

' set the tool type for the class
obj.myToolType = obj.tooltype.hammer '<-- i can't do this from vbs
-----------------------------------------------
' do I have to use constants in vbs to mimic enums?

Const hammer = 0
Const axe = 1

dim obj
set obj = createobject("dll.myclass")

' set the tool type for the class
obj.myToolType = hammer

Any secret techniuque for in using enums from vbs. Trying to achieve the same access from vbs as I would in vb6, vb.net etc.

Thanks,
Glenn



 
I can only think of one script host that supports this (WSH), and not for lack of complaining to the guy who created a lot of the Windows Scripting system software (Eric Lippert). For one thing IE would be a great start - at least in HTAs.

But anyway... if you are writing these as WSH scripts you do have an option:

Stop writing naked VBS files!

Take a look at the WSF file format. It offers a few other goodies (some great, some not so great) but the one you want here is the <REFERENCE> section.

WSF files are written as either strict or loose XML files. Most of the time you won't need strict ones. By adding a <REFERENCE> tag to your WSH file, WScript/CScript will load the TypeLib of the specified object's library.

This works great to bring in all of those FSO constants as well.

Basically they look like:

Code:
<job>
  <reference object="Scripting.FileSystemObject"/>
  <object id="FSO" progid="Scripting.FileSystemObject"/>
  <script language="VBScript">
    Option Explicit
    Dim tsOut

    'Create Unicode file.
    Set tsOut = FSO.OpenTextFile("some-file.txt", ForWriting, _
                                 True, TriStateTrue)
    tsOut.WriteLine "Here's some Unicode text."
    tsOut.Close
    Set tsOut = Nothing
  </script>
</job>
That's an off-the-cuff example but it should be close to right. Check out the Windows Script Technologies CHM file for more information.

Here's a more extensive example, using a custom code component to provide a character-GUI interface when run using CScript:

Code:
<job>
  <reference object="CharGUI.Controller"/>
  <object id="Con" progid="CharGUI.Controller"/>
  <script language="VBScript">
    Dim panBackdrop
    Dim panStatus
    Dim panText

    Sub PaintBackdrop()
      Set panBackdrop = _
        Con.Panels.Add("Backdrop", 0, 0, 79, 24, bgDkBlack Or fgDkRed)
      With panBackdrop
        .Clear &HB0
        .WriteXY " F1 Show Status " & Chr(&HB3) & _
                 " F2 Hide Status " & Chr(&HB3) & _
                 " F3 Enter Text " & Chr(&HB3) & _
                 " F12 EXIT SCRIPT ", _
                 bgLtCyan Or fgDkBlack, 6, 24
        .Paint
      End With
    End Sub

    Sub MakeStatus()
      Set panStatus = _
        Con.Panels.Add("Status", 0, 0, 33, 4, bgDkBlack Or fgLtCyan, _
                       btSingle, bgDkBlack Or fgLtCyan, "Status Board")
      With panStatus
        .WriteXY " ", bgLtGreen, 2, 1
        .WriteXY "Lights", , 4, 1
        .WriteXY " ", bgLtGreen, 12, 1
        .WriteXY "Camera", , 14, 1
        .WriteXY " ", bgLtGreen, 22, 1
        .WriteXY "Action", , 24, 1
      End With
    End Sub

    Sub PaintTextEntry()
      Set panText = _
        Con.Panels.Add("Text", 5, 7, 74, 19, bgLtBlack Or fgLtWhite, _
                       btDouble, bgDkRed Or fgLtYellow, "Text")
      panText.Paint
    End Sub

    Sub HandleTextEntry()
      Dim intKey

      panBackdrop.Enabled = peNone
      With panText
        .Enabled = peBuffered
        .Title = "Text Entry (ESC to exit)"
        .Paint
        Do
          Do
            WScript.Sleep 50
            intKey = .InKey()
          Loop Until intKey <> 0

          If intKey = ASC_CR Then
            .ReadLine 'Discard input for our purposes.
            .WriteScroll , True
          End If
        Loop Until intKey = ASC_ESC

        .Title = "Text"
        .Paint
        .Enabled = peNone
      End With
      panBackdrop.Enabled = peKeyInput
    End Sub

    Sub MainInputLoop()
      Dim intKey

      With panBackdrop
        .Enabled = peKeyInput

        Do
          Do
            WScript.Sleep 50
            intKey = .InKey()
          Loop Until intKey <> 0

          Select Case intKey
            Case -1 'F1.
              panStatus.Paint
            Case -2 'F2.
              panStatus.Hide panBackdrop.Colors, &HB0
            Case -3 'F3.
              HandleTextEntry
            'Else fall through.
          End Select

        Loop Until intKey = -12 'F12.
      End With
    End Sub

    Con.Initialize , , "cgTest2"
    PaintBackdrop
    MakeStatus
    PaintTextEntry

    MainInputLoop

    Set panText = Nothing
    Set panStatus = Nothing
    Set panBackdrop = Nothing
    Con.Panels.DeleteAll
  </script>
</job>

Note the many constants (actually enums) that are referenced here. The example provides a user interface inside the console that looks a lot like a VBDOS program or something, with bordered "panels" for different bits of information or interaction.
 
Oops, screenshot:

sshot002.gif


Pretty cool if I do say so myself... but the example illustrates my point anyway.

And yes, it does work full-screen and even in 50-line mode. This isn't some popped up automated IE instance, it's the actual console the script was run in.

But I'm sometimes easily amused.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top