GetBool

GetBool 함수는 여러 형태의 값들에 대해서 true 혹은 false 형태의 boolean값을 반환합니다.
아래와 같이 사용하는 데이터베이스나 데이터소스에 따라서, true와 false를 구분하는 기호들이 각양각색이지요?
1) 0: false, 1: true
2) YES: true, NO: true
3) Y: true, N: false
4) (문자열)"true": true, "false": false 등등


잘 이해가 되지 않으신가요?
아래 예제를 보면서 다시 설명드리겠습니다.
DataTable _dt = new DataTable();
_dt.Columns.Add("col1", typeof(string));	// YES/NO
_dt.Columns.Add("col2", typeof(string));	// Y/N
_dt.Columns.Add("col3", typeof(bool));		// true/false
_dt.Columns.Add("col4", typeof(int));		// 0/1

DataRow _dr = _dt.NewRow();
_dr["col1"] = "YES";
_dr["col2"] = "Y";
_dr["col3"] = true;
_dr["col4"] = 1;
_dt.Rows.Add(_dr);

if (base.GetBool(_dt.Rows[0]["col1"]) == true)
	Response.Write("col1 is true.");
else
	Response.Write("col1 is false.");

if (base.GetBool(_dt.Rows[0]["col2"]) == true)
	Response.Write("col2 is true.");
else
	Response.Write("col2 is false.");

if (base.GetBool(_dt.Rows[0]["col3"]) == true)
	Response.Write("col3 is true.");
else
	Response.Write("col3 is false.");

if (base.GetBool(_dt.Rows[0]["col4"]) == true)
	Response.Write("col4 is true.");
else
	Response.Write("col4 is false.");
결과는 아래와 같습니다.
col1 is true.
col2 is true.
col3 is true.
col4 is true.


자, 이번에는 DataTable 오브젝트가 아닌 숫자나 문자열로 boolean값을 반환 받아 보겠습니다.
string _str01 = "yES";
string _str02 = "y";
string _str03 = "true";
int _int04 = 1;

if (base.GetBool(_str01) == true)
	Response.Write("_str01 is true.");
else
	Response.Write("_str01 is false.");

if (base.GetBool(_str02) == true)
	Response.Write("_str02 is true.");
else
	Response.Write("_str02 is false.");

if (base.GetBool(_str03) == true)
	Response.Write("_str03 is true.");
else
	Response.Write("_str03 is false.");

if (base.GetBool(_int04) == true)
	Response.Write("_int04 is true.");
else
	Response.Write("_int04 is false.");
결과는 아래와 같습니다.
_str01 is true.
_str02 is true.
_str03 is true.
_int04 is true.


여러분들 중에서는 아마도 도대체 이 함수는 왜 필요한거지?라는 의문을 가지는 분이 계실 수도 있습니다.
GetBool 함수는 그리드의 Template 컬럼에서 유용하게 쓰입니다.
아래의 소스를 한 번 살펴보겠습니다.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetBool.aspx.cs" Inherits="BANANA.Web.Framework.Test.jmson.GetBool" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>GetBool</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
			<Columns>
				<asp:TemplateField HeaderText="O/X">
					<ItemTemplate>
						<asp:CheckBox runat="server" Checked='<%# base.GetBool(Eval("CHK")) %>' />
					</ItemTemplate>
				</asp:TemplateField>
			</Columns>
		</asp:GridView>
	</div>
	</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

namespace BANANA.Web.Framework.Test.jmson
{
	public partial class GetBool : BANANA.Web.BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				DataTable _dt = new DataTable();
				_dt.Columns.Add("CHK", typeof(string));	// YES/NO

				DataRow _dr = _dt.NewRow();
				_dr["CHK"] = "YES";
				_dt.Rows.Add(_dr);

				_dr = _dt.NewRow();
				_dr["CHK"] = "NO";
				_dt.Rows.Add(_dr);

				_dr = _dt.NewRow();
				_dr["CHK"] = "1";
				_dt.Rows.Add(_dr);

				GridView1.DataSource = _dt;
				GridView1.DataBind();
			}
		}
	}
}
결과는 아래와 같습니다.
O/X
일반적인 상황이라면, 아마도 protected bool로 데이터를 전달하거나, aspx내에서 코드를 길게 늘여 쓰는 일이 많을 것입니다.
GetBool 함수는 이러한 노가다성 작업을 줄여 줍니다.


GetBool 함수는 총 3가지 형태로 제공됩니다.
/// <summary>
/// Bool 값을 반환
/// </summary>
/// <param name="_objVal">오브젝트 데이터</param>
/// <returns>true 혹은 false를 반환</returns>
public bool GetBool(object _objVal)


/// <summary>
/// Bool 값을 반환
/// </summary>
/// <param name="_intVal">숫자 데이터(0/1)</param>
/// <returns>true 혹은 false를 반환</returns>
public bool GetBool(int _intVal)


/// <summary>
/// Bool 값을 반환
/// </summary>
/// <param name="_strVal">문자열 데이터</param>
/// <returns>true 혹은 false를 반환</returns>
public bool GetBool(string _strVal)

이상으로 GetBool 함수에 대해서 알아보았습니다.

TOP