Question Easy class scope question

gpgarber

New member
Joined
Nov 30, 2009
Messages
3
Programming Experience
10+
Forgive me if this seems simple. I'm an old VB6 programmer new to OOP.

I have a class thus:
Public Class GetRandom
Public seed As Integer
Public BeenThereDoneThat As Boolean

Property pSeed() As Integer
Get
Return seed​
End Get
Set(ByVal xSeed As Integer)
seed = xSeed​
End Set​
End Property
Public Sub GetMeMyRndNums(ByRef x As Byte, ByRef y As Byte)
Dim randomclass
If Not BeenThereDoneThat Then
randomclass = New Random(pSeed)
BeenThereDoneThat = True​
End If

x = randomclass.Next(1, 4)
y = randomclass.Next(1, 4)​
End Sub

Public Sub New(ByVal ScreenSeed As Integer)
pSeed = CInt(ScreenSeed)
BeenThereDoneThat = False​
End Sub​
End Class

My parent form invokes method GetMeMyRndNums, but obviously the scope of randomclass is Disposed each time, and thus I do not have my Random(pseed) available. How can I "keep" randomclass within the scope of my parent form? I have thought about moving the class within the Form, but is there a better method.

Thanks,
Gary
 
Last edited:
Nesting the class in the form is irrelevant to the scope of a variable that refers to an object of that type.

I don't really see the point of that class at all anyway. Why can't you just use a Random object in your form as is? Just assign a Random object to a member variable in your form and then call its Next method when you need a number?

Regardless, if you want a variable to persist its value between method calls and/or access it in multiple methods then you declare it as a member, i.e. outside of all the methods, rather than a local, i.e. inside a method.
 
Easy class scope quest

Thank you for your reply. Actually, I used to have those methods inside of my form, but I found I needed the constructor to assign a seed value to my Random() class.

I used to have my Dim randomclass = New Random(pSeed) inside of a form event, but my problem is that the seed variable is input from the screen. I don't want to dispose of this random class. I want to keep it active for the entire application, but each time I call the .Next method, I don't want to reseed it, else I will end up with the same random numbers. Does this make sense?
 
Yes and No

As jmcilhinney said "make it a member variable..."

so you might have something like...
VB.NET:
Public Class MyForm

   [B] Dim MyRnd As Random[/B]
    Dim MyX As Byte
    Dim MyY As Byte

    Public Sub New()
        Me.New(Now.Millisecond)
    End Sub

    Public Sub New(ByVal SeedValue As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MyRnd = New Random(SeedValue)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Something has changed get another random numbers bwtween 1 and 3

        MyX = Convert.ToByte(MyRnd.Next(1, 4))
        MyY = Convert.ToByte(MyRnd.Next(1, 4))

    End Sub

    Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get initial random numbers
        MyX = Convert.ToByte(MyRnd.Next(1, 4))
        MyY = Convert.ToByte(MyRnd.Next(1, 4))

    End Sub
End Class
 
Thank you both so much. I think the point I didn't emphasise is that the "seed" value is to be entered on my form. I just need to find an event to instantiate the NEW Random class. I didn't want to keep re-intantiating the Random each time. I used txtbxSeed_Validated for this, and declared my Random class outside of any SUBS (which I guess is what you guys are calling a member variable).
Thank you again,
Gary
 
Back
Top