0

Total beginner jumping in the deep end. This is my first post here.

I'm a mainframe programmer trying to join the 1990's and learn some object oriented programming(which is currently greek to me). Please forgive my elementary explanation below.

I'm developing an application which needs to keep an availability calendar for people on a Sql Server DB. Since I could have tens of thousands (or more) rows, I was hoping to be as efficient as possible and not waste space.

Being totally green to C#, I came across BitVector 32 and realized that if I simply ignore the 0 bit, I'll have a Boolean value (True/False to describe each persons availability) for every day of the month (32 bits for 31 days in the month, with some left over for February, etc).

BitVector32 availability = new BitVector32();

This handles one month, but I could then create an array of 12 bitvector32 values, and have the entire year covered in 48 bytes (bitvector32 utilizes 4 bytes X 12 months).

I have no problem instantiating and populating a bitvector32 structure by isolating individual bits. I then pass availability.data to a method to write it out to a Sql Server Table defined as an Int value:

UpdateDB(key1, key2, availability.Data);

     static void UpdateDB(int key1, int key2, Int32 avail)
     {
         using (SqlConnection connection = new SqlConnection etc etc etc) 
         {
             connection.Open();
              string selectString = @"Update VDB SET AVAILABILITYMONTH = @avail WHERE KEY1 = @key1 AND KEY2 = @key2";
             SqlCommand cmd = new SqlCommand(selectString, connection);
             cmd.Parameters.AddWithValue("@key1", key1);
             cmd.Parameters.AddWithValue("@key2", key2);
             cmd.Parameters.AddWithValue("@avail", avail);
             cmd.ExecuteNonQuery();
             connection.Close();
         }
     }

My problem is my next program where I need to read this integer value and convert or cast(?) it back to the Bitvector32 format.

I've tried this:

BitVector32 availability = new BitVector32();
availability = myReader.GetInt32(0);

but I get an error "Cannot implicitly convert type 'int' to 'System.Collections.Specialized.Bitvector32"

I'm right on the edge of nailing this process and I suspect it's a simple keyword or cast but I haven't been able to find it. Any ideas?

Also, is this the right approach? I didn't want to do a Boolean array for the entire year that would take up excess space.

Any input would be appreciated.

2 Answers2

2

You may use a BitVector32 constructor which takes int argument:

BitVector32 availability = new BitVector32(myReader.GetInt32(0));
AlexD
  • 30,405
  • 3
  • 66
  • 62
  • ok - beginner question #2. I'm populating this structure with each read of the database; does that mean I'm instantiating 'availability' each time I read a row? – Dimitri Hotchingurtis Aug 26 '14 at 23:27
  • You instantiate `availability` each time you do `new`. However it is not an expensive operation. – AlexD Aug 26 '14 at 23:37
  • ok - that was it! This site is awesome - thanks! (edited to add: whoops - just saw that I shouldn't be thanking yourself and Evan - but I will this time anyway; rookie mistake) – Dimitri Hotchingurtis Aug 26 '14 at 23:58
0

I think you're looking for this;

BitVector32 availability = new BitVector32(myReader.GetInt32(0));

BitVector32 has a constructor which takes an Int32 however you can't just assign an Int32 to a BitVector32 because they are not the same type and C# doe not do implicit conversions like that.

evanmcdonnal
  • 38,588
  • 13
  • 84
  • 107