Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.visual.basic    |    MS Visual Basic discussions, NOT dot-net    |    10,840 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 10,235 of 10,840    |
|    Ralph to All    |
|    Re: Passing Variables.    |
|    14 Sep 06 12:35:11    |
   
   From: nt_consulting64@yahoo.com   
      
   Here is a slightly more complex method using a collection. Which would be   
   more adaptable to changes. Provided more for illustration than as a solution   
   for something as simple as described.   
      
   [Warning!   
    Most error handling has been removed.   
    Beware of line wraps.   
    The original code has been tested but not this edited version.   
   ]   
      
   'Three forms as you outlined, but I added a button could have just used   
   _Close   
   ' modMain.bas   
   ' This uses passing the CStore to each Form before show   
   ' You could just as easily let CStore remain a global, but this shows how   
   you   
   ' can have multiple stores and not have to rework the forms.   
   Option Explicit   
   Private clsStore As CStore   
   Public Sub main()   
    Set clsStore = New CStore   
    Dim ff1 As Form1: Set ff1 = New Form1   
    ff1.Init clsStore : ff1.Show vbModal   
    Dim ff2 As Form2: Set ff2 = New Form2   
    ff2.Init clsStore : ff2.Show vbModal   
    Dim ff3 As Form3: Set ff3 = New Form3   
    ff3.Init clsStore : ff3.Show vbModal   
   End Sub   
   ' end modMain   
   ''   
   ' Form1.frm   
   Option Explicit   
   Private mCStore As CStore   
    ' hard-coded for each question/form   
   Private mMyQuestion As Long   
   Private mOption As Long   
   Public Sub Init(cls As CStore)   
    Set mCStore = cls   
    Debug.Assert Not mCStore Is Nothing   
   End Sub   
   Private Sub Form_Load()   
    mMyQuestion = 1 ' might be const   
   End Sub   
   Private Sub Form_Unload(Cancel As Integer)   
    Set mCStore = Nothing   
   End Sub   
   Private Sub Option1_Click(Index As Integer)   
    mOption = Index   
   End Sub   
   Private Sub cmdDone_Click()   
    mCStore.QuestionAdd mMyQuestion, Label1.Caption,   
   Option1(mOption).Caption, mOption   
    Unload Me   
   End Sub   
   ' end Form1   
   ''   
   ' Form2.frm   
   Option Explicit   
   Private mCStore As CStore   
   Private mMyQuestion As Long   
   Private mOption As Long   
   Public Sub Init(cls As CStore)   
    Set mCStore = cls   
    Debug.Assert Not mCStore Is Nothing   
   End Sub   
   Private Sub Form_Load()   
    mMyQuestion = 2   
   End Sub   
   Private Sub Form_Unload(Cancel As Integer)   
    Set mCStore = Nothing   
   End Sub   
   Private Sub Option1_Click(Index As Integer)   
    mOption = Index   
   End Sub   
   Private Sub cmdDone_Click()   
    mCStore.QuestionAdd mMyQuestion, Label1.Caption,   
   Option1(mOption).Caption, mOption   
    Unload Me   
   End Sub   
   ' end Form2   
   ''   
   ' Form3   
   ' Just one label   
   Option Explicit   
   Private mCStore As CStore   
   Public Sub Init(cls As CStore)   
    Set mCStore = cls   
   End Sub   
   Private Sub Form_Load()   
    Label1.Caption = mCStore.GetAllAnswers()   
   End Sub   
   Private Sub Form_Unload(Cancel As Integer)   
    Set mCStore = Nothing   
   End Sub   
   ' end Form3   
   ''   
   ' Class CQuestion   
   ' collects the question, the answer, and the option   
   ' the option is useful if you have to go back and have   
   ' the form to show previous values.   
   Public QuestionNum As Long   
   Public QuestionStr As String   
   Public AnswerStr As String   
   Public AnswerOpt As Long   
   ' end CQuestion   
   ''   
   ' Class colQuestions   
   Private mCol As Collection   
   Public Function Add(nQuestion As Long, QuestionStr As String, sAnswer As   
   String, lOption As Long) As CQuestion   
    'create a new object   
    Dim objNewMember As CQuestion   
    Set objNewMember = New CQuestion   
    'set the properties passed into the method   
    objNewMember.QuestionNum = nQuestion   
    objNewMember.QuestionStr = QuestionStr   
    objNewMember.AnswerStr = sAnswer   
    objNewMember.AnswerOpt = lOption   
    mCol.Add objNewMember, "Q" & CStr(nQuestion)   
    'return the object created   
    Set Add = objNewMember   
    Set objNewMember = Nothing   
   End Function   
    ' Index is always the question number   
   Public Property Get Item(vntIndexKey As Long) As CQuestion   
    Set Item = mCol("Q" & vntIndexKey)   
   End Property   
   Public Property Get Count() As Long   
    Count = mCol.Count   
   End Property   
   Public Sub Remove(vntIndexKey As Long)   
    mCol.Remove "Q" & vntIndexKey   
   End Sub   
   Public Property Get NewEnum() As IUnknown   
    Set NewEnum = mCol.[_NewEnum]   
   End Property   
   Private Sub Class_Initialize()   
    Set mCol = New Collection   
   End Sub   
   Private Sub Class_Terminate()   
    Set mCol = Nothing   
   End Sub   
   ' end colQuestions   
   ''   
   ' Class CStore   
   Private mcolQuestions As colQuestions   
   Public Sub QuestionAdd(nQuestion As Long, sQuestion As String, sAnswer As   
   String, lOption As Long)   
    mcolQuestions.Add nQuestion, sQuestion, sAnswer, lOption   
   End Sub   
   Public Function GetAnswer(ByVal nQuestion As Long, sQuestion As String,   
   sAnswer As String, lOption As Long) As Boolean   
    Dim cls As CQuestion   
    Set cls = mcolQuestions.Item(nQuestion)   
    nQuestion = cls.QuestionNum   
    sQuestion = cls.QuestionStr   
    sAnswer = cls.AnswerStr   
    lOption = cls.AnswerOpt   
   End Function   
   Public Function GetAllAnswers() As String   
    ' if order is not important then use For...Each   
    Dim sTmp As String   
    Dim lCnt As Long   
    Dim cls As CQuestion   
    For lCnt = 1 To mcolQuestions.Count()   
    Set cls = mcolQuestions.Item(lCnt)   
    sTmp = sTmp & cls.AnswerStr & vbCrLf   
    Set cls = Nothing ' redundant   
    Next lCnt   
    GetAllAnswers = sTmp   
   End Function   
   Private Sub Class_Initialize()   
    Set mcolQuestions = New colQuestions   
   End Sub   
   Private Sub Class_Terminate()   
    Set mcolQuestions = Nothing   
   End Sub   
      
   -ralph   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca