|
-
May 8th, 2007, 05:42 PM
#1
Thread Starter
Hyperactive Member
[2005] Generic List Problem
Hello,
This is my first time using generics, and I am having a problem getting it set up in my application.
I have a class called settings that will store information from a database. I want to take the settings class and store it in a generic list of type settings. The class that inherits from system.collections.generic.list is called databaselist. I want database list to be a shared friend so that it can be shared all over the application, but not beyond the application.
This is what I have for the databaselist class, which isn't much.
Code:
Friend Class DatabaseList : Inherits System.Collections.Generic.List(Of Settings)
Friend Shared Sub AddSettings(ByVal Settings As Settings)
End Sub
End Class
With this code I can expose a method to add settings to the databaselist. The problem is that when I try to type me.add or mybase.add no intellisense.
So, my question is: Am I doing this right at all? Am I missing the boat all together, or am I just getting something mixed up? How do I do this right?
Thank you for your help.
Ben
Using Visual Basic 2005/2008
-
May 8th, 2007, 05:52 PM
#2
Re: [2005] Generic List Problem
I think you may be misunderstanding what Shared means. It has nothing to do with being "shared all over the application", instead it only means that all instances of that class share this part of it. One of the results of that is that there is no meaning to Me.add, because Me means "this instance", but a shared item is common to ALL instances, so there is no meaning to "this instance". You would actually have to use the class name, rather than me:
DatabaseList.Add()
though it appears that you may not actually want it to be shared.
On the other hand, it also sounds like you only ever want one instance running in the program. If that is the case, look up the Singleton construct. If that is NOT the case, then don't look up the Singleton construct.
That's a start, but it will probably just launch a discussion.
My usual boring signature: Nothing
 
-
May 8th, 2007, 06:00 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Generic List Problem
Thank you, Shaggy, for your response.
I will look up the singleton construct. I do want only one instance of the DatbaseList class in the program. In VB6 I would use a module and declare a new instance.
You are right, I did misunderstand shared. It looks like my key problem is how to expose a class to the entire application so that it doesn't have to be created on each use because I want the data to persist over the life of the application.
Thank you for your help.
Ben
Using Visual Basic 2005/2008
-
May 8th, 2007, 06:30 PM
#4
Re: [2005] Generic List Problem
The basis of the Singleton is this:
Make your constructor Private (Private Sub New())
Then add a shared member to the class of the type of the class.....ok, that was a pretty bad description, let's try an example:
Assume the class is of a type called MyClass
vb Code:
Public MyClass
Public shared TheOnlyOne as New MyClass
Private Sub New()
End Class
Since the constructor is declared private, no code can create an instance of the class. However, there IS an instance, which is the shared public instance which is a member of the class. Since it is a shared member, it exists regardless of whether or not there is an instance of the class, which just means (as far as you are concerned) that the variable is created before the program actually starts (I think this is literaly true, in that all shared items are created before Sub Main actually executes). Since the variable in this case, is an instance of the class, then you have one instance of the class created before the program starts, and NO other instance can be created because the constructor is inaccessible.
To use this, you can do many things:
1) you can just use the instance by calling MyClass.TheOnlyOne.<whatever function or property>
2) You can make a reference to it:
Private localCopy as MyClass = MyClass.TheOnlyOne.
Note that this doesn't make a new instance of MyClass, but localCopy just holds a local reference to the single existing instance.
Other than that, the class should look like any other class, with Public properties, functions and subs as needed.
My usual boring signature: Nothing
 
-
May 8th, 2007, 07:11 PM
#5
Thread Starter
Hyperactive Member
Re: [2005] Generic List Problem
Thank you for that explanation, Shaggy. That makes perfect sense the way you described it. The only thing that seems a little wierd is that in your example, the class is creating an instance of itself, but in the context of your first post about shared items, it makes sense.
Ben
Using Visual Basic 2005/2008
-
May 8th, 2007, 10:20 PM
#6
Re: [2005] Generic List Problem
The first time I saw that, I thought it was a bug. It seemed totally irrational that an instance should contain an instance. Wouldn't that lead to an infinite recursion?
However, since it is a shared member, it is handled differently. These are actually pretty cool constructs. That leaves me with only one question: Is Singleton a guys name, or is it called that because it creates only a single instance (in which case, the capitalization is doubtful)?
My usual boring signature: Nothing
 
-
May 8th, 2007, 10:31 PM
#7
Re: [2005] Generic List Problem
It's called "Singleton" because it creates a single instance, and it is capitalised because its a proper name, i.e. the Singleton pattern.
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
|