Question about C# Constructor Arguments

Started by
6 comments, last by A4L 5 years, 4 months ago

Hi there.. I'm new to coding and was under the impression that this is a good place to ask noob c# questions, as working form a book can leave some questions unanswered!

is there a short and way in C# to write a array?

For example... say I have a class that takes a btye[3] arrry to store Red, Green, Blue (lets forget about alpha for ease sake). So you would have a constructor that takes a array as its variable.

In LUA I could do this very easily with something like Ball({1,2,3}) this Ball constructor would read {1,2,3} as array[1] = 1. array[2]=2 and array[3]=3. Which would then use those values to set r=1, g=2, b=3. I know in C# how I could make the constructor look like this... Ball ball = new Ball(1,2,3); to get the same thing.. but what I liked about LUA is that using arrays directly in the constructor meant I could keep everything grouped. so Ball.GetColour() returns array[3] for example.. I I could do things like.. ball2.colour = ball.getColour(); or w/e.

Am I making sense? lol.

So for my real world exercise example I would like to have a constructor that I init kinda like this.. Ball ball = new Ball({1,2,3},255) so there is a 3 values I can easily read as the RGB and 1 as the ALPHA. The class would need an array[3] for the {1,2,3} to fill.. but I do now know how to write an array in C# with out doing byte[] array = new byte[3] or w/e...

Thoughts?

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
Advertisement

The problem with arrays is that you get all these magic index numbers sprinkled throughout your code. Say you have a colour stored in an array. When you pass it to the drawing code, you need to write something like "draw(array[0], array[1], array[2])" (C# and most other languages start counting at 0. Lua is one of the exceptions there.)

The problem is now, you have "array[2]", but after a year you don't know what it represents. Was it a colour or the alpha channel? If a colour which component?

Instead of arrays with magic indices, a better solution is to make a Colour class (if it doesn't exist already) and write something like "new Ball(new Colour(1,2,3));". Bit more work, but you can write "ball.colour" or "ball.colour.red" which gives much more information what data you're passing around through the program.

 

You can initialize arrays in a variety of ways:  https://stackoverflow.com/questions/5678216/all-possible-array-initialization-syntaxes

The most abbreviated form is:

new[] { 10, 20, 30 }
12 hours ago, A4L said:

 init kinda like this.. Ball ball = new Ball({1,2,3},255) so there is a 3 values I can easily read as the RGB and 1 as the ALPHA.

Aside from the above posts, you can also use some constructor overloading for this with default color or specifying RGB only or complete color with alpha value.

 


    public class Ball
    {

        public Color Colour = new Color(255,255,255,255);


        public Ball()
        {

        }

        public Ball( byte r, byte g, byte b )
        {
            Colour.R = r;
            Colour.G = g;
            Colour.B = b;
        }

        public Ball(byte r, byte g, byte b, byte a)
        {
            Colour.R = r;
            Colour.G = g;
            Colour.B = b;
            Colour.A = a;
        }

    }

    public struct Color
    {
        public Color(byte r, byte g, byte b, byte a)
        {
            R = r; G = g; B = b; A = a;
        }

        public byte R;
        public byte G;
        public byte B;
        public byte A;
    }

}


   

 

12 hours ago, Nypyren said:

You can initialize arrays in a variety of ways:  https://stackoverflow.com/questions/5678216/all-possible-array-initialization-syntaxes

The most abbreviated form is:

new[] { 10, 20, 30 }

cool... so dose this take the type of the expected constructor input.. or would i have to make the constructor take "var" and then convert or w/e


Ball ball = new Ball(new[]{1,2,3},255})

 

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

The constructor can't take 'var' (var is only allowed for local variables).  The new[] {...} determines its type from the values inside the { }.  In this case since 10 20 and 30 are int literals, the array will be an int[].

This might be a problem in your byte[] case since I don't believe there is a byte literal syntax yet.

thanks... you have been very helpful

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

This topic is closed to new replies.

Advertisement