0

Read and Write to Text Files in c#

Category:


There are many ways to read and write to text files. We will see one option here. Be sure to add using System.IO namespace.
This is a function to write to a Text File.
private bool writeToTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        //data to be written in the file
        //\r\n is to insert a new line
        string data = "This is the first line \r\n" +
                      "This is the second line \r\n\r\n" +
                      "This is the third line";
        try
        {
          File.WriteAllText(fileName, data);//write the data to the text file
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }
In the above function,\r\n is used to insert a new line.
This function is used to read from text file.
 
      private bool readFromTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        try
        {
            string data = File.ReadAllText(fileName);//read from text file
            data = data.Replace("\r\n", "");//\r\n is replaced by
            Response.Output.Write(data);           
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }

0

Operator Overloading Page 2

Category:

class operatordemo
{
int a, b;
string s1, s2;
public operatordemo()
{
a = 10; b = 20; s1 = "csharp"; s2 = ".net";
}
public static int operator +(operatordemo r1, operatordemo r2)
{
int tot;
string str;
str = r1.s1 + r2.s2;
tot = r1.a + r2.b;
MessageBox.Show(str);
return tot;
}
}
private void button1_Click(object sender, EventArgs e)
{
operatordemo obj1 = new operatordemo();
operatordemo obj2 = new operatordemo();
int t;
t = obj1+obj2;
MessageBox.Show(t.ToString());
}

Press F5 and you see this Outputs



GOT it?
First Page in Operator Overloading

0

Operator Overloading Page 1

Category:

Operator Overloading:
                Using operator overloading we can change the meaning to the existing operator, All operator over-loading functions should be static, Using operator keyword we can over-load unary-operator and binary operators.

Example on Operator Overloading:

Start Microsoft Visual Studio - Click on file- New- Open New Project


Name your Project in the given field and press OK button, there you see a form that is in design mode

Add a Button Control on to your form, from ToolBox


Double click on Button here comes the Coding Part


0

Function Overloading Page 2

Category:

You need to include this Overloading functions in Our Class Named "emp" as shown below

class emp
{
       int ts;
//I am creating three functions using same name
       public void tsal(int sal)
       {
             MessageBox.Show(sal.ToString(),"Salary");
       }
       public void tsal(int sal,int bonus)
      {
            ts = sal + bonus;
            MessageBox.Show(ts.ToString(),"Salar+Bonus is ");
      }
      public void tsal(int sal, int bonus, int ta)
      {
             ts = sal + bonus + ta;
             MessageBox.Show(ts.ToString(),"Salary+Bonus+ta is ");
       }

}
private void Form1_Load(object sender, EventArgs e)
{

}
//I am calling three functions using button click event
private void button1_Click(object sender, EventArgs e)
{
      emp e1 = new emp();
      int s, b, t;
      s = 5000;
      b = 300;
       t = 200;
       e1.tsal(s);
       e1.tsal(s,b);
       e1.tsal(s,b,t);

}

Coming to the Output Use f5

You see a window as shown below


Click on Button

Output1
You see Salary=5000then click OK
Output2


You see Salary +Bonus =5300
Output3

You see Salary+Bonus+ta =5500
Got it?
To know about Operator Overloading Click Here

0

Function Overloading Page 1

Category:

Function Overloading

                          Using function overloading we can implement any no.of functions with the same name. by providing different prototypes or Arguments, While implement the function overloading we have to check only with function name and with the parameters, not with the return types.

Example on Function Over-loading

Start Microsoft Visual Studio - Click on file- New- Open New Project


Name your Project in the given field and press OK button, there you see a form that is in design mode

Add a Button Control on to your form, from ToolBox


Double click on Button here comes the Coding Part


0

Designtime Binding

Category:

Designtime Binding:
                                 In the design time binding the total instructions we can give using design mode to the compiler the design time binding we can classify in to two types

  1. Function Overloading
    • void tsal()
    • void tsal(int s)
    • void tsal(int s,int b)
    • int tsal();
  2. Operator Overloading
    • 10+20
    • str+str
    • obj+obj

0

Polymorphism

Category:

Polymorphism :
                           The polymorphism is the concept of sharing and ability features to the multiple forms, The polymorphism. we can classify into two types

  1. Designtime Binding
  2. Runtime Binding
    • virtual function

0

Random Topics for Intermediate Level

Category:

Oops Concepts

  • Data Encapsulation
  • Data Abstraction
  • Inheritance
  • Polymorphism

0

Getting Started with C# .NET Page 3

Category:

by Emmaneale mendu
The code itself will look very complicated, if you're new to programming. We'll get to it shortly. For now, right click the Program.cs tab at the top, and click Close from the menu that appears:
Now double click the Program.cs file in the Solution Explorer:

When you double click Program.cs, you should see the code reappear. So this code is the programme that will run when anyone starts your application.

Now click the plus symbol next to Properties in the Solution Explorer above. You'll see the following:





The file called AssemblyInfo.cs contains information about your programme. Double click this file to open it up and see the code. Here's just some of it:


The reddish colour text is something you can change. You can add a Title, Description, Copyright, Trademark, etc.

But right click the AssemblyInfo.cs tab at the top, and click Close from the menu. Now, in the Solution Explorer, click the plus symbol next to References:
These are references to code built in to C#. Much later, you'll see how to add your own files to this section.

Before we add some code, let's save the project. We'll do that in the next part below.

Previous Page