-
May 3rd, 2014, 01:31 PM
#1
Thread Starter
Hyperactive Member
[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?
-
May 3rd, 2014, 01:43 PM
#2
Addicted Member
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.
-
May 3rd, 2014, 02:46 PM
#3
Thread Starter
Hyperactive Member
Re: Time to learn classes...
Originally Posted by Atenk
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
-
May 3rd, 2014, 09:23 PM
#4
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.
-
May 3rd, 2014, 09:58 PM
#5
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.
-
May 3rd, 2014, 10:10 PM
#6
Thread Starter
Hyperactive Member
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.
-
May 4th, 2014, 02:22 AM
#7
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?
-
May 4th, 2014, 10:05 AM
#8
Thread Starter
Hyperactive Member
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.
-
May 4th, 2014, 11:23 AM
#9
Re: Time to learn classes...
Originally Posted by Grags
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
-
May 4th, 2014, 07:43 PM
#10
Thread Starter
Hyperactive Member
Re: Time to learn classes...
Last edited by Grags; May 4th, 2014 at 08:17 PM.
-
May 5th, 2014, 01:40 AM
#11
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.
-
May 5th, 2014, 01:52 AM
#12
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|