0

I have a class list (based on elements from another class) that I need to index. However I'm not sure how to make the enumerator accessible to the other classes, the main form and the nested classes. Here's the code I have:

The class:

Public Class Episodes
    Inherits Form1

    Public Shared _Episodes As List(Of Events)
    Public Shared Property Episodes() As List(Of Events)
        Get
            Return _Episodes
        End Get
        Set(value As List(Of Events))
            _Episodes = value
        End Set
    End Property

    Public Sub New(ByVal Episodes As List(Of Events))
        _Episodes = New List(Of Events)
        For Each Events As Events In S
            For i As Integer = 0 To S.Count - 2
                _Episodes.Add(S(i))
                _Episodes.Add(S(i + 1))
            Next
        Next
    End Sub

    ' ...
End Class

The Enumerable:

Public Shared j As Integer = Enumerable.Count(Episodes, Function(Events) Events.EventTime)

Is there a way to do this so that any part of the code can call on the Enumerable, i.e. access Episodes(S(i)) or Episodes(S(i+1))?

1 Answer 1

0

This seems to be a bit of a mess right now. Each time an Episodes objects is created, it's constructor (Sub New) overwrites the shared _Episodes field with a new list and then repopulates it from S (whatever that is). The _Episodes list is already shared globally via the Shared property called Episodes, but the list doesn't actually get populated until the first time an Episodes object is created. And then, as I said before, it gets overwritten and repopulated with each successive Episode object creation.

So, the simple answer to your question is, you already can access it globally, but it's probably not working exactly the way you are intending it to work. Since it's a shared property of a class, when you use it from other classes, you have to prefix it with the class name, like this:

Episodes.Episodes(S(i))

If you don't want to have to specify the class name, you could put the list in a module instead in a class. For instance, if you created a module like this:

Module Module1
    Public Property Episodes As List(Of Events)
End Module

Then you could use it anywhere in the project without prefixing it with the module name, for instance:

Episodes(S(i))

However, I wouldn't really recommend any of that, per se, sincew there are three problems which I see with your design:

  • You are re-creating the list each time an Episodes object is created
  • The property will be Nothing until the first Episodes object is created
  • The episodes are stored in a global variable

(There are many many explanations online as to why global state and singletons are best avoided, so I won't rehash it all here. If you don't know why globals are bad, I recommend doing some research on the topic)

3
  • Steven, Thanks for the help. So S is a list of Events, and what I'm trying to create is a list of pairs(episodes) from S. So if S a list of events = (A,1), (B,2), (C,3), (D,4); then a list C of episodes, would be ((A,1)(B,2)), ((B,2)(C,3)), ((C,3)(D,4)). All of this is based on events loaded through a textbox. I understand the issue with singletons, and I can't seem to find a way around it with the algorithm I trying to recreate, as the class Episodes(and the enumerable) has quite a few dependencies. Based on that, do you think there's a better way of building this? I appreciate the help. Apr 3, 2014 at 10:42
  • I'm sure there are some situations where a global is necessary, but they are very far and few between. The only time, that I can think of, off the top of my head, where you really need a global, is when you must have only one object that acts as your communication interface to some solitary piece of hardware or a third party process, or something like that. But even then, there are better ways to architect it as a singleton than just declaring it as a global variable in a module. Apr 3, 2014 at 12:33
  • There are very few times where globals are used where the same thing couldn't be implemented in a much better way with dependency-injection (DI) instead of globals. Apr 3, 2014 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.