Results 1 to 6 of 6

Thread: declaring object question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I'm not sure how to do this but here's what I'm trying to accomplish..

    In a module, I need to reference a control on a form many times. Rather than keep typing Form1.Control.Whatever, I would like to create a name such as Control1 so that in my module, I can write:

    With Control1
    .Whatever
    .Whatever1
    End With

    Any help on how to do this would be appreciated.

    Dan


  2. #2
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    You can use a shortcut very close to what you have already said. Just dim a variable of type Object somewhere, and then set it to the control you want to keep referencing.
    Say you had a particular textbox from somewhere you wanted to keep using:
    Code:
    Dim X As Object
    
    'in the main loading form
    Private Sub Form_Load()
    
       Set X = Form1.Text1
    
    End Sub
    Then you can just use X like it was a real textbox.
    Code:
    X.Text = "some text"
    Or if you had a lot of things:
    Code:
    With X
       .Left = 0
       .Top = 0
       'etc
    End With
    When you're done using the variable:
    Code:
    Set X = Nothing

    [Edited by Kaverin on 10-27-2000 at 01:02 PM]
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks! I think that's what I was looking for but there is a small problem using that method..

    None of the events, methods or properties are available for me to see when using the object.

    For example, when I type:

    frmMain.Winsock1.

    the events, methods and properties of the Winsock control automatically pop up after the last "."

    But, when doing the following:

    Dim x as Object

    Set x = frmMain.Winsock

    x.

    nothing pops up after the "."

    Any idea on how to accomplish this? It's hard to remember all the methods, events and properties for each object..

    Thanks,

    Dan

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    I would assume that's because in the manner I described, X isn't anything yet (because it isn't valid until the program runs). You'll just have to learn all the things, or if you know what X is supposed to be (meaning at design time), just type out the class itself, and it should pop up. Something like Form. I can't check on this, so I could be going far off in left field This comp I'm on at the moment has no VB at all.

    Just so you know, I think using a variable in this manner is called "late binding of an object". I'm sure someone will correct me if I'm wrong heh heh.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    No, it's because you used late binding.
    Instead of dimensioning the object variable as object, dimension it as textbox and the intellisense feature is back again.

    So not:
    Dim x as Object
    but:
    Dim x As TextBox

  6. #6
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Been away studying for some exams... but anyway, I wanted to say something else. By all means, I think it would be better to actually dim the variable as the type it will reference in the future. Using plain old object is just a general way in the event you 1) don't know what it might be referencing 2) it will reference several different objects (at different times of course).
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

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