Tuesday, 23 February 2016

.net I review

string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; //vb   need imports system.text
using System.Threading.Tasks;

namespace strings_demo
{

    class Program
    {

        static void Main(string[] args)
        {
            string a = "hello";
            for (int x = 0; x < a.Length; x = x + 1)
                Console.WriteLine(a.Substring(x, 1));
            char[] b = a.ToCharArray(); //vb dim b() as char=...
            string b2 = new string(b);//vb dim b2 as new string(b)
            foreach (char c in b) Console.WriteLine(c.ToString());
            //vb  for each c as char in b
            byte[] d = Encoding.ASCII.GetBytes(a);//vb dim d() as byte=
            string d2 = Encoding.ASCII.GetString(d);
            string e = "1,bob,ab";
            string[] f = e.Split(',');
            //vb  dim f() as string = e.split(convert.tochar(","))
            for (int y = 0; y < f.Length; y = y + 1)
                Console.WriteLine(f[y]);
            string f2 = String.Join(",", f);
            string g = ""; //non-modifiable string (variable length)
            //re-allocates memory everytime string is changed
            for (int z = 1; z <= 9; z = z + 1) g = g + z.ToString();
            Console.WriteLine(g);
            StringBuilder h = new StringBuilder(20);//modifiable string
            //above is fixed length in memory
            for (int z2 = 1; z2 <= 9; z2 = z2 + 1) h.Append(z2.ToString());
            Console.WriteLine(h);
            string i = "12345";
            int total = 0;
            for (int z3 = 0; z3 < i.Length; z3 = z3 + 1)
                total = total + Convert.ToInt32(i.Substring(z3, 1));
            Console.WriteLine(total);
            total = 0;
            char[] j = i.ToCharArray();//gets ascii values not 1,2,3...
            for (int z4 = 0; z4 < i.Length; z4 = z4 + 1)
                //must convert char's to string's before converts
                total = total + Convert.ToInt32(j[z4].ToString());
            //vb total=total + convert.toint32(convert.tostring(j(z4)))
            Console.WriteLine(total);
         

            Console.Read();
        }
    }
}

--------------------------------------------------------------------------
inherit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace inheritance
{
    class person
    {   public string name;
        protected long phone;
        private string email;
        public person()  //public sub new()   constructor
        {   Console.WriteLine("in person");
        }
        public person(string n, long p)
        {//constructors are run top-down,  destructors are run bottom-up
            name = n;
            phone = p;
        }
        public void print() //vb  public sub print()
        {   Console.WriteLine(name+phone);
        }
    }
    class worker : person //is-a inheritance
        //vb  on second line   say    inherits person
    {   public int sin_number;
        public double salary;
        public worker()
        {   Console.WriteLine("in worker");
        }
        public worker(int sin, double s,string n,long p):base(n,p)
        {   sin_number = sin;  //vb  above is mybase.new(n,p)
            salary = s;
        }
        public void print()
        {//vb mybase.print()
            base.print();  //calls method in parent class
            Console.WriteLine(sin_number + " " + salary);
        }
    }
    class salesperson : worker
    {   public double commission;
        public salesperson()
        {   Console.WriteLine("in salesperson");
        }
        public salesperson(double c,double s,
                           int sin,long p,string name):base(sin,s,name,p) //named constructor
            //vb  public sub new(c as double)
            //vb  on second line   say    mybase.new(sin,s,name,p)
        {   commission = c;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            salesperson s = new salesperson(0.10,40000,123,5551111,"ed");
            s.print();
            Console.Read();
        }
    }
}

------------------------------------------------------------------------------------------
inherit 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace inheritane2
{
    class animal //c# use sealed class to protect
                 //vb use notinheritable class to protect
    {
        public string name="charlie";
        public int age=3;
        public virtual void print() //vb overrideable
        {
            Console.WriteLine(name+age);
        }
    }
    class dog : animal //is-a
    {
        public string sound = "woof";
        public override void print() //vb overrides
        {
            //base.print();
            Console.WriteLine(sound);
        }
    }
    class engine
    {
        public int horsepower;
    }
    class transmission
    {
        public int gears;
    }
    class car
    {
        public engine eng = new engine(); //has-a
        //public eng as new engine()
        public transmission trans = new transmission();
    }
    static class copyright //uses-a
    { //vb shared
        static public void show()
        {
            Console.WriteLine("Copyright SAIT");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            car c = new car();
            c.eng.horsepower = 300;
            c.trans.gears = 4;
            dog d = new dog();
            copyright.show(); //no need to use new command
            Console.WriteLine("hi");
            Console.Read();
        }
    }
}

