Results 1 to 8 of 8

Thread: Declaring an object? just checking if i'm doing this right.....

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Red face

    Hello!
    I just gota question about declaring a Variable as an object
    like for instance....
    [code/]
    Dim txtTextBox as textbox
    [code]
    now does that just make txtTextBox a textbox object?
    so i could write a statment like this.....
    txtTextBox.text = "hello"

    But if thats true, whats the point? can someone give me an example of why i would do that?

    Because i mean thats just like changing the name property of it...and just rename the textbox "txtTextBox"
    i just don't understand why you would do that(if u know why, please post an example that is useful)...THanks for listening!

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Not a particularly good example but one that works. Suppose you code in a non-English language and you want to localize the names of objects to your language...

    [Edited by JHausmann on 08-17-2000 at 07:11 PM]

  3. #3
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    I use it for when I am writing a function or a sub in a module, and that function or sub needs a textbox to be executed correctly example:

    Code:
    Public Function DoStuff(TxtBox as TextBox)
    'Do Stuff Here
    End Function
    
    'and then in a command button you migh have
    
    Call DoStuff(Text1)

    I haven't really used it for a textbox, as you can just use a string, and dump the contents of a textbox into that string, but I have used it for a listbox and numerous other objects, the reason for this is I find it easier to type:
    TxtBox.text = "Stuff"
    than:
    Form1.Text1.Text = "Stuff"

    and also because you can use that sub or function again, and again with all different textboxes or whatever the object is, I find it quite handy.


    [Edited by Crypt on 08-18-2000 at 03:49 AM]

  4. #4
    Guest
    I also use it A Lot in subs that are in modules.
    I use TreeViews, TextBoxes, ListBoxes, etc. but I dont usually do this


    Code:
    Private Sub Command1_Click()
    Dim TheTextBox As TextBox
    
    'etc
    'etc
    
    End Sub
    I just never found the need to do that.

    and the title of this thread brings up another question, lets say I have a class module that is name "clsThing"

    how would I call it?

    Code:
    Dim Thing As New clsThing
    
    'or
    
    Dim Thing As clsThing
    Set Thing = New clsThing
    ???

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Both are correct; however, the second method (first Dim then Set as New) is more efficient.

    Remember to stick the Dim in your General Declarations area, and the Set in the Form_Load area.
    Also you should do this:
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        Set Thing = Nothing
    End Sub
    (Thing = Nothing, that didn't sound TOO weird, did it?)

    Everything I said here was for efficiency only. Use it so your program will take a lot of RAM (Instead of a WHOLE lot).

  6. #6
    Guest
    why would it take a whole lot/ a lot?
    do class modules take logs of memory or something?
    or are you just saying for for vb programs in general ?

  7. #7
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    I'm complaining about the efficiency of any RAM handling that Windows does (if such efficiency exists ).
    Windows and VB like it when you Dim in the declarations area, Set to New in the beginning and Set to Nothing in the end.
    I don't know why, I didn't design the Windows memory handling routines (and I'm glad I didn't), it might be a conspiracy, it might be anything. Blame Microsoft!

  8. #8
    Guest
    , I didn't design the Windows memory handling routines (and I'm glad I didn't),
    perhaps it would have been better if you had!

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