Results 1 to 10 of 10

Thread: Can't Dim walls

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    11

    Can't Dim walls

    Tried
    Dim walls = {wall1, wall2, wall3, wall4}
    But it says: Array initializers are valid only for arrays, but the type of 'wall' is 'Object'.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Can't Dim walls

    Uh.... walls is not dim'ed as an array.

    See https://learn.microsoft.com/en-us/do...atures/arrays/
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    11

    Re: Can't Dim walls

    So how can I do it?

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Can't Dim walls

    Quote Originally Posted by dbasnett View Post
    Uh.... walls is not dim'ed as an array.

    See https://learn.microsoft.com/en-us/do...atures/arrays/
    You know, that was my first thought. But this doesn't error and I have option strict On

    Code:
            Dim wall1 As Object, wall2 As Object
            wall1 = "something"
            wall2 = 12345
            Dim walls = {wall1, wall2}

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    11

    Re: Can't Dim walls

    There is 30 walls, I can't do that to 30 walls

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,473

    Re: Can't Dim walls

    Quote Originally Posted by Greogri View Post
    Tried
    Dim walls = {wall1, wall2, wall3, wall4}
    But it says: Array initializers are valid only for arrays, but the type of 'wall' is 'Object'.
    Is that the exact code giving the error and the exact error message? Seems strange the error message refers to "wall" but nowhere in the line of code you posted do you have anything declared as "wall".

    How are wall1, wall2, etc declared?

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Can't Dim walls

    Quote Originally Posted by PlausiblyDamp View Post
    Is that the exact code giving the error and the exact error message? Seems strange the error message refers to "wall" but nowhere in the line of code you posted do you have anything declared as "wall".

    How are wall1, wall2, etc declared?
    Good catch

  8. #8
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    538

    Re: Can't Dim walls

    How about just detectíng the color of the wall. If your sprite detects the color, it will not cross. Then you wont have to worry about creating so many objects.
    Last edited by Peter Porter; Nov 22nd, 2022 at 01:50 PM.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Can't Dim walls

    Quote Originally Posted by Greogri View Post
    So how can I do it?
    Code:
    Dim walls() ...
    You were missing ()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Can't Dim walls

    The issue is that you have Option Strict Off and Option Infer Off. You should turn both On in the project properties and also in the VS options, so they are On by default in future projects.

    With Option Infer On, the type of a variable can be inferred from the type of the initialising expression. You are initialising the walls variable with an array so it would be inferred to be that type. Because you have Option Infer Off, no type inference is performed.

    With Option Strict On, you need to specify a type for everything. You can do so implicitly for an initialised variable with Option Infer On but, otherwise, you must do so explicitly. With Option Strict Off, any variable for which you don't specify a type will default to type Object.

    You should ALWAYS have Option Strict On at the project level. It is Off by default primarily to make life easier for VB6 developers, particularly when upgrading existing code. Always turn it On and leave it On. In situations where you spe4cifically need late binding, turn it Off at the file level in only those files in which it is needed, using partial classes to keep the code in those files to an absolute minimum.

    If you don't want to use type inference - some people find it confusing when variables are not explicitly typed - then you can turn Option Infer Off but then you should have to specify the type of the variable explicitly, because you should still have Option Strict On. If you have both On, your code will work fine as it is, because walls will be inferred to be the correct type. If you have Option Strict On and Option Infer Off then you need to specify the type explicitly:
    vb.net Code:
    1. Dim walls As SomeType() = {wall1, wall2, wall3, wall4}
    where SomeType is the type of the array elements, whatever that is.

    EDIT:

    Actually, I just realised that this thread is tagged VS 2008. I'm not sure that Option Infer existed a decade and a half ago, when that was released. If it did then what I said above stands. If not, what I said above for having Option Infer Off is what you should follow. That and get yourself a more up-to-date version of VS unless you are required to use VS 2008 by work or school or the like. VS 2022 Community is free.

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