Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

using idictionary / hashtable from vbscript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pmoore - 02 Dec 2004 00:05 GMT
I have a class derived from IDictionary that I want to use in vbscript.
None of the enumeration operations seem to work so I thought I would try out
a standard hasthable - that doesnt work either
set hash = CreateObject("System.Collections.Hashtable")
hash("a") = "aa"
hash("b") = "bbb"

wscript.echo hash("a")

wscript.echo hash.Keys.Count
wscript.echo hash.Values.Count
wscript.echo hash.Count

for each entry in hash.Keys
    wscript.echo entry
next   

the for each fails saying that the object doesnt support the methos. I
assume it is something to do with the maping between GetEnumerator and
Dispid=-4/getNewEnum
"Peter Huang" [MSFT] - 02 Dec 2004 09:09 GMT
Hi,

Based on my understanding, you wants to expose the .NET collection to the
COM client so that the COM Client can access it with for each syntax.
Now I am research the isssue and we will reply here with more information
as soon as possible.
If you have any more concerns on it, please feel free to post here.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

"Peter Huang" [MSFT] - 03 Dec 2004 10:17 GMT
Hi,

I think you may try the code below.(You may try to expose the class library
to COM, so that the com client can access to the library.
HOW TO: Make a Visual C# .NET Class Usable in a foreach Statement
http://support.microsoft.com/?id=322022

You may have a try and let us know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

pmoore - 06 Dec 2004 18:01 GMT
I know this, but it is not useful.

I want to expose a class that implements IDictionary - I do not have a
choice on the interface. IDictionary implements IEnumerable

So in my example I showed trying to use the standard map class (which
implements IDictionary) this does not work.

> Hi,
>
[quoted text clipped - 12 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 07 Dec 2004 05:06 GMT
Hi

Thanks for your quickly reply!

We need to implment the IDictionaryEnumerator and IDictionary.
Also we need to expose the GetEnumerator method so that it can support
Enumerate.
        [DispId(-4)]
        [return:
MarshalAs(UnmanagedType.CustomMarshaler,MarshalType="System.Runtime.InteropS
ervices.CustomMarshalers.EnumeratorToEnumVariantMarshaler,CustomMarshalers,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public IDictionaryEnumerator GetEnumerator()

Also we need to return a public class in the current property which will
return a public class.
public class DirEnt
public object Current
{
    get
    {
        // TODO:  Add ColCls.Current getter implementation
        DictionaryEntry de = this.Entry;
        return new DirEnt(de.Key,de.Value);
    }
}

Here is the whole code snippet.
We can call in the vbscript as below.
dim o
dim objs
set objs = CreateObject("TestCo1.ColCls")
objs("a")="Hello"
objs("b")="World"
for each o in objs
WScript.Echo o.Key
WScript.Echo o.Value
next

using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace TestCo1
{
    public class DirEnt
    {
        private object _key;
        private object _value;
        public DirEnt()
        {
        }
        public DirEnt(object k,object v)
        {
            _key=k;
            _value = v;
        }
        public object Key
        {
            get  {return _key;}
            set {_key=value;}
        }
        public object Value
        {
            get {return _value;}
            set {_value=value;}
        }
    }
    public class ColCls :IDictionaryEnumerator,IDictionary
    {
        private Hashtable ht = new Hashtable();
        IDictionaryEnumerator ide=null;
        public ColCls()
        {
        }
        #region IDictionary Members

        public bool IsReadOnly
        {
            get
            {
                // TODO:  Add ColCls.IsReadOnly getter implementation
                return ht.IsReadOnly;
            }
        }
        [DispId(-4)]
        [return:
MarshalAs(UnmanagedType.CustomMarshaler,MarshalType="System.Runtime.InteropS
ervices.CustomMarshalers.EnumeratorToEnumVariantMarshaler,CustomMarshalers,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        public IDictionaryEnumerator GetEnumerator()
        {
            // TODO:  Add ColCls.GetEnumerator implementation
            System.Diagnostics.Debug.WriteLine("public IDictionaryEnumerator
GetEnumerator()");
            ide = ht.GetEnumerator();
            return (IDictionaryEnumerator)this;
        }

        public object this[object key]
        {
            get
            {
                // TODO:  Add ColCls.this getter implementation
                return ht[key];
            }
            set
            {
                // TODO:  Add ColCls.this setter implementation
                ht[key]=value;
            }
        }

        public void Remove(object key)
        {
            // TODO:  Add ColCls.Remove implementation
            ht.Remove(key);
        }

        public bool Contains(object key)
        {
            // TODO:  Add ColCls.Contains implementation
            return ht.Contains(key);
        }

        public void Clear()
        {
            // TODO:  Add ColCls.Clear implementation
            ht.Clear();
        }

        public ICollection Values
        {
            get
            {
                // TODO:  Add ColCls.Values getter implementation
                return ht.Values;
            }
        }

        public void Add(object key, object value)
        {
            // TODO:  Add ColCls.Add implementation
            ht.Add(key,value);
        }

        public ICollection Keys
        {
            get
            {
                // TODO:  Add ColCls.Keys getter implementation
                return ht.Keys;
            }
        }

        public bool IsFixedSize
        {
            get
            {
                // TODO:  Add ColCls.IsFixedSize getter implementation
                return ht.IsFixedSize;
            }
        }

        #endregion

        #region ICollection Members

        public bool IsSynchronized
        {
            get
            {
                // TODO:  Add ColCls.IsSynchronized getter implementation
                return ht.IsSynchronized;
            }
        }

        public int Count
        {
            get
            {
                // TODO:  Add ColCls.Count getter implementation
                return ht.Count;
            }
        }

        public void CopyTo(Array array, int index)
        {
            // TODO:  Add ColCls.CopyTo implementation
            ht.CopyTo(array,index);
        }

        public object SyncRoot
        {
            get
            {
                // TODO:  Add ColCls.SyncRoot getter implementation
                return ht.SyncRoot;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            // TODO:  Add ColCls.System.Collections.IEnumerable.GetEnumerator
implementation
            System.Diagnostics.Debug.WriteLine("IEnumerator
System.Collections.IEnumerable.GetEnumerator()");
            return this;//ht.GetEnumerator();
        }

        #endregion

        #region IEnumerator Members

        public void Reset()
        {
            // TODO:  Add ColCls.Reset implementation
            ide.Reset();
        }

        public object Current
        {
            get
            {
                // TODO:  Add ColCls.Current getter implementation
                DictionaryEntry de = this.Entry;
                return new DirEnt(de.Key,de.Value);
            }
        }

        public bool MoveNext()
        {
            // TODO:  Add ColCls.MoveNext implementation
            return ide.MoveNext();
        }

        #endregion

        #region IDictionaryEnumerator Members

        public object Key
        {
            get
            {
                // TODO:  Add ColCls.Key getter implementation
                return this.Key;
            }
        }

        public object Value
        {
            get
            {
                // TODO:  Add ColCls.Value getter implementation
                return this.Value;
            }
        }

        public DictionaryEntry Entry
        {
            get
            {
                // TODO:  Add ColCls.Entry getter implementation
                return ide.Entry;
            }
        }

        #endregion

    }
}

Best regards,

Perter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.