Results 1 to 12 of 12

Thread: [RESOLVED] Time to learn classes...

  1. #1

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Resolved [RESOLVED] Time to learn classes...

    I have the following 3 sub/Functions.

    KEY:
    lst = List Of String
    RN = Integer
    I = Integer
    LRem (Remove the item from the list)
    LAdd (Add the item to the list)
    L (See if the item is in the list)


    Code:
        Private Sub LRem(ByVal I As Integer, ByVal S As String)
            lst(RN, I).Remove(S)
        End Sub
    Code:
        Private Sub LAdd(ByVal I As Integer, ByVal S As String)
            If lst(RN, I).Contains(S) = False Then
                lst(RN, I).Add(S)
            End If
        End Sub
    Code:
        Private Function L(ByVal I As Integer, ByVal S As String) As Boolean
            If lst(RN, I).Contains(S) Then
                Return True
            Else
                Return False
            End If
        End Function
    I *think* I can add the lot to a single class?

    I was hoping someone could show me how?

  2. #2
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Time to learn classes...

    If I understand well, you can add the lot just below "Public class", or after any other "End sub", NOT after the
    "End sub" of the class.
    Last edited by Atenk; May 3rd, 2014 at 01:50 PM.

  3. #3

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Time to learn classes...

    Quote Originally Posted by Atenk View Post
    If I understand well, you can add the lot just below "Public class", or after any other "End sub", NOT after the
    "End sub" of the class.
    Yea, I know this

    I was wondering if it was easy to convert them into a single Class.

    Thanks for the response nonetheless

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Time to learn classes...

    First things first, what is 'lst' really? It looks like a 2D array from that code but it's hard to say for sure. What exactly does 'RN' represent too?

    You could do with changing some of those horrendous names too.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Time to learn classes...

    {edit: worked on this post while jmcilhinney posted, so some observations/questions are redundant to his}
    You don't convert them to a class, you put them in a class.
    I assume you have then in a class now, i.e. Public Class Form1, but I suppose you could have then in a Module.
    If you don't want them in your Form Class, then either add another Class to your form file, or preferably add a class file (usually hit Ctrl-Alt-C) or chose from the Project menu in the IDE.
    Put the three routines in that class. It looks like you would want to be able to access those three methods from outside the class, so you want to make them Public, not Private.

    If you're going to put the methods in a class, then the data the methods access should normally be part of that class, so you should add a declaration for "lst" in the class, which raises a couple of questions.
    It looks like lst is a two dimensional array of list of string, but when do you dimension the array? Is it fixed at time of creation, or will the bounds change?
    If the bounds need to be dynamic, should it be a list of list of strings instead of 2d array of list?

    You reference RN, but don't pass it, so assume it should be a member of the class. It should be made a property so that you can ensure that it is not set to an illegal value, but in this quick example, I made it public so is accessed directly.
    You should probably look for some proper Class creation tutorials, as the below is probably not the best example and is making some assumptions based on the methods you posted.
    Code:
    Public Class Form1
      Dim my2dList As New myStringList(3, 5)  'Create an instance of the class and the ubounds of the array.
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        my2dList.RN = 2
        my2dList.LAdd(3, "This is a test")
        If my2dList.L(3, "This is a test") Then
          MsgBox("""This is a test"" is in my2dList(2,3)")
        End If
      End Sub
    
    End Class
    
    Public Class myStringList
      Private lst(,) As List(Of String)
      Public RN As Integer
    
      Public Sub New(ByVal d1 As Integer, ByVal d2 As Integer) '
        ReDim lst(d1 , d2)
        For i As Integer = 0 To d1
          For j As Integer = 0 To d2
            lst(i, j) = New List(Of String)
          Next
        Next
      End Sub
    
      Public Sub LRem(ByVal I As Integer, ByVal S As String)
        lst(RN, I).Remove(S)
      End Sub
    
      Public Sub LAdd(ByVal I As Integer, ByVal S As String)
        If lst(RN, I).Contains(S) = False Then
          lst(RN, I).Add(S)
        End If
      End Sub
    
      Public Function L(ByVal I As Integer, ByVal S As String) As Boolean
        If lst(RN, I).Contains(S) Then
          Return True
        Else
          Return False
        End If
      End Function
    End Class
    Last edited by passel; May 3rd, 2014 at 10:05 PM.

  6. #6

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Time to learn classes...

    Thanks a lot Passel. I've had a quick glance through what you've put. It doesn't seem any more different from a Function or Sub at the moment, or I'm just missing the bigger picture.

    I can't really test it out now, as I have a lot to sort out. But at some point tomorrow evening I will definitely take a look.

    Greatly appreciate the help. This site is awesome, you're all very helpful.

    And this time I was able to rep you

    Hmm, just went through it again. You seem to be wiping the Data stored in my lists in this sub...
    Code:
    Public Sub New(ByVal d1 As Integer, ByVal d2 As Integer) '
        ReDim lst(d1 , d2)
        For i As Integer = 0 To d1
          For j As Integer = 0 To d2
            lst(i, j) = New List(Of String)
          Next
        Next
      End Sub
    Once the information is loaded from file. That information is used many many times. Again tho, I'm probably missing the bigger picture.

    First things first, what is 'lst' really? It looks like a 2D array from that code but it's hard to say for sure. What exactly does 'RN' represent too?

    Yes lst is a 2 dimensional array. Public Lst(3000, 100) as List(Of String)
    RN = RoomNumber, represents the room the user is in.
    And I like my naming system so ner!
    Last edited by Grags; May 3rd, 2014 at 10:16 PM.

  7. #7
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Time to learn classes...

    Grags,
    If you like your naming system, then you like your naming system and that's that. However, it doesn't change one simple fact. Maintenance will be a nightmare.
    At the moment, you are writing your programs and you understand the abbreviations perfectly but one day, you will want to change something, or improve your program in some way. That is the time you will regret using this naming system because it will make it vastly more difficult to understand what is happening.
    Take your last snippet as an example:
    Code:
    Public Sub New(ByVal d1 As Integer, ByVal d2 As Integer) '
        ReDim lst(d1 , d2)
        For i As Integer = 0 To d1
          For j As Integer = 0 To d2
            lst(i, j) = New List(Of String)
          Next
        Next
      End Sub
    Now with more descriptive names. Disclaimer here, I have to guess what the variables mean (just as you won't when you come to maintain it)
    Code:
    Public Sub New(ByVal AcrossLimit As Integer, ByVal DownLimit As Integer) '
        ReDim 2DListOfCoOrds(AcrossLimit  , DownLimit )
        For Column As Integer = 0 To AcrossLimit 
          For Row As Integer = 0 To DownLimit 
            2DListOfCoOrds(Column , Row ) = New List(Of String)
          Next
        Next
      End Sub
    Which of these is easier to read? The main idea of Basic is that it can be read almost as English. It seems daft to undo that advantage by not choosing descriptive variable names doesn't it?

  8. #8

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Time to learn classes...

    Thanks Españolita. Appreciate, your time to do a constructive post.

    I was actually thinking to myself as I was lay in bed that no one will understand that my comment was a joke. And therefore I shouldn't have said it. I was tempted to come back online and edit it out. But I was too snuggly.

    EDIT: I would also like to mention that jmcilhinney is clearly a respected member of the community, and advice he/she has to offer is welcomed with open arms.
    Last edited by Grags; May 4th, 2014 at 10:25 AM.

  9. #9
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Time to learn classes...

    Quote Originally Posted by Grags View Post
    I was actually thinking to myself as I was lay in bed that no one will understand that my comment was a joke.
    "so ner" sort of gave it away as a joke, especially since your posts are rational and courteous. However, if in doubt, that's what smilies are for

  10. #10

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Time to learn classes...

    Hi good evening

    Passel, I have added the code to my project. But I think I need to take your advice and search for some tutorials. Because at the moment, I can't figure it out.

    I understand the Button in there is for testing purposes.
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        my2dList.RN = 2
        my2dList.LAdd(3, "This is a test")
        If my2dList.L(3, "This is a test") Then
          MsgBox("""This is a test"" is in my2dList(2,3)")
        End If
      End Sub
    But when I try to use that snippet anywhere else I get error. Even if I change...

    Private my2dList As New myStringList(3, 5)
    To
    Public my2dList As New myStringList(3, 5)

    I will have a browse on youtube and see what I can find on the subject. And will try again.

    Thanks again for all the help


    EDIT: I think this is 1 of those things I'm never going to understand. I've watched a couple of youtube vids. I actually watched an hour long 1 not that long ago. But I just don't get it.
    Last edited by Grags; May 4th, 2014 at 08:17 PM.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Time to learn classes...

    You probably don't need or want a class in this case anyway.
    To me, the primary two reasons to create a class are first, you want to create multiple instances of an object, like players in a game, or enemies, or obstacles, etc. Second, is for code reuse, i.e. reusing a class in future programs. There are plenty of other use cases, I'm sure, but those are the two main reasons for me.

    If you only want one this one big two dimensional array of string lists, to track something, then a class is probably not necessary.
    But a large too dimensional array of lists seems suspect itself.
    You mention the 3000 is the number of rooms, so each room can have 100 lists of strings.
    In that scenario, the 3000 rooms is where I would think a class might be traditionally used.
    You would have a class that provided the properties and methods needed to describe and manipulate a room object.
    Then you could create as many instances of the room object as you needed.
    If you used a list, then you could add rooms at any time.
    But, if you understand your structures and how to access them as you already have them, and it is a struggle to understand classes, I would say don't worry about classes for now. Finnish your current project and revisit classes in the future, when you can start a less ambitious project more oriented toward learning classes, rather than trying to convert some big existing project to using classes in order to learn about classes.

  12. #12

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Time to learn classes...

    Thank you Passel. You always give descriptive and friendly advice. And in this case, I think you're right. I'm going to skip classes for the now, and come back to them later on in life.

    Many thanks again.

    and as usual :/

    You must spread some Reputation around before giving it to passel again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width