The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

c# Arrays and wierd errors

.kbf?.kbf? Registered User regular
edited October 2009 in Help / Advice Forum
float[, ,] x = null;
x = new float[4,8,8]; (all the error are on this line)

error CS1519: Invalid token '=' in class, struct, or interface member declaration
error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
error CS1519: Invalid token ';' in class, struct, or interface member declaration

If I declare it this way float[, ,] x = new float[4,8,8]; I works fine but from what I've found via google if you want to declare array size via a variable(which is what I want to do) you have to do it the way that's giving me the errors.

Any ideas from you programming gurus out there?

.kbf? on

Posts

  • AngelHedgieAngelHedgie Registered User regular
    edited October 2009
    .kbf? wrote: »
    float[, ,] x = null;
    x = new float[4,8,8]; (all the error are on this line)

    error CS1519: Invalid token '=' in class, struct, or interface member declaration
    error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
    error CS1519: Invalid token ';' in class, struct, or interface member declaration

    If I declare it this way float[, ,] x = new float[4,8,8]; I works fine but from what I've found via google if you want to declare array size via a variable(which is what I want to do) you have to do it the way that's giving me the errors.

    Any ideas from you programming gurus out there?

    First off, there's actually a programming thread in the tech subforum, so you're best off posting the question there.

    Second, I don't see why you would have to do that. float[, ,] x = new float[i,j,k]; should work fine. I'm curious to see where you got the advice.

    AngelHedgie on
    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum / Steam: noxaeternum
  • KakodaimonosKakodaimonos Code fondler Helping the 1% get richerRegistered User regular
    edited October 2009
    Can't do that with primitive arrays in C#. You always have to new it and define the sizes at the same time you declare the variable.

    http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

    If you want a variable size container, look at List<T>.

    Kakodaimonos on
  • CaswynbenCaswynben Registered User regular
    edited October 2009
    Can't do that with primitive arrays in C#. You always have to new it and define the sizes at the same time you declare the variable.

    http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

    If you want a variable size container, look at List<T>.
    This isn't true. float [,,] is a valid type that can be null, and can be assigned, or used as a parameter. I am confused by the OP, because what he has there is perfectly acceptable C#. I even pasted it into VS2008 and it compiles. Do you have another error in say, the line after what you pasted?

    Caswynben on
  • GanluanGanluan Registered User regular
    edited October 2009
    Caswynben wrote: »
    Can't do that with primitive arrays in C#. You always have to new it and define the sizes at the same time you declare the variable.

    http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

    If you want a variable size container, look at List<T>.
    This isn't true. float [,,] is a valid type that can be null, and can be assigned, or used as a parameter. I am confused by the OP, because what he has there is perfectly acceptable C#. I even pasted it into VS2008 and it compiles. Do you have another error in say, the line after what you pasted?

    Based on the error, I would guess the OP is actually using the second line outside of any method, something like this:
    public class MyClass
    {
          	 float[, ,] x = null;
             x = new float[4,8,8];
    
            public void MyMethod()
            {
                 Stuff
            }
    }
    

    The direct assignment works because that's the default value you're assigning it. You can't do an assignment for an already declared variable as part of a class definition.

    Ganluan on
  • .kbf?.kbf? Registered User regular
    edited October 2009
    Ganluan wrote: »
    Caswynben wrote: »
    Can't do that with primitive arrays in C#. You always have to new it and define the sizes at the same time you declare the variable.

    http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

    If you want a variable size container, look at List<T>.
    This isn't true. float [,,] is a valid type that can be null, and can be assigned, or used as a parameter. I am confused by the OP, because what he has there is perfectly acceptable C#. I even pasted it into VS2008 and it compiles. Do you have another error in say, the line after what you pasted?

    Based on the error, I would guess the OP is actually using the second line outside of any method, something like this:
    public class MyClass
    {
          	 float[, ,] x = null;
             x = new float[4,8,8];
    
            public void MyMethod()
            {
                 Stuff
            }
    }
    

    The direct assignment works because that's the default value you're assigning it. You can't do an assignment for an already declared variable as part of a class definition.

    That's it!

    .kbf? on
This discussion has been closed.