How to access hashtable declared and instantiated in one class from another?

candice

Active member
Joined
Jan 23, 2007
Messages
30
Programming Experience
Beginner
Hi guys,
I have a question here confused me for a long time.
say, if I declare and instantiate a hashtable in Class1, and I wanna use it in Class 2. what should I do? I understand that in Class2 I need to declare the hashtable and get the hashtabel instantiated in Class1. But how can I do it?
following is part of my code.
Class1:
VB.NET:
Imports Excel = Microsoft.Office.Interop.Excel 
Imports System.Collections 
Public Class ButtonTable 
    Inherits System.Collections.CollectionBase 
    Private ReadOnly HostForm As System.Windows.Forms.Form 
    Dim Z1 As String 
    Dim Z2 As String 
    Public ht As Hashtable 
    Public aButton As New System.Windows.Forms.Button() 

    Public Sub AddNewButton() 
        ' Create a new instance of the Button class. 

        Dim i As Integer 
        Dim oXLApp As Excel.Application 'Declare the object variables 
        Dim oXLBook As Excel.Workbook 
        Dim oXLSheet As Excel.Worksheet 

        oXLApp = New Excel.Application 'Create a new instance of Excel 
        oXLBook = oXLApp.Workbooks.Open("C:\Documents and Settings\intern\My Documents\yoyoyo.xls") 
        'Open an existing workbook 
        oXLSheet = oXLBook.Worksheets(1) 'Work with the first worksheet 
        For i = 1 To 100 
            Z1 = oXLSheet.Cells.Item(i, 5).Value 
            Z2 = oXLSheet.Cells.Item(i, 6).Value 
            Dim aButton As New System.Windows.Forms.Button() 
            ' Set intial properties for the button object. 
            aButton.Text = Z1 
            aButton.Name = Z2 
            Dim ht As New Hashtable() 
            ht.Add(aButton.Name, aButton) 
        Next i 

        oXLApp.Visible = False 'Don't show it to the user 
        oXLBook.Save() 
        ' Ensure that the save command is fully completed 
        Do While Not oXLBook.Saved 
            Application.DoEvents() 
        Loop 
        ' Then close the Workbook 
        oXLBook.Close() 
    End Sub 
End Class

Class2:
VB.NET:
Imports Microsoft.Office.Interop 
Imports System.Collections 

Public Class ButtonShow 
    Inherits System.Windows.Forms.Form 
    Dim datafrm As Form2 
    ' Declare a new ButtonTable object. 
    Dim MyControlTable As ButtonTable 

  Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
  Dim ht As Hashtable 
        Dim Item As DictionaryEntry 
        For Each Item In ht 
            Console.WriteLine(Item.Key) 
        Next Item 
    End Sub 
End Class

In Class2, I wanna use a Console.WriteLine method to output hence test the items in the hashtable. I Dim ht As Hashtable, but how can I use the items stored in ht in Class1?
confused.gif

Thanks very much for any help!
 
It's the same as anything in OO programming:

You have two objects, you have to decide how one is to know about the other, and also what things one exposes to the other.

I cant see from your code how Class2 comes into existence, so i'll have to use a example of my own:

VB.NET:
Class Employer
 
  Private emps as New List(Of Employee) 'collection of employees
 
  Private nam as String = "The Boss"
  Public Property Name
    Get 
      Return nam
    End Get
  End Property
 
  Public Sub RecruitEmployee()
    emps.Add(New Employee([B]Me[/B]))
  End Sub
End Class
 
Class Employee
  Private Employer myEmployer
 
  Public Sub New([B]Employer whoIsMyBoss[/B])
    myEmployer = whoIsMyBoss
  End Sub
 
  Public Sub WhatsYourEmployersName()
    MessageBox.Show(myEmployer.Name)
  End Sub
End Class

What we are talking about here is a HAS-A relationship

And employer HAS-A employee. AN Employee HAS-A employer.
The Employer must exist first before he can have employees
Ergo the employer creaets employees
When he does, each employee is told who is employer is
Now each employee has reference to his employer and can find things out about him.

You need to work out the "HAS-A'ness" of your objects, and make sure when one creates the other, it tells the other who it is. The other must retain that for use. Also, in good OO style, you should make the things the "employee" will access about the "employer" into properties like I have here, rather than making the "nam" string public
 
Back
Top