locked
Help with Set and Get Property Statements and their Array Variables in a Class - Looking for best and right way to do this. RRS feed

  • Question

  • TITLE: Help with Set and Get Property Statements and their Array Variables in a Class - Looking for best and right way to do this.

    USING: Windows 7 Professional x64 sp1, Visual Studio 2012 Professional, Visual Basic.NET  

    NOTES: I do not wish to upgrade Windows or Visual Studio.  I am a beginner at VB.NET.

    BACKGROUND: I am not certain how to say or word this, I am not certain what I need.  I have been playing around with the following in combination = Classes, Properties, Arrays/Variables. Through trial and error and some help searching online and asking here I have figured out how to declare my project variables and even set their values.  I now have a number of Property statements, some based on array variables others not.  All of these property statements despite being different ways of doing things, the nuances, they all seem to work.  I am stuck on these property statements, I do not fully understand how or why these all work, the code is a bit hard to understand.  I do not understand the best way or the right way of doing these property statements, either like those I have included, or if there is a better way of doing them I would like to know.  I do not know if all of my property statements are correct and error free, they seem to be error free from running my project but i have no idea.  Things like placement of brackets(), or even lack of brackets, needing to or not needing to pass byVal/Variables to the Property when its called.  This is all a bit confusing to me.

    QUESTION1: Do all my property statements work as intended?

    QUESTION2: Pick of mine or write a better (best, right) way of doing both 1d and 2d properties.

    QUESTION3: If you have anything else constructive or otherwise to say please post it.

    PROJECT CODE BELOW: (I have only included below the code that is relevant to my problem, taken from my project.  I hope there are no errors or anything missing, I tried my best)

    Public Class Game1
        Public Property UnitFriendProp As UnitFriend = New UnitFriend
    
    
        Public Class UnitFriend
    
    
            Private FriendHealth() As Int32 = {100, 111, 100}
    
            Private FriendHealth2 As Int32() = {200, 200, 333}   'both ways seem valid
    
            Private FriendHealth2x() As Int32 = {300, 444, 300}   'both ways seem valid
    
            Private FriendHealth3(,) As Int32 = New Int32(,) {
                {0, 0, 0, 0, 1},
                {0, 0, 0, 1, 1},
                {0, 0, 1, 1, 1},
                {0, 0, 0, 1, 9},
                {0, 0, 0, 7, 1}
    } 'this works too
    
            Private FriendHealthx(,) As Int32 = New Int32(,) {
                {0, 0, 0, 0, 1},
                {0, 0, 0, 1, 1},
                {0, 0, 1, 1, 1},
                {0, 0, 0, 1, 9},
                {0, 0, 0, 7, 1}
    } 'this works too
    
    
            Public Property FriendHealthProp(ByVal index As Int32) As Int32   'works (but not best way)
                Get
                    Return FriendHealth(index)
                End Get
                Set(ByVal value As Int32)
                    FriendHealth(index) = value   'does work but how??? look at how i set it
                End Set
            End Property
    
    
            Public Property FriendHealth2Prop() As Int32()   'works
                Get
                    Return FriendHealth2
                End Get
                Set(ByVal value As Int32())
                    FriendHealth2 = value   'does work
                End Set
            End Property
    
    
            Public Property FriendHealth2xProp() As Int32()   'works
                Get
                    Return FriendHealth2x
                End Get
                Set(ByVal value As Int32())
                    FriendHealth2x = value   'does work
                End Set
            End Property
    
    
            Public Property FriendHealth3Prop() As Int32(,)  'works
                Get
                    Return FriendHealth3
                End Get
                Set(ByVal value As Int32(,))
                    FriendHealth3 = value 'does work
                End Set
            End Property
    
    
            'create a property with byval index1d and index2d
            Public Property FriendHealthxProp(ByVal index1D As Int32, ByVal index2D As Int32) As Int32   'also works (but not best way)
                Get
                    Return FriendHealthx(index1D, index2D)
                End Get
                Set(ByVal value As Int32)
                    FriendHealthx(index1D, index2D) = value   'also does work but how??? look at how i set it
                End Set
            End Property
    
    
            'use the proper naming method/convention for variables with properties.  so make a private variable which would be called _Mynamevar and its property would just be called Mynamevar  - the underscore _ before variable name just means/tells you it is a private variable, make sure to create variables as private when they have a property.
    
        End Class
    End Class

    Public Class FifthWorkaroundClass
    
        Public Sub testitnow()
            MessageBox.Show(mygame.UnitFriendProp.FriendHealthProp(0))
        End Sub
    
        Public Sub testitnow2()
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth2Prop(2))
        End Sub
    
        Public Sub testitnow2x()
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth2xProp(1))
        End Sub
    
        Public Sub testitnow3()
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth3Prop(3, 4))
        End Sub
    
        Public Sub setitnow3()
            mygame.UnitFriendProp.FriendHealth3Prop(3, 4) = 285
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth3Prop(3, 4))
        End Sub
    
        Public Sub setitnow2x()
            mygame.UnitFriendProp.FriendHealth2xProp(1) = 266
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth2xProp(1))
        End Sub
    
        Public Sub setitnow2()
            mygame.UnitFriendProp.FriendHealth2Prop(2) = 225
            MessageBox.Show(mygame.UnitFriendProp.FriendHealth2Prop(2))
        End Sub
    
        Public Sub setitnow()
            mygame.UnitFriendProp.FriendHealthProp(0) = 234
            MessageBox.Show(mygame.UnitFriendProp.FriendHealthProp(0))
        End Sub
    
        Public Sub testitnowx()
            MessageBox.Show(mygame.UnitFriendProp.FriendHealthxProp(0, 0))
        End Sub
    
        Public Sub setitnowx()
            mygame.UnitFriendProp.FriendHealthxProp(0, 0) = 311
            MessageBox.Show(mygame.UnitFriendProp.FriendHealthxProp(0, 0))
        End Sub
    End Class

    Public Class Form1
        Dim myfifthworkaroundclass As New FifthWorkaroundClass
    
    
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            myfifthworkaroundclass.testitnow()
        End Sub
    
        Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            myfifthworkaroundclass.testitnow2()
        End Sub
    
        Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
            myfifthworkaroundclass.testitnow2x()
        End Sub
    
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            myfifthworkaroundclass.testitnow3()
        End Sub
    
        Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            myfifthworkaroundclass.setitnow3()
        End Sub
    
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            myfifthworkaroundclass.setitnow2x()
        End Sub
    
        Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
            myfifthworkaroundclass.setitnow2()
        End Sub
    
        Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
            myfifthworkaroundclass.setitnow()
        End Sub
    
        Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
            myfifthworkaroundclass.testitnowx()
        End Sub
    
        Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
            myfifthworkaroundclass.setitnowx()
        End Sub
    
    End Class

    Module Module1
        Public mygame As New Game1
    
    End Module
    

    Create a Form called Form1.  Create buttons on the form with Button Text as follows...

    Button6 = "FriendHealth"
    Button13 = "Set FriendHealth"
    Button7 = "FriendHealth2"
    Button12 = "Set FriendHealth2"
    Button8 = "FriendHealth2x"
    Button11 = "Set FriendHealth2x"
    Button9 = "FriendHealth3"
    Button10 = "Set FriendHealth3"
    Button14 = "FriendHealthx"
    Button15 = "Set FriendHealthx"


    Saturday, June 13, 2020 11:54 PM

