CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: better code

  1. #1
    Join Date
    Jul 2005
    Posts
    3

    better code

    I have my ugly code:

    dim o1, o2, o3,... as object

    object1.property = 100
    object2.property = 100
    ...
    How I make it better,
    for example by array object?
    (I am beginner.)

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: better code

    Quote Originally Posted by Darbujan
    I have my ugly code:

    dim o1, o2, o3,... as object

    object1.property = 100
    object2.property = 100
    ...
    How I make it better,
    for example by array object?
    (I am beginner.)
    What are you trying to do?

    Code:
    Dim o(2) As Object 'whatever datatype
    Dim LoopCount As Integer
    
    For LoopCount = 0 To UBound(o)
      o(LoopCount).property = 100
    Next LoopCount

  3. #3
    Join Date
    Jul 2005
    Posts
    3

    Re: better code

    I will be exatly. I do somewhere error.
    ...
    Set ie1 = CreateObject("InternetExplorer.Application")
    ie1.navigate address1
    Set ie2 = CreateObject("InternetExplorer.Application")
    ie2.navigate address2
    ...
    ie1.visible= true
    ie2.visible=true
    ...

    if write this:
    ...
    Dim ie(2) As Object 'whatever datatype
    Dim i As Integer

    For i = 0 To UBound(ie)
    ie(i).Visible = True
    Next i
    ...

    =>runtime error 91 bject variable or width blockvariable not set

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: better code

    You are just decalring the Object but you are not Instantiating it. You also need to instantiate the object either using a New Keyword or the CreateObject function

    Something like this
    Code:
    Dim ie(2) As Object 'whatever datatype
    Dim i As Integer
    Set ie(0) = New WhatEverObject
    Set ie(1) = New WhatEverObject
    For i = 0 To UBound(ie)
    ie(i).Visible = True
    Next i

  5. #5
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: better code

    Code:
    Dim ie(2) As Object 'whatever datatype
    Dim i As Integer
    
    For i = 0 To UBound(ie)
        Set ie(i) = CreateObject("InternetExplorer.Application")
        ie(i).navigate address2
        ie(i).Visible = True
    Next i
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: better code

    One side note...

    When declaring multiple variables of a specific type, you cannot declare the type only at the end of the line. The type of each variable must be specified, otherwise you'll end up with Variants.

    In other words, do NOT do this:
    Dim A, B, C As String

    DO this:
    Dim A As String, B As String, C As String

    or

    Dim A$, B$, C$

    (The "$" is the type declaration character for a string)
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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