none
Properties in Class RRS feed

  • Question

  • Hey

    Probably a stupid question but something ive never quite understood (new to more complicated classes)...

    I have set up a class with a series of Set Get properties like this (only one here for the example)

    Public Class MySettings
       Private ReadOnly objReg As New GetSetRegistry
    
       Friend Property ApplyMaterial As Boolean
          Set(ByVal value As Boolean)
             objReg.WriteValue("ApplyMaterial", value)
          End Set
          Get
             Return objReg.ReadValue("ApplyMaterial")
          End Get
       End Property
    
       Public Sub New()
    
       End Sub
    End Class
    

    GetSetRegistry is another Class that has the Read / Write functions

    In my form, i call the class like this

    Friend GetSetProps As MySettings = New MySettings

    and then will be wanting to Get Property values like this

    Dim GetMats As String = GetSetProps.ApplyMaterial

    and set the property like this

    Dim Whatever As String = "SomeMaterial"
    GetSetProps.ApplyMaterial = Whatever

    My Question is, with the structure above, do the properties get loaded as i call New even though there isnt a method call within the New method? In other words, by simply creating a new instance on the class, are all of the properties populated or do i need to call something from the New() method to invoke the population?

    Like i said, probably a stupid question to some but these are quite new to me.

    Thanks


    Im a self taught VB.Net guy who writes code for Autodesk Inventor. I may not know the terminology but i try so please be patient. Im not a kid so please dont treat me like one :)

    Friday, June 26, 2020 5:31 AM

Answers

  • Hello,

    First off there is no need for a new constructor if there is nothing to be done as in this case. With the following class regKey is initialized in a new constructor created by Visual Studio. If you put a breakpoint on regKey and run the code you will see that is the first line to execute. Note also how the get/set are coded, value in the set is implied similar to how the new constructor is implied.

    Public Class Setting
        Private regKey As RegistryKey =
                    Registry.LocalMachine.
            OpenSubKey("SOFTWARE\MyApp", True)
    
        Public Property ApplicationName() As String
            Get
                Return regKey.GetValue("AppName", "(not set)").ToString()
            End Get
            Set
                regKey.SetValue("AppName", Value)
            End Set
        End Property
    End Class

    Sample code

    Dim setting = New Setting
    
    If setting.ApplicationName = "(not set)" Then
        setting.ApplicationName = "Karen app"
    End If
    
    Console.WriteLine(setting.ApplicationName)

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by NachoShaw Saturday, June 27, 2020 6:26 PM
    Friday, June 26, 2020 12:33 PM
    Moderator

All replies

  • Hello,

    First off there is no need for a new constructor if there is nothing to be done as in this case. With the following class regKey is initialized in a new constructor created by Visual Studio. If you put a breakpoint on regKey and run the code you will see that is the first line to execute. Note also how the get/set are coded, value in the set is implied similar to how the new constructor is implied.

    Public Class Setting
        Private regKey As RegistryKey =
                    Registry.LocalMachine.
            OpenSubKey("SOFTWARE\MyApp", True)
    
        Public Property ApplicationName() As String
            Get
                Return regKey.GetValue("AppName", "(not set)").ToString()
            End Get
            Set
                regKey.SetValue("AppName", Value)
            End Set
        End Property
    End Class

    Sample code

    Dim setting = New Setting
    
    If setting.ApplicationName = "(not set)" Then
        setting.ApplicationName = "Karen app"
    End If
    
    Console.WriteLine(setting.ApplicationName)

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by NachoShaw Saturday, June 27, 2020 6:26 PM
    Friday, June 26, 2020 12:33 PM
    Moderator
  • Karen,

    You know I stop answering persons who ask something while they think they know it better. You told once use useful names. In this thread this goes wrong by the OP and gives the idea he or she does not well know the difference between a method and a property (both members). 


    ApplyMaterial can be a Name for a method

    AppliedMaterial can be a Name for a property


    Success
    Cor

    Saturday, June 27, 2020 5:58 PM
  • Cor

    Once again, your reply bears no relevance to the question and you have posted either just provoke a response or because you're an asshole.

    We have had these discussions before. You persist in being antagonistic and have a thing about either trying to call me out or point out that i dont know what im doing. I am here in a forum asking questions so that in itself is claiming that maybe i dont know hence the questions. What i dont do is pass comment to other users, insult them or make comments for lack of knowledge.

    so to be absolutely 100% clear:

    I DO NOT REQUIRE ANY INPUT OR ADVICE FROM YOU!

    As much as i dont know the code world implicitly, i am learning and take my advice graciously to whoever is kind enough to give it. The fact that i may or may not know the difference between things confirms the very reason why i am here which is to ask & learn. If i knew everything, i wouldnt have need to ask anything.

    Why are you here? to piss me off? It must be great to sit behind a keyboard and virtually mouth off at your content, Keyboard warriors are like that... In real life you're probably mouse like who still lives at home with his Mother.

    Seeing as there is nothing i can do to prevent you from posting on my threads, i'll reach out to the admins team to see if they can have a word on my behalf.

    Now in the wise words of Robert Weildon - "just because you take yourself seriously, it doesnt mean that i have to"

    Cor, go away


    Im a self taught VB.Net guy who writes code for Autodesk Inventor. I may not know the terminology but i try so please be patient. Im not a kid so please dont treat me like one :)

    Saturday, June 27, 2020 6:26 PM
  • Hi Karen

    Thanks for the reply, that helps me understand much more now.

    Appreciate the time and effort in replying

    Thanks

    Nacho


    Im a self taught VB.Net guy who writes code for Autodesk Inventor. I may not know the terminology but i try so please be patient. Im not a kid so please dont treat me like one :)

    Saturday, June 27, 2020 6:27 PM