이전에 답변을 받으 셨다면 죄송합니다.하지만 읽고 검색을했는데 답을 찾을 수 없습니다.
string[] myColors = {"red","white","blue"};
예를 들어 배열의 n 번째 멤버에 액세스하여 해당 색상 중 하나를 선택 했다고 가정 해 보겠습니다 . myColors[2]
이것은 문자열 "파란색"이됩니다.
이와 별도로, 클래스 인 box가 있는데, 그중 myBox라는 인스턴스를 생성했습니다. 클래스에는 Color 유형의 boxColor 속성이 있습니다. 그리고 해당 Color 유형의 가능한 값에는 Color.red = RGB (255,0,0), Color.white = RGB (255,255,255) 및 Color.blue = RGB (0,0,255) (인수를 위해)가 포함됩니다. , 이러한 열거 된 값의 실제 값과 유형은 관련이 없으며 똑같이 float
's 또는 다른 C # 유형일 수 있습니다.
myBox.boxColor를 내가 얻은 값으로 설정하는 방법은 myColors[2]
무엇입니까?
즉 myBox.boxColor = (Color) (value of the string myColors[2]);
이 경우 실제로 특정 색상으로 화면에 그릴 수 있습니다.
어떤 유형의 매핑이 있어야합니다. 여기서는 사전을 사용하지만 대안이 있습니다. Color
귀하의 질문에 따라 사용 했지만 Color
귀하가 선택한 다른 클래스 또는 구조로 대체 할 수 있습니다.
public struct Color
{
int r, g, b;
public Color(int r, int g, int b)
{
this.r = r;
this.g = g;
this.b = b;
}
}
public static class BetterColors
{
static Dictionary<string, Color> colorDictionary = new Dictionary<string, Color>();
static BetterColors()
{
colorDictionary.Add("Red", new Color(255, 2, 4));
colorDictionary.Add("Blue", new Color(0, 3, 251));
colorDictionary.Add("Green", new Color(0, 200, 0));
}
static public Color GetColor(string colorName)
{
return colorDictionary[colorName];
}
}
class Box
{
public Color boxColor { get; set; }
}
class Program
{
static void Main()
{
string[] myColors = { "Red", "Green", "Blue" };
Box myBox = new Box();
myBox.boxColor = BetterColors.GetColor(myColors[1]);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다