네임스페이스
한 낱말이 분야에 따라 다르게 해석되는 경우가 많다. 실세계를 바탕으로 추상화된 클래스도 어떤 분야에서 모델링했는지에 따라 같은 이름이 전혀 다른 모습을 띠고 있을 수 있다. 이를 구분하기 위해 사용하는 것이 '네임스페이스'이다.
네임스페이스의 정의
namespace 키워드를 사용하여 다음과 같이 정의한다.
namespace Banking {
public class Customer { }
}
네임스페이스의 중첩
네임스페이스 내에 또 다른 네임스페이스가 올 수 있다. 그러나 클래스 내에는 네임스페이스를 넣을 수 없기 때문에 네임스페이스에 대한 코드 조각은 단일 파일 페이지 모델로 설정된 TestCode.aspx에서 사용할 수 없다.
namespace System {
namespace Web {
namespace UI {
public class Page : TemplateControl, IHttpHandler {
...
}
}
}
}
중첩된 네임스페이스는 다음과 같이 간단하게 표현할 수 있다.
namespace System.Web.UI {
public class Page : TemplateControl, IHttpHandler {
...
}
}
다른 네임스페이스에 있는 클래스 사용하기
namespace Banking {
public class Customer {
private string m_Name;
public void SetName(string name) { m_Name = name; }
public string GetName() { return m_Name; }
}
}
namespace Medical {
public class MedicalCustomer {
Banking.Customer m_Customer; // 다른 네임스페이스 클래스형 사용
public void RegisterCustomer(string name) {
// 다른 네임스페이스 클래스 생성자 사용
m_Customer = new Banking.Customer();
m_Customer .SetName(name);
}
public string GetName() { return m_Customer.GetName(); }
}
}
위와 같이 다른 네임스페이스 내에 있는 클래스는 '[네임스페이스].[클래스명]'으로 사용한다. 매번 [네임스페이스]를 사용하여 참조하는 것은 번거로운 일이므로, 'using 지시자'로 간단하게 쓸 수 있다.
using Banking;
namespace Medical {
public class MedicalCustomer {
Customer m_Customer; // Banking 네임스페이스에 정의된 클래스형 사용
public void RegisterCustomer(string name) {
// Banking 네임스페이스에 정의된 클래스 생성자 사용
m_Customer = new Customer();
m_Customer.SetName(name);
}
public string GetName() { return m_Customer.GetName(); }
}
}
using 지시자의 특징은 다음과 같다.
- using 지시자 뒤에는 네임스페이스명만 올 수 있다.
- using 지시자의 뒤에는 클래스를 지정할 수 없다.
- 프로그램의 첫 부분에 선언해준다.
TestCode.aspx의 B 부분에 다음 코드를 넣어 실행해보자.
int result = 0; // 정수형 변수로 최종 결과가 저장
int myInt = 0; // 정수형 변수 x를 선언하고 그 값을 0으로 설정
// while문은 변수 myInt가 7보다 작을 때까지 다음 블록을 계속 실행
while (myInt < 7) {
myInt++; // 변수 myInt에 1을 더해 변수 myInt에 저장
result += myInt; // result에 myInt를 더해 result에 저장
// 만약 변수 result가 7보다 크면 while문의 블록 다음으로 이동
if (result > 7) break;
}
// 제어 변수i가 0부터 3보다 작을 때까지 반복문 실행
// 순환할 때마다 제어 변수 i는 1씩 증가
for (int i = 0; i < 3; i++) {
result += i; // result에 i를 더해 result에 저장
}
// 정수형 변수 result를 문자열형으로 바꾸어 ShowResult 메서드의 매개변수로 전달
ShowResult(result.ToString());
// result가 출력
웹 사이트에 다음 조건을 만족하는 Grade.aspx를 만들어라.
- 성적을 입력하고 버튼을 누르면 학점을 출력하는 웹 페이지다.
- 90점 이상 A, 80~89점 B, 70~79점 C, 60~69점 D, 60점 미만 F를 출력한다.
번호 | 컨트롤 종류 | 속성 | 설정 |
① | TextBox | ID | txtScore |
② | Button | ID | btnGrade |
Text | '학점 계산' | ||
③ | Label | ID | lblGrade |
Text |
// Grade.aspx.cs 파일
protected void btnGrade_Click(object sender, EventArgs e)
{
int score = int.Parse(txtScore.Text);
if (score >= 90 && score <= 100)
{
lblGrade.Text = "A";
}
else if (score < 90 && score >= 80)
{
lblGrade.Text = "B";
}
else if (score < 80 && score >= 70)
{
lblGrade.Text = "C";
}
else if (score < 70 && score >= 60)
{
lblGrade.Text = "D";
}
else if (score < 60 && score >= 0)
{
lblGrade.Text = "F";
}
else
{
lblGrade.Text = "올바른 점수가 아닙니다.";
}
}
몫을 이용해 swicth 문으로도 할 수 있을 것 같다.
'ASP.NET 4.0' 카테고리의 다른 글
[ASP.NET] Page 클래스 속성 (0) | 2021.12.28 |
---|---|
[ASP.NET] 웹 폼의 동작 원리(GET·POST 방식) (0) | 2021.12.28 |
[ASP.NET] 객체 지향 프로그래밍 (1) (0) | 2021.12.27 |
[ASP.NET] 제어문 (0) | 2021.12.27 |
[ASP.NET] 연산자 (0) | 2021.12.27 |
댓글