Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

One of members write this code:
now I want variables just take 0 or 1
how do I do it?
C#
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;
  }
}


Please help me.

Thanks in Advance.
Posted
Updated 12-Jun-11 20:11pm
v3

1 solution

Best way to do it is using enumeration:

C#
enum TwoValueType { Zero, One, }

int initialValue = (int)TwoValueType.Zero;


If you need numeric type such as int, you will have to type cast like in the last line in the code sample above.

Honestly, remove your constructor and destructor already, and never write them again! Write only the constructor when you need to do something. Never use destructor unless you understand very well why doing so.

—SA
 
Share this answer
 
Comments
Member 7904482 13-Jun-11 1:57am    
thanks
but where I have to use them?
could you edit my code?
thanks
Sergey Alexandrovich Kryukov 13-Jun-11 3:03am    
How can I know that? You did not explain which variable should be 0 or 1 and why.
--SA
Sergey Alexandrovich Kryukov 13-Jun-11 3:08am    
OK, are you trying to implement a class to represent a set? Your operations look and call like that. The whole idea is wrong. First of all, this is not memory-effective. You need to make a set of bits, not a set of 32-bit integers (!). You don't need an integer type with 0 or 1 values. It's a bit in a bit set.

Frankly, this class is not needed at all. Enumeration itself provide all you need for all those operations.
--SA
Sergey Alexandrovich Kryukov 13-Jun-11 3:10am    
More problems: you class should be abstracted from I/O, but you're putting Console.Read, etc. in it. Is it just during development? Even for development, I would not advice you to do so.
--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