----------------------------------------------------------------

multi-forms

--------------FORM 1 -------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace windemo2
{
    public partial class Form1 : Form
    {
        public static string data;
        //vb public shared data as string
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(textBox1.Text + textBox2.Text + textBox3.Text);
            textBox3.Text = "changed";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(textBox1.Text);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {//does not activate
            if (e.KeyCode == Keys.F1)
                MessageBox.Show("f1 pressed");
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
                MessageBox.Show("f1 pressed");
            if (e.KeyCode == Keys.F2)
                MessageBox.Show("f2 pressed");
            textBox2.Text = e.KeyCode.ToString();
            if (e.KeyCode.ToString() == "Next")
                MessageBox.Show("page down pressed");
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            textBox1_KeyDown(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            data = textBox1.Text; //put parameter in shared variable
            //pass parameter in constructor
            Form2 x = new Form2(textBox1.Text);//vb  dim x as new form2()
            x.parameter = textBox1.Text; //pass parameter with get/set
            x.f = this;//pass parameter using pointers
            //vb  above is = me
            //above must change form designer code
            //to have public security on textboxes to pass
            //modal call
            //x.Show();
            //non-modal call
            x.ShowDialog();
            textBox1.Text = data;//get from shared variable
            textBox5.Text = x.parameter;//get/set retrieval
            //MDI call
            //x.MdiParent  = this;//vb  =me
            //x.Show();
        }
    }
}


---------------------- FORM 2 ------------------

sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace windemo2
{
    public partial class Form2 : Form
    {
        public Form1 f;//vb public f as form1
        public string parameter//public property parameter as string
        {
            set { textBox3.Text = value; }
            get { return textBox3.Text; }
        }
        public Form2()
        {
            InitializeComponent();
        }
        //vb  public sub new()
        //vb  end sub
        public Form2(string x) //constructor
        {//vb public sub new(x as string)
            InitializeComponent();
            textBox1.Text = x;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }

        private void Form2_Load(object sender, EventArgs e)
        {//grab shared variable
            textBox2.Text = Form1.data;
            textBox4.Text = f.textBox1.Text;//grab pointer
        }

        private void button2_Click(object sender, EventArgs e)
        { //move back to public shared variable
            Form1.data = textBox2.Text;
            f.textBox6.Text = textBox4.Text;//pass with pointer
            this.Close();
        }
    }
}

--------------------------------------------------------------------

object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace objects3
{
    static class order //vb remove static
    {   static public int ordernumber; //vb  change static to shared
        static public double total;
    }
    class customer
    {   public string name;
        static public double creditlimit;
        public const double shiprate = 5.00; //like a static
        public readonly double rate2 = 10.00; //new version of const
    }
    class bank
    {
        public double account, amount, interest;
        public bank() { }   //vb  sub new()
        public bank(double am)
        {
            amount = am;
        }
        public bank(double am, double inter):this(am)
        {
            //amount = am;
            interest = inter;
        }
        public bank(double acc, double am, double inter):this(am,inter)
            //vb  public sub new(acc as double, ...)
        {
            this.account = acc;
            //in vb   me.new(am,inter) inside the code
            //amount = am;
            //interest = inter;
        }
     

    }
    class payroll
    {
        public string name=""; //attribute (no code attached)
        public payroll() { }
        public payroll(string n, double r)
        {
            this.name = n;
            this.rate = r;
        }
        public payroll(double r, string n):this(n,r)
        {
            //me.new(n,r)
        }
        private double _rate=0;
        public double rate //property (code attached)
        {
        get { return _rate;  } //somebody is printing variable
        set { if (value <= 100) _rate = value;  } //getting a value
        }
        //vb
        //dim _rate as double
        //public property rate as double
        //   set
        //   end set
        //   get
        //   end get
        // end property
        public double tax { get; set; } //useless
    }
    class Program
    {   static void Main(string[] args)
        {
            payroll p = new payroll();
            p.rate = 20;
            p.rate = 2000;
            Console.WriteLine(p.rate);

            Console.Read();
        customer c = new customer();
            c.name = "ed";
            Console.WriteLine(c.rate2);
            customer.creditlimit = 10000;
            customer d = new customer();
            d.name = "mary";
            order.ordernumber = 10;
            order.total = 1000;
            worker.worker w = new worker.worker();
            w.name = "bob";
            w.salary = 60000;
            Console.WriteLine(w.name);
        }
    }
}

------------------------------------------------------------

object 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace object1
{
    public class time
    {
        public int hour, minute, second;
        ~time()//destructor
        {
            Console.WriteLine("in destructor");
            Console.WriteLine("in destructor");
            Console.WriteLine("in destructor");
            Console.Read();//waits for a bit
        }
        public time() //empty constructor
        {
            Console.WriteLine("in constructor");
            hour = 0; minute = 0; second = 0;
        }
        public time(int x, int y, int z) //full constructor
        {
            this.hour = x;
            this.minute = y;
            this.second = z;
        }
        //fix for printing
        public override string ToString()
        {
            return hour+":"+minute+":"+second;
        }
        //fix for copying
        public void Copy(time x)
        {
            this.hour = x.hour;
            this.minute = x.minute;
            this.second = x.second;
        }
        public override bool Equals(object obj)
        {
            time x = (time)obj;
            return this.hour==x.hour && this.minute==x.minute &&
                                this.second==x.second;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            time t = new time(10,0,0); //t is an object
            //t.hour = 10;
            time t2 = new time();
            t2.hour = 10;
            Console.WriteLine(t);  //problem 1 with objects
            //if (t == t2) Console.WriteLine("same"); //problem 2
            if (t.Equals(t2)) Console.WriteLine("same");
            else Console.WriteLine("different");
            time t3 = new time();
            t3 = t; //problem 3
            time t4 = new time();
            t4.Copy(t);  //must use function to copy
            t.hour = 15;
            Console.WriteLine(t4.hour);
            Console.Read();
            //time.hour = 10;

        }
    }
}

----------------------------------------------------------------

error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace trap
{

    class Program
    {
        static void Main(string[] args)
        {
            double x=0;
            top: //assigns a label
            Console.WriteLine("enter a number");
            string s = Console.ReadLine();
            try
            {
                x = Convert.ToDouble(s);//can be many lines of code
            }
            catch (DivideByZeroException e1)//e1 is any name
            //can catch specific errors
            { Console.WriteLine("math error"); }
            catch (Exception e2) //catches all remaining errors
            { Console.WriteLine(e2.Message);
             goto top;
            }
        top2: Console.WriteLine("Enter second number");
        double x2 = 0;
        try { x2 = Convert.ToDouble(Console.ReadLine()); }
        catch (Exception e3) { goto top2; }

        int a = 1, b = 1, c=0;
        try { c = a / b; goto bottom; }
        catch (Exception e4) { Console.WriteLine(e4.Message); }
        finally { Console.WriteLine("at end"); }
            //finally runs if errors exists AND if no errors exist
            //.NET makes sure that if any code inside a try
            //does a return, a goto, or if the program fails
            //or is closed
            //then finally code will always run.
            //useful for closing files, cleanup, closes databases,
            //and writing things to log files
            bottom:
            Console.WriteLine(x);

            short ss = 32767;
            try { ss = checked((short)(ss + 1)); }
            catch (Exception e5) { Console.WriteLine("overflow"); }
            Console.WriteLine(ss);

        top3: Console.WriteLine("Enter third number");
            double x22 = 0;
            if (!double.TryParse(Console.ReadLine(), out x22)) goto top3;
            //vb out x22
            //  change it to <outattribute> byref x22 </outattribute>
            Console.WriteLine(x22);

            Console.ReadLine();
        }
    }
}

-----------------------------------------------------------------------------------------------

format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace formats2
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "1234.56", y = "22.80",z="bob";
            double a, b;
            int i;
            a = Convert.ToDouble(x);
            i = Convert.ToInt32(a);
            b = (double)a; //vb   = ctype(a,double)
            Console.WriteLine("{0,10}{1,15:c}{2,-15:f3}", z, a, b);
            Console.ReadLine();

        }
    }
}

No comments:

Post a Comment