Results 1 to 6 of 6

Thread: run time error 9 subscript out of range

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    run time error 9 subscript out of range

    Please help me, i'm a newbie in vb
    i have eror in my images processing aplication
    show error is :run time error 9 subscript out of range in visual basic"
    Code:
    Private Sub Command6_Click()
    Dim i As Long, j As Long
    Dim r As Long, g As Integer, b As Integer
    Dim p1 As Long, p2 As Long, p3 As Long, p4 As Long, p5 As Long '9x9 grid
    Dim p6 As Long, p7 As Long, p8 As Long, p9 As Long
    Dim ImgData() As Long
    
    For i = 1 To Picture1.ScaleWidth - 2
        For j = 1 To Picture1.ScaleHeight - 2
                
            r = ImgData(0, i - 1, j - 1) + ImgData(0, i, j - 1) + ImgData(0, i + 1, j - 1) + ImgData(0, i - 1, j) + _
            ImgData(0, i, j) + ImgData(0, i + 1, j) + ImgData(0, i - 1, j + 1) + ImgData(0, i, j + 1) + _
            ImgData(0, i + 1, j + 1)
            
            g = ImgData(1, i - 1, j - 1) + ImgData(1, i, j - 1) + ImgData(1, i + 1, j - 1) + ImgData(1, i - 1, j) + _
            ImgData(1, i, j) + ImgData(1, i + 1, j) + ImgData(1, i - 1, j + 1) + ImgData(1, i, j + 1) + _
            ImgData(1, i + 1, j + 1)
            
            b = ImgData(2, i - 1, j - 1) + ImgData(2, i, j - 1) + ImgData(2, i + 1, j - 1) + ImgData(2, i - 1, j) + _
            ImgData(2, i, j) + ImgData(2, i + 1, j) + ImgData(2, i - 1, j + 1) + ImgData(2, i, j + 1) + _
            ImgData(2, i + 1, j + 1)
            
            SetPixelV Picture2.hdc, i, j, RGB(r / 9, g / 9, b / 9)
        Next j
        Picture1.Refresh
    Next i
    End Sub
    line erorr in "r = ImgData(0, i - 1, j - 1) + ImgData(0, i, j - 1) + ImgData(0, i + 1, j - 1) + ImgData(0, i - 1, j) + _"

    please help me

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: run time error 9 subscript out of range

    Code:
    Dim ImgData() As Long
    
    For i = 1 To Picture1.ScaleWidth - 2
        For j = 1 To Picture1.ScaleHeight - 2
                
            r = ImgData(0, i - 1, j - 1) + ImgData(0, i, j - 1) + ImgData(0, i + 1, j - 1) + ImgData(0, i - 1, j) + _
    No where in there do you set ImgData.... so it contains nothing... you're trying to pull an egg from an egg carton that's not only empty but doesn't even exist.
    you need to define at the very least the dimensions of the array before using them. And in this case, you need to make sure that it's been filled.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    Re: run time error 9 subscript out of range

    it doesn't work... should i upload my project to you..
    i have try to declaration ImgData() with variant version but it's not working..
    Could you help me please

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: run time error 9 subscript out of range

    I don't know what to tell you other than in itty bitty bits....


    This tells VB "HEy, I need a bucket.... but I don't know how bit it needs to be yet, but I need a bucket"...

    then you start your loop... then your second loop.... so far so good...
    but then you try to use your bucket:
    ImgData(0, i - 1, j - 1)


    But "WAIT A MINUTE" says VB (by giving you an error) ... you HAVEN'T BUILT the bucket nor have you even FILLED the bucket...

    It is empty... what do you expect to find inside of ImgData? Right now it's vapor.

    do you see what I'm driving at?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    3

    Re: run time error 9 subscript out of range

    ImgData(100,100,100) ?? i have try it and still doen't work, check your pm

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: run time error 9 subscript out of range

    I don't respond well to PMs... learning here should be out in the public, free for anyone to learn from. It defeats the purpose of the forums to start taking a thread off line...

    that said... NO.... doing imgData(100,100,100) won't work ... neither with imgData(0,0,0) ...

    I'm going to try one more time to explain the problem. Dim imgData() as Long .... gives you a bucket....

    Now... let's say you want to wash your car.... normally what you would do is take your bucket and FILL it with something (typically water) .... you can't wash your car with an empty bucket, can you? no. you need water in it.

    that's what's happening... you created a bucket imgdata() .... and then trying to use it, but there is NOTHING IN IT. You didn't fill it with anything. You're expecting it to be magically filled with data of some kind.

    Let me put it this way:
    Code:
    Dim myName as String
    MsgBox(myName)
    What would the Message Box show? Nothing... because myName was never filled with anything. That's the same problem you've got... imgData is empty... there is nothing in it. So until you redim it with all three dimensions AND load it with data... what you're doing is going to fail every time.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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