CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2000
    Location
    canada
    Posts
    124

    Problem with class

    I have a public object "a" (public on a form only). The type of this object depend of a choice from the user. I must keep the same name ("a") for both types.
    First, can I do this and second, how??!

    Thanks for all help you can give to me,
    Isabelle


  2. #2
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: Problem with class

    1) If you declare a variable as Public, then it is public to the entire project. If you want the object to be accesible to all procedures within the form, Declare it with Private scope in the General Declarations section of your module.

    2)I think this is what you want to do:

    'In Genral Declarations...
    Dim a as Object
    'Later in your code...
    private Sub ChooseAObjectType(iType as Integer)
    'Check to see if the object has already
    'been set. If so, set it to nothing
    If Not a is nothing then
    set a = nothing
    End If
    Select Case iType
    Case 0
    set a = CreateObject("SomeLib.SomeClass")
    Case 1
    set a = CreateObject("SomeOtherLib.SomeOtherClass")
    End Select
    End Sub



    You need to take care when you're using one variable that can possibly be objects of different types that you don't call a method of the object that doesn't exist. Take a look at TypeOf

    Kyle


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured