使用C#我试图开发以下两个。 我的做法可能会有一些问题,需要你的建议。 另外,我不知道是否有任何现有的方法做同样的事情。
private static String HexConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } catch (Exception ex) { //doing nothing } return rtn; } private static String RGBConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; } catch (Exception ex) { //doing nothing } return rtn; }谢谢。
Using C# I was trying to develop the following two. The way I am doing it may have some problem and need your kind advice. In addition, I dont know whether there is any existing method to do the same.
private static String HexConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } catch (Exception ex) { //doing nothing } return rtn; } private static String RGBConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; } catch (Exception ex) { //doing nothing } return rtn; }Thanks.
最满意答案
我没有看到这里的问题。 代码对我来说很好
我唯一可以想到的是try / catch块是冗余的 - Color是一个结构体,R,G和B是字节,所以c不能为空, cRToString() , cGToString()和cBToString()不能真的失败(唯一的方法,我可以看到他们失败是一个NullReferenceException ,并且他们都不能真的为空)。
您可以使用以下方法清理整个事情:
private static String HexConverter(System.Drawing.Color c) { return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } private static String RGBConverter(System.Drawing.Color c) { return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; }I'm failing to see the problem here. The code looks good to me.
The only thing I can think of is that the try/catch blocks are redundant -- Color is a struct and R, G, and B are bytes, so c can't be null and c.R.ToString(), c.G.ToString(), and c.B.ToString() can't actually fail (the only way I can see them failing is with a NullReferenceException, and none of them can actually be null).
You could clean the whole thing up using the following:
private static String HexConverter(System.Drawing.Color c) { return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } private static String RGBConverter(System.Drawing.Color c) { return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; }将System.Drawing.Color转换为RGB和Hex值(Convert System.Drawing.Color to RGB and Hex Value)使用C#我试图开发以下两个。 我的做法可能会有一些问题,需要你的建议。 另外,我不知道是否有任何现有的方法做同样的事情。
private static String HexConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } catch (Exception ex) { //doing nothing } return rtn; } private static String RGBConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; } catch (Exception ex) { //doing nothing } return rtn; }谢谢。
Using C# I was trying to develop the following two. The way I am doing it may have some problem and need your kind advice. In addition, I dont know whether there is any existing method to do the same.
private static String HexConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } catch (Exception ex) { //doing nothing } return rtn; } private static String RGBConverter(System.Drawing.Color c) { String rtn = String.Empty; try { rtn = "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; } catch (Exception ex) { //doing nothing } return rtn; }Thanks.
最满意答案
我没有看到这里的问题。 代码对我来说很好
我唯一可以想到的是try / catch块是冗余的 - Color是一个结构体,R,G和B是字节,所以c不能为空, cRToString() , cGToString()和cBToString()不能真的失败(唯一的方法,我可以看到他们失败是一个NullReferenceException ,并且他们都不能真的为空)。
您可以使用以下方法清理整个事情:
private static String HexConverter(System.Drawing.Color c) { return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } private static String RGBConverter(System.Drawing.Color c) { return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; }I'm failing to see the problem here. The code looks good to me.
The only thing I can think of is that the try/catch blocks are redundant -- Color is a struct and R, G, and B are bytes, so c can't be null and c.R.ToString(), c.G.ToString(), and c.B.ToString() can't actually fail (the only way I can see them failing is with a NullReferenceException, and none of them can actually be null).
You could clean the whole thing up using the following:
private static String HexConverter(System.Drawing.Color c) { return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } private static String RGBConverter(System.Drawing.Color c) { return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; }
发布评论