Home > Computer Programming > enum in c#

enum in c#


wow, enum, apa sih enum di c#

enum adalah sebuah deklarasi untuk membuat object yang bisa kita defined sendiri misalkan lihat contoh di bawah:

public enum ObjectMaxType
        {
            String,
            Query,
            Error,
            BoolYes,
            BoolNo,
            Failed,
            Success
        }

saya mau membuat sebuah tipe object di mana isinya adalah apakah tipenya string, apakah tipenya query, apakah tipenya error, apakah tipenya Yes, apakah tipenya No, apakah failed (gagal) dan apakah statusnya sukses

cara penggunaannya:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class myobject
    {
        public enum ObjectMaxType
        {
            String,
            Query,
            Error,
            BoolYes,
            BoolNo,
            Failed,
            Success
        }

        string passedValue;
        ObjectMaxType objectType;
        DataTable table;

        public string PassedValue
        {
            get { return passedValue; }
            set { passedValue = value; }
        }

        public DataTable Table
        {
            get { return table; }
            set { table = value; }
        }

        public ObjectMaxType ObjectType
        {
            get { return objectType; }
            set { objectType = value; }
        }

        public myobject(string PassedValue, ObjectMaxType ObjectType)
        {
            this.passedValue = PassedValue;
            this.objectType = ObjectType;
        }

        public myobject(DataTable Table, ObjectMaxType ObjectType)
        {
            this.table = Table;
            this.objectType = ObjectType;
        }

        public myobject() { }

    }

    class Program
    {
        static void Main(string[] args)
        {
            myobject status = new myobject();
            try
            {
                myobject abu = new myobject();
                abu.PassedValue = "this Abu is not Nawas";
                abu.ObjectType = myobject.ObjectMaxType.String;
                DataTable mydata = new DataTable();
                Console.WriteLine(abu.PassedValue);
                #region
                SqlConnection oConn = new SqlConnection(" must be filled by your datasource ");
                SqlCommand oCmd = new SqlCommand("select whatever your table is ");
                oCmd.Connection = oConn;
                oConn.Open();
                SqlDataAdapter oDa = new SqlDataAdapter(oCmd);
                oDa.Fill(mydata);
                oConn.Close();
                #endregion

                myobject abutable = new myobject(mydata, myobject.ObjectMaxType.Query);
                Console.WriteLine(abutable.Table.TableName.ToString());

                status.PassedValue = "Success";
                status.ObjectType = myobject.ObjectMaxType.Success;
            }
            catch (Exception err)
            {
                status = new myobject(err.Message, myobject.ObjectMaxType.Error);
                //Console.WriteLine(status.PassedValue);
            }

            Console.WriteLine("Status Apps is : " + status.ObjectType + "\n" + "the message is : " + status.PassedValue);
            Console.Read();
        }
    }
}

Categories: Computer Programming
  1. No comments yet.
  1. No trackbacks yet.

Please kindly Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: