FutureCake
FutureCake

Reputation: 2943

function that can take different type of struct C#

I have a function that needs and specific datatype as a parameter. The function looks like the following:

public static Color get_axis_loc_color(colormap axis, float location){
    var difRed = axis.r_end - axis.r_start;
    var difGreen = axis.g_end - axis.g_start;
    var difBlue = axis.b_end - axis.b_start;

    difRed = (int)(difRed * location) + axis.r_start;
    difGreen = (int)(difGreen * location) + axis.g_start;
    difBlue = (int)(difBlue * location) + axis.b_start;

    return new Color(a: 1, r: difRed, g: difGreen, b: difBlue);
}

now i have a struct that contains the colormap data like so:

public struct color_map  
{
    public struct x 
    {
        public static readonly int r_start =  255;
        public static readonly int g_start =  255;
        public static readonly int b_start =   255;
        public static readonly int r_end =  0;
        public static readonly int g_end =  0;
        public static readonly int b_end =  255;
    }

    public struct y
    {
        public static readonly int r_start = 255;
        public static readonly int g_start = 255;
        public static readonly int b_start =  255;
        public static readonly int r_end = 255;
        public static readonly int g_end = 0;
        public static readonly int b_end = 0;
    }

    public struct z
    {
        public static readonly int r_start = 103;
        public static readonly int g_start = 190;
        public static readonly int b_start = 155;
        public static readonly int r_end = 0;
        public static readonly int g_end = 150;
        public static readonly int b_end = 0;
    }
}

now when i call my function i need to be able to pass the following variables for the colormap axis parameter:
colormap.x or colormap.y or colormap.z

But i cant do this because the type doesn't match. How would i go about doing this?

If anything is unclear let me know so i can clarify.
Thanks in advance!

Upvotes: 0

Views: 74

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

You're on the wrong path when it comes to designing your code here. Any answer that purports to just answer your question isn't going to give you the right direction.

You can do it using reflection but it's not going to be pretty and not going to be performant.

You could also do it using interfaces, but this would be trying to just circumvent designing the code better in the first place.

Instead, what you should do is use 1 type and 3 variables.

Here, let me demonstrate:

public struct color_map
{
    private color_map(int r1, int g1, int b1, int r2, int g2, int b2)
    {
        r_start = r1;
        g_start = g1;
        b_start = b1;
        r_end = r2;
        g_end = g2;
        b_end = b2;
    }

    public int r_start { get; }
    public int g_start { get; }
    public int b_start { get; }
    public int r_end { get; }
    public int g_end { get; }
    public int b_end { get; }

    public static readonly color_map x = new color_map(255, 255, 255, 0, 0, 255);
    public static readonly color_map y = new color_map(255, 255, 255, 255, 0, 0);
    public static readonly color_map z = new color_map(103, 190, 155, 0, 150, 0);
}

This would allow you to pass in a parameter of type color_map and access the properties.

Your get_axis_color method could then be declared like this (I rewrote it to use System.Drawing.Color, but notice that the only other change I made was to make it type a color_map parameter, and not colormap, to be in line with the structure from above):

public static Color get_axis_loc_color(color_map axis, float location)
{
    var difRed = axis.r_end - axis.r_start;
    var difGreen = axis.g_end - axis.g_start;
    var difBlue = axis.b_end - axis.b_start;

    difRed = (int)(difRed * location) + axis.r_start;
    difGreen = (int)(difGreen * location) + axis.g_start;
    difBlue = (int)(difBlue * location) + axis.b_start;

    return Color.FromArgb(alpha: 1, red: difRed, green: difGreen, blue: difBlue);
}

Upvotes: 4

Related Questions