Click here to Skip to main content
15,885,213 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I write this code for union and unity of binary variable in C# but when I run it, the console just open and close. what is its problem?
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
public class CBoolOperate
{
    int[] a = null;
    int[] b = null;
    int[] c = null;
    CBoolOperate()//default constructor
    {
    }
    ~CBoolOperate()//default destructor
    {
    }
    public void get()
    {
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            a[i] = Console.Read();
        }
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            b[i] = Console.Read();
        }
    }
    public void union()//A member function
    {
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 || b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public void and()//A member function
    {
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 && b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public int main()
    {
        CBoolOperate r=new CBoolOperate();
        r.get();
        r.union();
        r.and();
        return 0;
    }
}



Please help me.

Thanks in Advance.
Posted
Updated 12-Jun-11 19:01pm
v2

Hello dear,
I think you are a beginner in programing
I correct you code and it works well
Because constructor of CBoolOperate was private i make CBoolOperate.main as static , instead it could be

public CBoolOperate()//default constructor
{
}
and 
static void Main(string[] args)
{
    CBoolOperate A = new CBoolOperate();
    A.main();
    Console.ReadLine();
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CBoolOperate.main();
            Console.ReadLine();
        }
    }
}
public class CBoolOperate
{
    int[] a = null;
    int[] b = null;
    int[] c = null;
    CBoolOperate()//default constructor
    {
    }
    ~CBoolOperate()//default destructor
    {
    }
    public void get()
    {
        this.a = new int[5];
        this.b = new int[5];
        this.c = new int[5];
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
        Console.WriteLine("please enter first 5 binary digit\n");
        for (int i = 0; i < 5; i++)
        {
            b[i] = Convert.ToInt32(Console.ReadLine());
        }
    }
    public void union()//A member function
    {
        Console.WriteLine("result of union for numbers");
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 || b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public void and()//A member function
    {
        Console.WriteLine("result of and for numbers");
        for (int i = 0; i < 5; i++)
        {
            if (a[i] == 1 && b[i] == 1) c[i] = 1;
            else c[i] = 0;
            Console.WriteLine(c[i]);
        }
        Console.WriteLine("");
    }
    public static int main()
    {
        CBoolOperate r=new CBoolOperate();
        r.get();
        r.union();
        r.and();
        return 0;
    }
}
 
Share this answer
 
v2
Comments
Member 7904482 13-Jun-11 1:38am    
thanks
Sergey Alexandrovich Kryukov 13-Jun-11 1:39am    
This is a correct fix, my 5.
Could you format code properly? It needs pre tags and structure.
But I already explained everything, did you read my solution?
--SA
This is because your static method Program.Main (which is a real entry point of your program) does nothing. Your probably thought that your other method main is an entry point. No. It is not an entry point of the program; it won't be called.

The default entry point is a static method Main with compatible signature. You can change it in your project's Properties.

By the way: Don't ever write a constructor or destructor which don't do anything. Destructors are almost never used because of Garbage Collector (GC) operation.

—SA
 
Share this answer
 
v3
Comments
Member 7904482 13-Jun-11 1:12am    
can you help me more
I dont understand what you mean
Sergey Alexandrovich Kryukov 13-Jun-11 1:33am    
Oh, move your code from "main" to "Program.Main". (Reza did it for you.) You "main" is not the entry point of the program, never called. Better yes, remove class Program, rename "main" to "Main" and rename it to static.

But I would suggest you understand my answer as is. If you don't understand anything, ask me about it. It's important. Entry point is a static function called by the .NET first when it loads the application and tries to run it. Anything else you want me to explain?
--SA
CS2011 13-Jun-11 1:22am    
All though you can have more than one main in c# program and you can specify which one you want to use however as you pointed out that was not the case here.But I have one doubt that what difference it make if I use ReadLine(true) or ReadLine();
Sergey Alexandrovich Kryukov 13-Jun-11 1:37am    
Here this is not the case, because entry point should be static. OP though "main" is entry point, but real entry point is "Program.Main", which does nothing.

A doubt about ReadLine? (ReadKey, not readling.) See MSDN: ReadKey(true) suppresses echo of the key pressed as we usually want. In this case, the method just blocks the thread until key is pressed -- exactly what is needed in such cases.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900