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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Simulation of VB6 Style Default properties

Simulation of VB6 Style Default properties

Scheduled Pinned Locked Moved C#
csharpquestion
10 Posts 5 Posters 3 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.
  • F Offline
    F Offline
    Fakher Halim
    wrote on last edited by
    #1

    Many developers have been trying to figure out a way to simulate VB6 style default property in C#. I thought of using operator overloads first. Since assignment operator doesn't overload, I used implicit to type cast. MyClass c="Test String";//A constant string directly assigned to any class instance. Console.WriteLine(c); //print it to verify It is implemented in the following class using implicit type casting operator class MyClass{//Has Text Property, as default //Ability to assign strings directly to .Text Property of MyClass -- Like VB6 public static implicit operator MyClass(string s) { MyClass c=new MyClass(); c.Text=s; return c; } private string _Text; public string Text{ get{return _Text; } set { _Text = value; } } public override string ToString() { return Text;} } Does someone know a simpler way? Fakher Halim

    H D J 3 Replies Last reply
    0
    • F Fakher Halim

      Many developers have been trying to figure out a way to simulate VB6 style default property in C#. I thought of using operator overloads first. Since assignment operator doesn't overload, I used implicit to type cast. MyClass c="Test String";//A constant string directly assigned to any class instance. Console.WriteLine(c); //print it to verify It is implemented in the following class using implicit type casting operator class MyClass{//Has Text Property, as default //Ability to assign strings directly to .Text Property of MyClass -- Like VB6 public static implicit operator MyClass(string s) { MyClass c=new MyClass(); c.Text=s; return c; } private string _Text; public string Text{ get{return _Text; } set { _Text = value; } } public override string ToString() { return Text;} } Does someone know a simpler way? Fakher Halim

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

      That's really the only way to do it. There is no "default property" in the Common Type System (CTS), which defines how types and members are defined. So, since MyClass is not a String, you have to use an operator as you've done. Frankly, I think the whole thing is silly. What's so hard about instantiating and assigning the Text property? Using the operator you incur one more call on the call stack.

      Microsoft MVP, Visual C# My Articles

      F S 2 Replies Last reply
      0
      • H Heath Stewart

        That's really the only way to do it. There is no "default property" in the Common Type System (CTS), which defines how types and members are defined. So, since MyClass is not a String, you have to use an operator as you've done. Frankly, I think the whole thing is silly. What's so hard about instantiating and assigning the Text property? Using the operator you incur one more call on the call stack.

        Microsoft MVP, Visual C# My Articles

        F Offline
        F Offline
        Fakher Halim
        wrote on last edited by
        #3

        Those VB6 developers were INSISTING on not using MyClass.Text and just assigning MyClass directly with a string , as they would do in pre-.NET days The classical example was a Label control, where both of following mean the same: lblEnterName.Caption = "Enter full name" lblEnterName = "Enter full name" Fakher Halim

        H 1 Reply Last reply
        0
        • F Fakher Halim

          Many developers have been trying to figure out a way to simulate VB6 style default property in C#. I thought of using operator overloads first. Since assignment operator doesn't overload, I used implicit to type cast. MyClass c="Test String";//A constant string directly assigned to any class instance. Console.WriteLine(c); //print it to verify It is implemented in the following class using implicit type casting operator class MyClass{//Has Text Property, as default //Ability to assign strings directly to .Text Property of MyClass -- Like VB6 public static implicit operator MyClass(string s) { MyClass c=new MyClass(); c.Text=s; return c; } private string _Text; public string Text{ get{return _Text; } set { _Text = value; } } public override string ToString() { return Text;} } Does someone know a simpler way? Fakher Halim

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          I find it a very interesting way of simulating it. But, frankly, I always hated this on VB, as this made the syntax very ambiguous. Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them... :sigh:

          F 1 Reply Last reply
          0
          • F Fakher Halim

            Those VB6 developers were INSISTING on not using MyClass.Text and just assigning MyClass directly with a string , as they would do in pre-.NET days The classical example was a Label control, where both of following mean the same: lblEnterName.Caption = "Enter full name" lblEnterName = "Enter full name" Fakher Halim

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

            I understand what the intentions are, but this isn't VB6 - it's the .NET Framework. Change is inevitable.

            Microsoft MVP, Visual C# My Articles

            1 Reply Last reply
            0
            • D Daniel Turini

              I find it a very interesting way of simulating it. But, frankly, I always hated this on VB, as this made the syntax very ambiguous. Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them... :sigh:

              F Offline
              F Offline
              Fakher Halim
              wrote on last edited by
              #6

              Yes, it is absolutely counter intuitive in today's strongly typed world, but sometime you need to address some irrational requirement. Fakher Halim

              1 Reply Last reply
              0
              • F Fakher Halim

                Many developers have been trying to figure out a way to simulate VB6 style default property in C#. I thought of using operator overloads first. Since assignment operator doesn't overload, I used implicit to type cast. MyClass c="Test String";//A constant string directly assigned to any class instance. Console.WriteLine(c); //print it to verify It is implemented in the following class using implicit type casting operator class MyClass{//Has Text Property, as default //Ability to assign strings directly to .Text Property of MyClass -- Like VB6 public static implicit operator MyClass(string s) { MyClass c=new MyClass(); c.Text=s; return c; } private string _Text; public string Text{ get{return _Text; } set { _Text = value; } } public override string ToString() { return Text;} } Does someone know a simpler way? Fakher Halim

                J Offline
                J Offline
                Jeff Varszegi
                wrote on last edited by
                #7

                Your method does NOT simulate default properties. It creates a new object and resets the handle, which could be disastrous from both a performance and stability standpoint. It's interesting nonetheless. I agree with both Heath Stewart and Daniel Turini: the syntax is awful from an object-oriented perspective anyway, which is why they didn't build it in. This should be one of the times you just say "no", and you have the excellent support of not actually being able to provide the functionality! Regards, Jeff Varszegi

                F 1 Reply Last reply
                0
                • J Jeff Varszegi

                  Your method does NOT simulate default properties. It creates a new object and resets the handle, which could be disastrous from both a performance and stability standpoint. It's interesting nonetheless. I agree with both Heath Stewart and Daniel Turini: the syntax is awful from an object-oriented perspective anyway, which is why they didn't build it in. This should be one of the times you just say "no", and you have the excellent support of not actually being able to provide the functionality! Regards, Jeff Varszegi

                  F Offline
                  F Offline
                  Fakher Halim
                  wrote on last edited by
                  #8

                  I agree. It is just to illustrate how to create a new class instance out of a string. Of course it doesn't have code to add string into an existing instance, nor it has similarity to a VB6 control for that matter. Sure it is a weird syntax -- shocking to any OO person, and that is exactly why I posted this so called VB6 compatibility possibility -- knowing the controversy. Fakher Halim

                  J 1 Reply Last reply
                  0
                  • H Heath Stewart

                    That's really the only way to do it. There is no "default property" in the Common Type System (CTS), which defines how types and members are defined. So, since MyClass is not a String, you have to use an operator as you've done. Frankly, I think the whole thing is silly. What's so hard about instantiating and assigning the Text property? Using the operator you incur one more call on the call stack.

                    Microsoft MVP, Visual C# My Articles

                    S Offline
                    S Offline
                    scadaguy
                    wrote on last edited by
                    #9

                    Heath Stewart wrote: Frankly, I think the whole thing is silly. What's so hard about instantiating and assigning the Text property? Using the operator you incur one more call on the call stack. I couldn't agree more.

                    1 Reply Last reply
                    0
                    • F Fakher Halim

                      I agree. It is just to illustrate how to create a new class instance out of a string. Of course it doesn't have code to add string into an existing instance, nor it has similarity to a VB6 control for that matter. Sure it is a weird syntax -- shocking to any OO person, and that is exactly why I posted this so called VB6 compatibility possibility -- knowing the controversy. Fakher Halim

                      J Offline
                      J Offline
                      Jeff Varszegi
                      wrote on last edited by
                      #10

                      I feel your pain, believe me. I have to admit that it is a fun little problem, one of those ones that always seems just out of grasp. I can't find any way to do it without some modification of the runtime. Regards, Jeff Varszegi

                      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