How in vb.net can I insert a value on a read only property?

186 views Asked by At

I want to create a color using the property r,g,b but unfortunately Visual Studio says r is a readonly value. Please help.

Dim newcolor As Color
newcolor.R = vermelho
newcolor.G = verde
newcolor.B = azul
3

There are 3 answers

1
the_lotus On BEST ANSWER

You'll have to use the shared method FromArgb.

Dim red As Color = Color.FromArgb(255, 0, 0)

Or in your case

Dim newcolor As Color = Color.FromArgb(vermelho, verde, azul)
0
GunnerFan420 On

Well R, G and B are definitely read only as they only have a GET method. I normally work with colors like this:

Dim Color As System.Drawing.Color

Color = Color.LightSteelBlue
Color = Color.SteelBlue

You can also do:

Dim ColorCode As Integer
ColorCode = Color.SteelBlue.ToArgb

Good Luck!

0
TyCobb On

You can't if this is the System.Drawing.Color struct.

If you look at the reference source for Color.R, you will see there is not a backing field for you to set. Unless of course you manipulate the Value, but then you may as well just declare a new Color each time you need to update it.

public byte R 
{
    get 
    {
        return(byte)((Value >> ARGBRedShift) & 0xFF);
    }
}