Re: C# and C++ (again)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.
--
"Tad Marshall" <tad@xxxxxxxxxxxxxxx> wrote in message
news:%23gH6s65QFHA.3076@xxxxxxxxxxxxxxxxxxxxxxx
> 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
>
> "Tad Marshall" <tad@xxxxxxxxxxxxxxx> wrote in message
> news:OL$%23es5QFHA.3096@xxxxxxxxxxxxxxxxxxxxxxx
>> 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
>>
>> "Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx> wrote in message
>> news:mZ-dnWDGdrCzJ__fRVn-sQ@xxxxxxxxxxxxxx
>>> 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.
>>> --
>>> "Tad Marshall" <tad@xxxxxxxxxxxxxxx> wrote in message
>>> news:OtCKDy1QFHA.1172@xxxxxxxxxxxxxxxxxxxxxxx
>>>> 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.
>>>>
>>>>
>>>
>>>
>>
>>
>
>


.



Relevant Pages

  • Re: C# and C++ (again)
    ... array into a structure. ... language where I will be most productive. ... be prepared that you won't get the structs and unions ... > long as you are in managed code, you will have to start with these ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: nonhomogenous structs (was: lisp performance questions and observations)
    ... We programmers tend to view the instruction level of an architecture ... This is reinforced by the limited hardware courses we ... The conclusion is that in general, vectors of non-homogenous structs ... > naturally represented as an array of structs, ...
    (comp.lang.lisp)
  • Re: array of struct from c++ to c#
    ... I currently have a single struct being passed from a C++ dll to a C# ... I'm now trying to pass an array of structs back to ...
    (microsoft.public.dotnet.framework.interop)
  • Re: * struct-like list *
    ... Age: 108 Birthday: 061095 SocialSecurity: 476892771999 ... I would like to have an array of "structs." ... I want to go through the file, filling up my list of structs. ... DOB: 061095 ...
    (comp.lang.python)
  • Re: Allocating structs on the stack
    ... > I am preparing for a class this fall that teaches C# programming. ... One big academic issue to me is that use of structs in ... > an array, in which case the rule seems to not be implemented). ... The compiler doesn't know whether an array element has been fully ...
    (microsoft.public.dotnet.languages.csharp)