Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. interfaces and GUIDS

interfaces and GUIDS

Scheduled Pinned Locked Moved C#
helpquestiondelphi
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jinwah
    wrote on last edited by
    #1

    Can anyone help? I want to code an interface and assign a GUID to it. public interface IMyInterface { } On the web I found articles that say I should put the interfaces GUID above the interface declaration. But I get an error message saying GUID is not recognised. My using clause has System. So how can I do this? The reason is in Delphi my objects support interfaces and then by zooming thru a list of objects I can ask if the object supports the interface and if so talk to the object thru that interface. I have had a good look thru my books and internet articles but cannot find any really good info. If there is a new way to do this I would also be interested. Thanks Luke

    H 1 Reply Last reply
    0
    • J Jinwah

      Can anyone help? I want to code an interface and assign a GUID to it. public interface IMyInterface { } On the web I found articles that say I should put the interfaces GUID above the interface declaration. But I get an error message saying GUID is not recognised. My using clause has System. So how can I do this? The reason is in Delphi my objects support interfaces and then by zooming thru a list of objects I can ask if the object supports the interface and if so talk to the object thru that interface. I have had a good look thru my books and internet articles but cannot find any really good info. If there is a new way to do this I would also be interested. Thanks Luke

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Yes, you do use the GuidAttribute but you must use the right namespace: System.Runtime.InteropServices. You can either include it toward the top with the using statement, or prefix your GuidAttribute (or any class for that matter) with the proper namespace. You must also be referencing the assembly in which the Type contains. I'm only mentioning this so you understand. The System.Runtime.InteropServices.GuidAttribute Type is in the mscorlib.dll assembly, so it is automatically referenced by the compiler (unless it's told otherwise, which is only possible with the command-line compiler).

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      J 1 Reply Last reply
      0
      • H Heath Stewart

        Yes, you do use the GuidAttribute but you must use the right namespace: System.Runtime.InteropServices. You can either include it toward the top with the using statement, or prefix your GuidAttribute (or any class for that matter) with the proper namespace. You must also be referencing the assembly in which the Type contains. I'm only mentioning this so you understand. The System.Runtime.InteropServices.GuidAttribute Type is in the mscorlib.dll assembly, so it is automatically referenced by the compiler (unless it's told otherwise, which is only possible with the command-line compiler).

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        J Offline
        J Offline
        Jinwah
        wrote on last edited by
        #3

        is that above the interface declaration System.Runtime.InteropServices.GuidAttribute["....."] public interface IMyinterface { } And how can I ask an object if it supports the interface so that I can talk to it soley thru that interface (this is how I would have done it in Delphi) Cheers Luke

        H 1 Reply Last reply
        0
        • J Jinwah

          is that above the interface declaration System.Runtime.InteropServices.GuidAttribute["....."] public interface IMyinterface { } And how can I ask an object if it supports the interface so that I can talk to it soley thru that interface (this is how I would have done it in Delphi) Cheers Luke

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          No offense meant, but you should probably read the C# specification. Attributes are like so (fully-qualified example):

          [System.Runtime.InteropServices.Guid("363EBCCC-5E9A-4e14-8A5D-F0DA44E69EF9")]
          public interface IMyInterface
          {
          {

          In C# (and some other languages, but you have to read their specs to find out), you can drop the "Attribute" at the end of attributes. If you want to use properties that aren't in the constructor parameters, fill all the necessary params then use PropertyName=Value, like so:

          [MadeUpAttribute("somestring", SomeParam="SomeValue")]
          public class SomeClass
          {
          }

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          J 1 Reply Last reply
          0
          • H Heath Stewart

            No offense meant, but you should probably read the C# specification. Attributes are like so (fully-qualified example):

            [System.Runtime.InteropServices.Guid("363EBCCC-5E9A-4e14-8A5D-F0DA44E69EF9")]
            public interface IMyInterface
            {
            {

            In C# (and some other languages, but you have to read their specs to find out), you can drop the "Attribute" at the end of attributes. If you want to use properties that aren't in the constructor parameters, fill all the necessary params then use PropertyName=Value, like so:

            [MadeUpAttribute("somestring", SomeParam="SomeValue")]
            public class SomeClass
            {
            }

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            J Offline
            J Offline
            Jinwah
            wrote on last edited by
            #5

            No offence taken. I have brought 2 very good reference texts for C# / .NET. My problem is that I professionally program in Delphi. So I know what I want to do but I haven't yet got used to framing the question I want to ask in C# terms to get the answer. But moving to C# has been very easy due to the similar class libraries so I have a head start on my fellow C++ m8s. Thanks for your help tho.

            H 1 Reply Last reply
            0
            • J Jinwah

              No offence taken. I have brought 2 very good reference texts for C# / .NET. My problem is that I professionally program in Delphi. So I know what I want to do but I haven't yet got used to framing the question I want to ask in C# terms to get the answer. But moving to C# has been very easy due to the similar class libraries so I have a head start on my fellow C++ m8s. Thanks for your help tho.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              My comment wasn't in reference to how you "framed" your question, but rather your syntax. Attributes in C# are enclused in square brackets ([ and ]) and you tried using them for your constructor params, which are always parens (( and )). This is just a simple matter of syntax. While it's true that the syntax (i.e., language) you use to write your code for .NET isn't really so important (knowing how to use and extend the base class library, and how to make your own libraries and applications is more important), you still need to know the syntax of the language you've choosen. I was just pointing out that you should read over the C# language specifications to know exactly what the syntax is - you might even learn a few tricks that aren't always discussed in books (like using the using statement to automatically dispose IDisposable implementations, or to use the same statement to creat a Type alias (especially handy when you have Type collisions, i.e. two different Types with the same name that belong to two or more namespaces you've imported).

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              U 1 Reply Last reply
              0
              • H Heath Stewart

                My comment wasn't in reference to how you "framed" your question, but rather your syntax. Attributes in C# are enclused in square brackets ([ and ]) and you tried using them for your constructor params, which are always parens (( and )). This is just a simple matter of syntax. While it's true that the syntax (i.e., language) you use to write your code for .NET isn't really so important (knowing how to use and extend the base class library, and how to make your own libraries and applications is more important), you still need to know the syntax of the language you've choosen. I was just pointing out that you should read over the C# language specifications to know exactly what the syntax is - you might even learn a few tricks that aren't always discussed in books (like using the using statement to automatically dispose IDisposable implementations, or to use the same statement to creat a Type alias (especially handy when you have Type collisions, i.e. two different Types with the same name that belong to two or more namespaces you've imported).

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                U Offline
                U Offline
                User 260964
                wrote on last edited by
                #7

                Im my opinion, a good example of the uses of attributes is when it comes to describing properties for the property explorer in Visual Studio.net.

                using System;
                using System.ComponentModel;

                namespace Examples
                {
                public class ExampleControl : UserControl
                {
                private IndentStyle m_eExampleProperty = IndentStyle.Smart;
                [
                Category("Design"),
                Description("Gets/sets an example value."),
                DefaultValue(IndentStyle.Smart),
                Browsable(true)
                ]
                public IndentStyle ExampleProperty
                {
                get
                {
                return m_eExampleProperty;
                }
                set
                {
                m_eExampleProperty = value;
                }
                }
                }
                }

                This would produce this kind of output:

                If you want to try this out, just cut-and-paste the code into a source file, and create an instance of the control on the designer form. - Daniël Pelsmaeker

                Microsoft is to quality software what McDonalds is to gourmet cooking

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups