C# and C++ (again)

  • Thread starter Thread starter Tad Marshall
  • Start date Start date
T

Tad Marshall

Hi,

I wrote some code that works with a SQL Server 2000 database, and all the
examples I was finding used VB.NET, so that's what I picked as a language.

This wasn't the right choice for me. My background is in Win32 programming
in C++, and I designed stuff that really wanted to use unions and static
casts, and VB.NET just let me down. The code works, but it's very clumsy
code and I'd rather have it in a language better suited to what I'm doing.

I'm going to rewrite the code into some version of C, which will not be that
big a deal. I have just under 2000 lines of VB.NET code and it will end up
being around 1500 lines of either C++ or C# code.

Given that I was a happy camper when I did Win32 C++ programming, is C++
still the best language for me to use when I rewrite my VB.NET code? Or
would it be worthwhile for me to take a few days to become comfortable with
C# and use that instead?

My impression so far is that C# is sort of a "cleaned up" version of C++,
with better syntax for the kinds of things that .NET does but fundamentally
fairly similar to C++. Does this seem like an accurate assessment to you?

Many of you must have switched from C++ to C#. Was it basically painless
and something you were happy to do? Or would you really have been more
happy staying with C++?

I could go either way. There is nothing especially wrong with VB.NET, but
it just isn't the best language for me given my background and the way that
I like to write things. I am quite sure that I could translate this code
into either C++ or C# given a few days to get up to speed on the .NET issues
in C++ and learn the C# syntax. I am using a small subset of .NET and have
no issues with that part of the problem. My C++ code could be managed or
unmanaged so long as I can access the ADO.NET and DateTime functions I'm
using (which I guess would all be managed code) and there is nothing that I
am doing that requires unmanaged code. But, my code would be better and I
would be more productive in working on it if it was in some flavor of C.

If you have any advice that you feel like offering, it would be very helpful
to me. Thanks!

Tad

P.S. I'm using VS.NET 2003 on Windows XP, and VB.NET is working just fine
.... I do like IntelliSense and hit the tab key quite a lot as I code. I
assume that this works about the same way in C# and probably C++, but I have
no experience to support this conjecture.
 
Hi Tad,

I was a C person for many years before doing VB, then a VB person before
switching to C# in the .Net wave.

If you are more comfortable with C-style syntax then, by all means, switch
to C#. However, be prepared that you won't get the structs and unions that
you used to get in C/C++.

C assumed a specific character set and a specific byte order. This made
structs and unions very easy to use to convert binary values and translate
types.

The .Net framework was written from a multi-lingual multi-cultural
standpoint. As a result, we can no longer assume the same things. As long
as you are in managed code, you will have to start with these assumptions.

So your translation from VB.Net to C# may not yield fewer lines of code, as
you suggest. The total lines of code are likely to be the same.

That said, I'd recommend that you take the time to jump to C# if you want to
move over to the C side of the house. To be honest, I find it to be a more
satisfying and fun language than many other languages I've used. That's
personal, of course. I know of many folks who are much happier with VB.Net,
and that is fine as well.

Just be forewarned... some of the things you can do in C or C++ are not
available in managed code. Not in Java either. You may not get what you
are looking for.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Hi Nick,

Thanks a lot for the thoughtful response. You very accurately saw what I
was hoping for and set me straight.

The bit of code that annoyed me the most was a really stupid Select/Case
statement that I had to write because VB.NET did not seem to let me embed an
array into a structure. I have some data that is essentially a three
dimensional array, and in C, this would have been something like

struct myItem {
int itemKey;
float itemInfo(3,3,3);
}

and in VB.NET I ended up writing a routine to take an index between 0 and 26
and look up the itemInfo field I wanted. Pure annoyance from my
perspective.

I was also disappointed when I tried to set a memory breakpoint in VS.NET
2003. I have used these breakpoints for years in Win32 C++ code, and
sometimes they are the perfect way to chase down a bug. VB.NET said that
these breakpoints were not available in VB.NET. Perhaps they are also not
available in C#.

I'm leaning towards using C++ for my rewrite, based on what you have told
me. My current VB.NET code is around 40% .NET calls and 60% hand-written
code, or something like that. In converting to some C flavor, my Dim i As
Integer becomes "int i;" and all the ByVal notation goes away, so I bet it
is more concise in C(++ or #) than in VB.

I don't regret coming up to (slow) speed in VB.NET, but it just isn't the
language where I will be most productive. I am writing some software that
only I will be running, and if making my hand-written code be unmanaged is
the way to go, that is fine with me. I need a pair of 500 element
structures, each of which holds a 3 by 3 by 3 array, and this is really easy
in C++. An array of structures that include arrays does not push this
language very hard. VB.NET wanted a ReDim state for each element ... ah, no
thank you.

On this array thing, it would be very nice to treat this 27 element array as
either 3 by 3 by 3 or just a list of 27 numbers. That's what I could have
done in C ... just declare a union and I am done. Does this mean that I
will only be happy in C++, and unmanaged C++ at that?

Thanks for the great response!

Tad
 
The other "C trick" that I wanted to use is to take a 100 element array,
grab one element from it, and pass it as a single thing to a subroutine.
The subroutine would think that it had just one structure to look at, and
would know nothing about the array that it was pulled from. Simple casting
in C++ makes this easy ... I couldn't figure out a way to do the same thing
in VB.NET. Possibly I just don't know VB.NET well enough. I ended up
passing all the array indices to the subroutine, though they are none of its
business and just add code bulk and the possibility of typos.

It sounds like I should be writing this code in unmanaged C++ ... do you
agree?

Thanks!

Tad
 
Hi Tad,

Actually VB.Net can do all of the things you want it to do. So can C#. You
aren't really using the features I was thinking of. (I used to do embedded
firmware in C for data communications devices... Unions and Structs were a
great way to quickly parse a binary stream. You aren't doing that).

The answer is to use classes, not structs. It is trivial to create a class
that will perform the functions that your 27-element structure was
performing, in a clean manner. No need to break apart a portion of an array
and send it to a function... simply make the function into a method of an
object that contains a 3x3x3 array.

You can create and use an array that is 3x3x3 simply in VB.Net.
Dim Multiarray(3,3,3) as Integer
Multiarray(2,1,1) = 4

The next thing I'd recommend is to buy a good book on OO development. Your
text leads me to believe that you are walking away from so much of the real
power of the language.

Note: C#: same comments, different syntax

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
I don't regret coming up to (slow) speed in VB.NET, but it just isn't the
language where I will be most productive. I am writing some software that
only I will be running, and if making my hand-written code be unmanaged is
the way to go, that is fine with me. I need a pair of 500 element
structures, each of which holds a 3 by 3 by 3 array, and this is really easy
in C++. An array of structures that include arrays does not push this
language very hard. VB.NET wanted a ReDim state for each element ... ah, no
thank you.

You shouldn't need to actually use ReDim as you're not resizing
anything, but without seeing the code it's hard to say exactly what you
should have done.
On this array thing, it would be very nice to treat this 27 element array as
either 3 by 3 by 3 or just a list of 27 numbers. That's what I could have
done in C ... just declare a union and I am done. Does this mean that I
will only be happy in C++, and unmanaged C++ at that?

It's easy to create the same kind of effect - but in a much more OO
way. Your struct becomes:

public class MyItem
{
int key;
float[,,] info = new float[3,3,3];

public int Key
{
get { return key; }
set { key = value; }
}

public float this (int index)
{
get { return info[index/9, (index%9)/3, index%3]; }
set { info[index/9, (index%9)/3, index%3]=value; }
}

public float this (int i1, int i2, int 3)
{
get { return info[i1, i2, i3]; }
set { info[i1, i2, i3]=value; }
}

}

(Assuming you want it to be a reference type rather than a value type -
the default position should normally be to create reference types, but
if necessary it wouldn't be hard to convert the code to be a struct
instead of a class.)

You'd then use:

MyItem item = new MyItem();
item.Key = 60;
item[2,1,0] = 50f;
item[10] = 10f;

etc
 
Back
Top