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. Visual Basic
  4. VB.NET Properties

VB.NET Properties

Scheduled Pinned Locked Moved Visual Basic
helpcsharp
15 Posts 5 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.
  • P Offline
    P Offline
    programmervb netc
    wrote on last edited by
    #1

    Is it possible to force a property some how in VB.NET.(For a form) One way I could do this is to use a parameter in the constructor and delete the default constructor, but from what I have been told you are not supposed to modify the windows generated code. I am a noob with properties so any help would be appreciated. BTW the reason I am trying to do all of this is because if we do not have a parameter passed to this form when it is opened then it will not work properly and I would like to make it so that if we call this form without passing that property the compiler generates and error.

    Humble Programmer

    C D 2 Replies Last reply
    0
    • P programmervb netc

      Is it possible to force a property some how in VB.NET.(For a form) One way I could do this is to use a parameter in the constructor and delete the default constructor, but from what I have been told you are not supposed to modify the windows generated code. I am a noob with properties so any help would be appreciated. BTW the reason I am trying to do all of this is because if we do not have a parameter passed to this form when it is opened then it will not work properly and I would like to make it so that if we call this form without passing that property the compiler generates and error.

      Humble Programmer

      C Offline
      C Offline
      Chinners
      wrote on last edited by
      #2

      You should look at the "New" sub. If you put code like the following, then everytime you create an instance the form, all parameters will be required: Public Sub New(Param as String) Blah Blah End Sub Does this work with forms? I think so.

      P 1 Reply Last reply
      0
      • C Chinners

        You should look at the "New" sub. If you put code like the following, then everytime you create an instance the form, all parameters will be required: Public Sub New(Param as String) Blah Blah End Sub Does this work with forms? I think so.

        P Offline
        P Offline
        programmervb netc
        wrote on last edited by
        #3

        Would this not be considered modifying windows generated code? Also the new is the constructor is that correct?

        Humble Programmer

        S 1 Reply Last reply
        0
        • P programmervb netc

          Would this not be considered modifying windows generated code? Also the new is the constructor is that correct?

          Humble Programmer

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          Yes, the New sub is the constructor. You are only adding an additional overloaded constructor which takes the parameters you need. If you want to ensure that only your contructor can be called (and not the default parameterless one) you can make the default constructor be a private function.

          Scott Dorman

          Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


          Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

          P 1 Reply Last reply
          0
          • S Scott Dorman

            Yes, the New sub is the constructor. You are only adding an additional overloaded constructor which takes the parameters you need. If you want to ensure that only your contructor can be called (and not the default parameterless one) you can make the default constructor be a private function.

            Scott Dorman

            Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


            Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

            P Offline
            P Offline
            programmervb netc
            wrote on last edited by
            #5

            But again would that not be modifying form generated code by changing the New sub to be Private?

            Humble Programmer

            D S 2 Replies Last reply
            0
            • P programmervb netc

              Is it possible to force a property some how in VB.NET.(For a form) One way I could do this is to use a parameter in the constructor and delete the default constructor, but from what I have been told you are not supposed to modify the windows generated code. I am a noob with properties so any help would be appreciated. BTW the reason I am trying to do all of this is because if we do not have a parameter passed to this form when it is opened then it will not work properly and I would like to make it so that if we call this form without passing that property the compiler generates and error.

              Humble Programmer

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Maybe I'm missing something. You're trying to create your own Form class, removing the New constructor that doesn't take any parameters, and adding another New constructor that does take a parameter?? Am I correct? You cannot remove the parameterless New method. Doing so will make the form unusable in the form designer since it will only create an instance of your form using the parameterless New method. OK, what's the property you're trying to "force" on this form??

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              P 1 Reply Last reply
              0
              • P programmervb netc

                But again would that not be modifying form generated code by changing the New sub to be Private?

                Humble Programmer

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                No, it's not. There are also pitfalls do removing the New() method, as I've descrbied in my other post. The designer generated code sits in a #Region block specifically designated as "designer generated".

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                1 Reply Last reply
                0
                • P programmervb netc

                  But again would that not be modifying form generated code by changing the New sub to be Private?

                  Humble Programmer

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  As far as I can tell, when you create a new class that inherits from System.Windows.Forms.Form, Visual Studio will not automatically generate a default (parameterless) constructor in the code. The direct answer to your question is that, since the code does not already contain a default (parameterless) constructor, adding a new private default constructor is not changing generated code.

                  Scott Dorman

                  Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                  Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                  D 1 Reply Last reply
                  0
                  • S Scott Dorman

                    As far as I can tell, when you create a new class that inherits from System.Windows.Forms.Form, Visual Studio will not automatically generate a default (parameterless) constructor in the code. The direct answer to your question is that, since the code does not already contain a default (parameterless) constructor, adding a new private default constructor is not changing generated code.

                    Scott Dorman

                    Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                    Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Whoops! My bad. [Edit] Actually, it's in the base Form class. But, you can't remove it if you want the form to show up in the designer. The designer uses only the parameterless constructor to create an instance of the form to show on the design surface. Removing it kills the ability to design the form.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    S 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Maybe I'm missing something. You're trying to create your own Form class, removing the New constructor that doesn't take any parameters, and adding another New constructor that does take a parameter?? Am I correct? You cannot remove the parameterless New method. Doing so will make the form unusable in the form designer since it will only create an instance of your form using the parameterless New method. OK, what's the property you're trying to "force" on this form??

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008

                      P Offline
                      P Offline
                      programmervb netc
                      wrote on last edited by
                      #10

                      I have and existing form that calls another form which were both designed visually. I need to force a client number when calling this form or we cannot retrieve the correct information.

                      Humble Programmer

                      A 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Whoops! My bad. [Edit] Actually, it's in the base Form class. But, you can't remove it if you want the form to show up in the designer. The designer uses only the parameterless constructor to create an instance of the form to show on the design surface. Removing it kills the ability to design the form.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007, 2008

                        S Offline
                        S Offline
                        Scott Dorman
                        wrote on last edited by
                        #11

                        Hmmm...I actually created a blank VB WinForms project and didn't see any constructors, even in the designer generated file (Form1.Designer.vb). I do see it if I look at the compiled code with Reflector. Yes, I found that if I created my own private default constructor the code wouldn't compile, throwing errors in the Application.Designer.vb file.

                        Scott Dorman

                        Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                        Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                        D 1 Reply Last reply
                        0
                        • P programmervb netc

                          I have and existing form that calls another form which were both designed visually. I need to force a client number when calling this form or we cannot retrieve the correct information.

                          Humble Programmer

                          A Offline
                          A Offline
                          Adam Maras
                          wrote on last edited by
                          #12

                          Simply create a instance variable and a parameter in the form class:

                          Private _clientNumber as Integer
                          Public Property ClientNumber() As Integer
                          Get
                          Return _clientNumber
                          End Get
                          Set(ByVal Value As Integer)
                          _clientNumber = Value
                          End Set
                          End Property

                          After you instantiate the form, but before you display it, set this value in the calling code. Then use the value stored in the property when you show the form.

                          P 1 Reply Last reply
                          0
                          • A Adam Maras

                            Simply create a instance variable and a parameter in the form class:

                            Private _clientNumber as Integer
                            Public Property ClientNumber() As Integer
                            Get
                            Return _clientNumber
                            End Get
                            Set(ByVal Value As Integer)
                            _clientNumber = Value
                            End Set
                            End Property

                            After you instantiate the form, but before you display it, set this value in the calling code. Then use the value stored in the property when you show the form.

                            P Offline
                            P Offline
                            programmervb netc
                            wrote on last edited by
                            #13

                            This is actually what we are doing now. What I want to know is there any way that you can say if that does not have a value when the form is shown don't compile throw a compile time error. Like if you did remove the New Sub and made your own with parameters and when you called the class you did not supply the parameter it would blow up.

                            Humble Programmer

                            D 1 Reply Last reply
                            0
                            • P programmervb netc

                              This is actually what we are doing now. What I want to know is there any way that you can say if that does not have a value when the form is shown don't compile throw a compile time error. Like if you did remove the New Sub and made your own with parameters and when you called the class you did not supply the parameter it would blow up.

                              Humble Programmer

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              programmer_vb.net_c++ wrote:

                              What I want to know is there any way that you can say if that does not have a value when the form is shown don't compile throw a compile time error.

                              Not correctly, no. You would check something like this at runtime, not design time. You'd throw an exception if the proper values were not in place.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007, 2008

                              1 Reply Last reply
                              0
                              • S Scott Dorman

                                Hmmm...I actually created a blank VB WinForms project and didn't see any constructors, even in the designer generated file (Form1.Designer.vb). I do see it if I look at the compiled code with Reflector. Yes, I found that if I created my own private default constructor the code wouldn't compile, throwing errors in the Application.Designer.vb file.

                                Scott Dorman

                                Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                                Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #15

                                Scott Dorman wrote:

                                Hmmm...I actually created a blank VB WinForms project and didn't see any constructors, even in the designer generated file (Form1.Designer.vb).

                                I already correct my mistake. It's not in the designer generated file. It's in the base Form class that your Form1 class is inheriting from.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007, 2008

                                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