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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Exposing enums, properties and variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alexia.bee@gmail.com - 27 Feb 2008 11:46 GMT
Hi,

I created dll class library which I exposed methods on it.
I want to expose enums, properties and variables. The problem is that
I don't have an idea.

This is what I do:

namespace EIServices
{

   [Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface _Network
   {
       [DispId(1)]
       EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);
   }

   public class Network
   {
       public enum EI_STAT_SERVICES
       {
           /* Infra */
           ieFAIL = 0,
           ieSUCCESS
       }
       public EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable)
       {
       }
   }
}

What should I do if I want to expose the enum to the dll user?

Thanks for the help.
Jon Skeet [C# MVP] - 27 Feb 2008 11:53 GMT
<snip>

> What should I do if I want to expose the enum to the dll user?

That has already exposed the enum - but nested inside the Network
class. Any reason for not making it a top-level type instead of nesting
it?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

alexia.bee@gmail.com - 28 Feb 2008 06:17 GMT
>  <alexia....@gmail.com> wrote:
>
[quoted text clipped - 9 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Jon hi and thanks for the reply.

How do I make it a top-level type which can be used in vbs also?
Is that the way to expose variables and properties to vbs also?
Jon Skeet [C# MVP] - 28 Feb 2008 07:03 GMT
> How do I make it a top-level type which can be used in vbs also?

Just declare it outside the class.

> Is that the way to expose variables and properties to vbs also?

No - variables and properties always need to be inside a type, whereas
an enum is a type itself.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

alexia.bee@gmail.com - 28 Feb 2008 10:29 GMT
>  <alexia....@gmail.com> wrote:
> > How do I make it a top-level type which can be used in vbs also?
[quoted text clipped - 9 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
   public enum EI_STAT_SERVICES
   {
       ieSUCCESS = 32,
       ieAdapterNotFound
   }
   [Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface _Network
   {
       [DispId(1)]
       EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

   }
   public class Network
   {

   }
}

When I create a C# app  which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.
Willy Denoyette [MVP] - 28 Feb 2008 11:02 GMT
On Feb 28, 9:03 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> <alexia....@gmail.com> wrote:
> > How do I make it a top-level type which can be used in vbs also?
[quoted text clipped - 10 lines]
> Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
   public enum EI_STAT_SERVICES
   {
       ieSUCCESS = 32,
       ieAdapterNotFound
   }
   [Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface _Network
   {
       [DispId(1)]
       EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

   }
   public class Network
   {

   }
}

When I create a C# app  which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.

Hmmm...... Why are you trying to create an instance of an enum type????
The enum is the type returned by the SetDeviceState method.

Here is what you should do...

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> 32 Then
   ...

Willy.
alexia.bee@gmail.com - 28 Feb 2008 14:38 GMT
On Feb 28, 1:02 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.be> wrote:
> <alexia....@gmail.com> wrote in message
>
[quoted text clipped - 65 lines]
>
> Willy.

Hi Willy,

I don't want to write hard coded the val 32, I want to use it in the
following way:

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> stat.ieSUCCESS  Then...
Willy Denoyette [MVP] - 02 Mar 2008 17:34 GMT
On Feb 28, 1:02 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.be> wrote:
> <alexia....@gmail.com> wrote in message
>
[quoted text clipped - 65 lines]
>
> Willy.

Hi Willy,

I don't want to write hard coded the val 32, I want to use it in the
following way:

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> stat.ieSUCCESS  Then...

Well you will need to use Windows Scripting Host (WSH) for this (search MSDN
for details).
In you script file you need to add a "reference" tag to set a reference to
the typelib generated by regasm (or VS) for your component.
Also you need to watch when using enum names in script, use an _ instead of
a dot, for instance if you have an enum in C# that looks like Color.Green,
then you need to specify Color_Green in script.

Here is a simple sample to illustrate the usage, substitute your own
"typelib" uuid with the one in the sample. You can obtain the typelib guid
by looking into the registry or simply by running oleview on your typelib
(somefile.tlb).

<?XML version="1.0" ?>
<package>
 <job>
   ' uuid of the typelib, dont use reference object = ....!
   <reference guid="{98C2D27D-99F6-3DB3-9BB3-508D35BCA248}"/>
   <script language="vbscript">
       ' create an instance of a C# class
       set o = WScript.CreateObject("xxxx.yyyy")
       ' call a method that retruns an enum (Color) value
       val = o.GetColor()
     If val = Color_Green  Then
      WScript.Echo "Green"
     End If
   </script>
 </job>
</package>

Hope this helps.

Willy.

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.