Results 1 to 16 of 16

Thread: [RESOLVED] What is wrong with this code?

  1. #1

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Resolved [RESOLVED] What is wrong with this code?

    VB Code:
    1. Dim imagearray(4) As Image
    2. Set imagearray(0) = LoadPicture(App.Path & "\images\10.bmp")
    VB6 claims the second line to be a "type mismatch". Any ideas?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    417

    Re: What is wrong with this code?

    Try this code

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim imagearray(4) As StdPicture
    3.     Set imagearray(0) = LoadPicture("C:\test.gif")
    4.     Image1.Picture = imagearray(0)
    5. End Sub
    When your dreams come true.
    On error resume pulling hair out.

  3. #3
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: What is wrong with this code?

    Image is a 'property' used by StdPicture, picturebox (which is also a stdPic), etc. It's not a stand alone object.

    However things like this would work but aren't really beneficial.

    VB Code:
    1. Dim ImgData as Image
    2. Dim stdPic as stdPicture
    3.  
    4. stdPic = LoadPicture("C:\Test.gif")
    5. set imgData = stdPic.Image

  4. #4

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: What is wrong with this code?

    Ok, that seems to be working, but now this:
    VB Code:
    1. Public Sub pict()
    2. Dim i, imagearray(4)
    3. card(0).Picture = imagearray(0)
    4. End Sub
    The second to last line causes an error of "object required". Any reason why?

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: What is wrong with this code?

    Quote Originally Posted by ajames
    Ok, that seems to be working, but now this:
    VB Code:
    1. Public Sub pict()
    2. Dim i, imagearray(4)
    3. card(0).Picture = imagearray(0)
    4. End Sub
    The second to last line causes an error of "object required". Any reason why?
    It's cause you declared it as a variant. And on top of that, you even forgot to load an image. Try this:

    VB Code:
    1. Public Sub pict()
    2. Dim i As Long, imagearray(4) [b]As StdPicture[/b]
    3. [b]Set imagearray(0) = LoadPicture("C:\test.gif")[/b]
    4. card(0).Picture = imagearray(0)
    5. End Sub

  6. #6
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: What is wrong with this code?

    try set? (set card(0).picture = etc)

    Edit: Jacob rules

  7. #7

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: What is wrong with this code?

    that one seems to work (jacob), but i seem to have to dim "imagearray" in each sub I need to use it in. Is there a fix for that?

  8. #8
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: What is wrong with this code?

    make 'm public? (in other words, use Private or public above all your sub/function codes)

    VB Code:
    1. '1st line:
    2. Option explicit
    3.  
    4. Public ImageArray() etc etc.
    5.  
    6. Private sub Form_Load etc. etc etc

  9. #9

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: What is wrong with this code?

    Quote Originally Posted by Devion
    VB Code:
    1. Public ImageArray()
    When I use this, it renders most of the code as "invalid outside procedures" (even "set" and "app.path")

  10. #10
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: What is wrong with this code?

    Err.. only the variables should be declared outside subs.. not code itself..

  11. #11

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: What is wrong with this code?

    I changed it to "Public Sub imageArray()", but now when i try to do "pic.picture=imageArray(0)" it says "invalid number of arguments or invalid property assignment".

    I tried consulting help, but, as we all know, the microsoft help files never seem to tell you what to do, but instead tell you exactly what the error message says but with slightly altered words...

  12. #12
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: What is wrong with this code?

    I think you are looking at it from a weird angle ...

    ImageArray() is an array.. not a sub.

    VB Code:
    1. Private i As Long, imagearray(4) As StdPicture
    2.  
    3. Public Sub pict()
    4.     Set imagearray(0) = LoadPicture("C:\test.gif")
    5.     card(0).Picture = imagearray(0)
    6. End Sub

    This would allow ImageArray to be public within the form or module (not outside) and also I would be public within the form or module.

    If you would change private to public you could access those variables from different forms or modules.

  13. #13

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: What is wrong with this code?

    Thanks! It works! At last! rep points for you then

  14. #14
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [RESOLVED] What is wrong with this code?

    One thing you should know about arrays. When you declare it like this

    VB Code:
    1. Private ImageArray() As StdPicture

    It has no elements. 0 wouldn't even count either. So what you do is that after you declare it like so, you ReDim it within a sub or function:

    VB Code:
    1. Private ImageArray() As StdPicture
    2.  
    3. Private Sub Form_Activate()
    4.  
    5.     ReDim ImageArray(100) As StdPicture
    6.  
    7. End Sub

    Use ReDim Preserve if you want to keep the current values that are contained within an array, otherwise just using ReDim will erase them. This technique comes in handy if you have an unknown number of elements in your array.

  15. #15
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: [RESOLVED] What is wrong with this code?

    Thanks jacob for the redim preserve, never used that. I havent had to but that is a cool thing to know!

  16. #16
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: [RESOLVED] What is wrong with this code?

    You'll learn something everyday. oh! and thnx for the rep point. =)

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