Answers

  • Hello,

    Here are several observations and recommendations.

    The array stuff is personal choice, similar to other things done in your code, if it works for you then use it. PersonallY my recommendations follow C# best practices for my team but when I see Int32 used instead of Integer my guess is you are using an older version of Visual Studio and an earlier version of the .NET Framework.

    Don't use a number in a class name e.g. Game1 should be Game then when using it a variable name would be Game1. FriendHealth versions are personal choice. Properties such as FriendHealthProp should be named FriendHealth. Value in a property set is done by the compiler. Methods in a class like FifthWorkaroundClass should not be calling MessageBox.Show, instead other options are using delegates/events, or using INotifyPropertyChanged Interface or both. Avoid using "my" in variable names, instead of myfifthworkaroundclass  using mfifthworkaroundclass or _fifthworkaround class and would clarify Public or Private rather than Dim, use Dim for inside of functions and procedures.

    if you are setting Button Text property via 

    Button6 = "FriendHealth"

    This is not possible with VB.NET unless you found out something I'm not aware of.


    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



    Sunday, June 14, 2020 12:43 AM

All replies

  • Hello,

    Here are several observations and recommendations.

    The array stuff is personal choice, similar to other things done in your code, if it works for you then use it. PersonallY my recommendations follow C# best practices for my team but when I see Int32 used instead of Integer my guess is you are using an older version of Visual Studio and an earlier version of the .NET Framework.

    Don't use a number in a class name e.g. Game1 should be Game then when using it a variable name would be Game1. FriendHealth versions are personal choice. Properties such as FriendHealthProp should be named FriendHealth. Value in a property set is done by the compiler. Methods in a class like FifthWorkaroundClass should not be calling MessageBox.Show, instead other options are using delegates/events, or using INotifyPropertyChanged Interface or both. Avoid using "my" in variable names, instead of myfifthworkaroundclass  using mfifthworkaroundclass or _fifthworkaround class and would clarify Public or Private rather than Dim, use Dim for inside of functions and procedures.

    if you are setting Button Text property via 

    Button6 = "FriendHealth"

    This is not possible with VB.NET unless you found out something I'm not aware of.


    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



    Sunday, June 14, 2020 12:43 AM
  • I pasted my code in the opening post above into a new empty project and it seemed to work as it always has though i did not test thoroughly.

    I forgot to add that i am using .NET Framework 4.5

    I guess some help understanding my problem would be great.

    Just seen your post Karen, many thanks I will get to that when I can.

    Sunday, June 14, 2020 12:48 AM
  • Hi Karen,

    First, thank you for resposnding,

    Can you link anything to show C# Best Practices, i guess they apply to vb.net aswell?  Else any best practices for vb.net.

    I am actually using Windows 7 x64 bit with service pack one.  I am using visual studion 2012 Professional that i purchased on a disk.  I am using .net framework 4.5 with the downloadable msdn library help files on a disk. My reason for using int32 was to make my application compatible with older operating systems that are 32 bit, at least i am trying to do that.  I have no intention to use windows 10 so i am not aiming for compatability with that, rather i am aiming for compatability with older systems, as backwards as that sounds.

    My naming convention in my project needs some work, i havent addressed that yet, my project is just theories thrown out there.  Thank you for pointing out that i should use Game instead of Game1.  I do know how to correctly name my properties and their private variables, just havent got around to it.

    I do use messagebox.show a lot, I have recently found out how to use the Stop command, in my last que4stion here.  I do not know what delegates/events are.  I can look into INotifyPropertyChanged.   

    That is excellent advice to use Dim only in subs or functions, i just looked up procedure. I was not good at declaring previously.

    I was not trying to set the button text with those last lines, they were not code, more explaining to the user to change the text property of which buttons to make the code work, reason i did not place it in a code block.  This code = Button1.text = "something" i know how to do.  

    I had hope for a further explanation on the different ways of doing properties in my code, though as you said it is not important and i can use any of those methods.  I guess you are saying there is no problems/errors with any of them.

    Again thanks for resposnding Karen, have a great day!
    Sunday, June 14, 2020 6:49 PM
  • In regards to best practices, there is no one document that is right for everyone especially if a developer takes a document “as is” without fully understanding what is said one place then done abstractly later.

    These might be helpful (they are mine and considered starters)

    https://social.technet.microsoft.com/wiki/contents/articles/53398.vb-net-writing-better-code-part-1.aspx

    https://social.technet.microsoft.com/wiki/contents/articles/53576.vb-net-writing-better-code-part-2.aspx

    https://social.technet.microsoft.com/wiki/contents/articles/51216.net-defensive-data-programming-part-1.aspx

    For instance in Microsoft guideline we have (and the same goes for if this was VB.NET)

    if ((val1 > val2) && (val1 > val3))
    {
        // Take appropriate action.
    }

    What is val1, val2 and val3? They may make sense now but not six months from now. Yet something like this is acceptable because since before .NET Borland Delphi (mother of .NET) used T for generic types.

    public static async Task<(T1, T2)> WhenAll<T1, T2>(Task<T1> task1, Task<T2> task2) => (await task1, await task2);

    Now picking the next VB.NET sample at random. There is nothing ambiguous about anything with naming of objects and methods. Note the private variable which starts with an underscore, many VB.NET developers would replace the underscore with m which is old school and stands for module level, it can be used or intermixed with _ it you want. 

    Private _NorthWindContext As NorthWindContext
    It's good to understand how to debug rather than use Stop (have never used it, ever) or MessageBox's. Learn to step through code and examine data. Also delegates and events can be helpful

    Regarding INotifyPropertyChanged, let's say you load a DataGridView with a DataTable (wrong but that is what many do, instead assign a DataTable to a BindingSource then the BindingSource is the DataSource of the DataGridView and in short do research on this) and make a change in the DataGridView the DataTable data is changed too yet if you load a DataGridView with a list and make a change nothing is changed in all cases while with INotifyPropertyChanged this will happen especially when implementing with a BindingList or a BindingList and BindingSource.

    Above is condensed information, to really dig in, this is not the place for that.

    A couple of last things, "Simple is not always best" as many developers seem to think this, "research, research, research" rather than ask forum questions (we like people to ask questions) as you get better at research you will learn and fix things without asking questions and as time goes by this will happen faster and faster. After my first two years of learning I have only asked three questions about coding here and the results, nobody could figure out a proper solution so I want back and dug in and fixed those things myself.

    In closing: You are on the right track, asking about existing code rather than asking how to! Also your code is an excellent start but as commented there is improvements, we all have been there, me, you and every single other coder.


    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

    Sunday, June 14, 2020 11:48 PM
  • Thanks for your thorough reply Karen, Only got around to answering this now.

    Your best practice documents look decent, I took a look at some of them, I understand some parts but others go over my head.  The database stuff was a bit difficult for me, I will put that off until I need to know it.  Some parts I did understand contained very good information, others we have discussed before, some was too complicated for me, i am not so good at reading other peoples code still.  I will continue with these at a later date.

    For now I have decided to read one of my vb.net programming books, hopefully from start to finish, it is a begginer version but i also have some intermediate books to.  I think i need to start from scratch and get some background in the basics, i know already bits and pieces of a number of things and have never fully read a proramming book all the way through, i hope i can change that.

    I will put off learning best practices, how i should write my code etc, but come back to it when i have the basics.  I am still unsure how to write variable names, but will learn that later.  i understand private and public naming conventions, with an underscore before a private variable, i will utilize that, its knowing how to descriptively name variables i do not understand yet.

    A big part of me wants to just rush making a program, however sloppy, i made big strides before.. to hopefully finish a project, using skills i know or can learn.  This should kindle my fire for programming.  If i do this i may be back soon with more questions, after i have exausted my research of a problem.

    Regarding debugging, I have since found the buttons to step into code, I remember them from long ago on distant vb versions of the past, I used to use these methods back then.  I will try this out.

    My memory is not so good, but i will give it my best to learn vb.net.

    Thank your for the inspirational words Karen.
    Wednesday, June 17, 2020 8:41 PM
  • I plan to use a text file as a database, and use writeline?, readline? on it and populate variables that way.  I have done this in the distant past.  It will take away the database headache of learning for now.

    EDIT: I remember back in the day I used breakpoints in my code, you could set them on a line of code in te IDE and execution would stop right there and then you could view the variables values etc and as you say step into the code.

    I understand that simple is not usually better, I have been trying to do things in the simplest, easiest, quickest way possible to get to the end goal.  Where as If i put the time in to learn the hard way it will be easier for me to do it the right way.

    I am still in two minds whether to dedicate my time to learning vb.net or to try and rush a project through till the end using what i currently know and can easily find out through research.  Not sure which to do...

    I am not so good at managing my time, another of my shortcomings.  I even have books from a bundle site on how to get better at that, but if you can guess.. I never read them.

    I have another failure in that I can put things down for a year sometimes more or less before coming back to them, usually if they are too hard.  I think just finishing a vb.net project would do my programming life the world of good, I would be more likely to continue and not put it down for soo long.  Of course I would start very small, an extreemly small game that you could barely call finished being rough around the edges with rubbish AI.  I have for as long as i can recall had a fascination with AI, I think my first sttempt at it will be without reading anything on the subject, using purely my own ideas as to how to do it, just for curiosity sake as t o what i come up with.  I know places on the internet to read up on it for when i want to take it seriously.

    Wednesday, June 17, 2020 8:44 PM