- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
What's the difference?
Sorry, couldn't resist.
Admin
Yeah, seriously. Real men code in VB6!
*runs*
Admin
Long names for identifiers or depth of classes does not directly correspond to "more code". It's still a single statement, which is fine with me. Smalltalk, Java and Delphi programmers have dealt with this for years, new C# coders coming from a procedureal environment (Pascal, C, REXX, etc) think it's abhorrent to have such "busy" names for identifiers.
All I can say is... take a gander at any C or C++ header file for Windows and tell me the identifers for some of those constants don't throw you for a loop!
Admin
See, there's this fabulous thing called a using clause that lets you abbreviate namespace complexity.
Consider System.Console.Writeline.
You could do this:
using System;
using out = System.Console;
Now, you can do this:
out.WriteLine("foo");
Better yet, since you have assigned an identifier of "out" to System.Console, you can easily switch out System.Console to something else. I have done this very thing.
I wrote a static class called SocketReporter which is a multithreaded tcp server that resembles the console. As the app runs, all output going to out is sent to any tcp clients which are connected to the SocketReporter service when it's running. This makes it a breeze to debug Win32 service apps. I can run the .exe in the debugger and have it write to the system console when necessary, then swap over to SocketReporter with a conditional compile for Release mode when I'm running the service on a server and monitor its output.
Admin
Couldn't you just declare a variable and point it to System.Console?
Admin
Note that the Debug.WriteLine is only necessary if you want to print to the debug window, and could be replaced by either MessageBox.Show() or Console.WriteLine() (depending on whether it's a console or windows forms app), which would be a fairer comparison. You can also shorten it down quite a lot, either by using the "using" keyword to specify the namespace at the top of your source file, or by using a different choice of methods.
Oh, and your Java code wouldn't actually compile. You're missing a ')'.
How about this:
System.Windows.Forms.MessageBox.Show(Convert.ToInt32("ABC", 16));
which, if you have System.Windows.Forms included in your imported classes at the top of your .cs file, can become this:
MessageBox.Show(Convert.ToInt32("ABC", 16));
The actual conversion is the Convert.ToInt32() method. "But sir, I don't know how to read MSDN! Waaa!" http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemConvertClassToInt32Topic19.asp
On a slightly more controversial note, are we now judging languages solely on how many characters I need to type? I'll stick with C#, since it doesn't have classes infested with horrible Hungarian notation (unlike C++), and the IDE takes most of the work out of it anyway with Intellisense.
Admin
Darn will I ever get used to the way this forum works - I always forget I'm only on the first page. Anyway...
Er, isn't that what he did?
Admin
Yeah, relying on your compiler to tell you when you have compile errors 'encourages laziness'
Admin
LOL. I was just nitpicking, I know.
Admin
I'm self taught too.
The point I was making was that setting a variable equal to a number means that the variable contains just that - a number. It doesn't matter what syntax you use to set the value; the end result's the same.
Therefore, when you print it out again, of course it's going to print out as a decimal! It's not a bug and it's not a special feature. It's basic behaviour of the language.
<FONT face="Lucida Console" size=2> var hex1 = 0x0f;</FONT>
<FONT face="Lucida Console" size=2>does exactly the same thing as:</FONT>
<FONT face="Lucida Console" size=2> var hex1 = 15;</FONT>
<FONT face="Lucida Console" size=2>They both set the variable hex1 equal to the number 15.</FONT>
So when you print out the contents of hex1, you're going to get 15.
Admin
John,
why do some coders become touchy if someone says something that might possibly shed a bad light onto their language? No language is 'perfect'.
I have replied to the question: "will you smugly show a way to do it with 50% less code?" No more, no less.
I am not interested in something as useless as language bashing.
Admin
"I'll stick with C#, since it doesn't have classes infested with horrible Hungarian notation (unlike C++)"
I'm sorry - where in the standard C++ language is there any hungarian notation? In fact, the guys behind C++ are pretty much against hungarian notation, full stop.
Now some Microsoft libraries may have hungarian in them, but that's Microsoft. And they may have finally learnt their lesson if, as you imply, C# doesn't have it in its standard libraries.
Admin
I was somehow under the impression that a number var would retain the format it was defined in.
So it doesn't.
I can sleep again.
Admin
Sorry, I got a bit carried away. I just wanted to point out that it wasn't a fair comparison. But the point stands - the fact that JavaScript can do it in fewer characters doesn't make JavaScript a better language. If you weren't trying to make that comparison, I can't see wat point you were trying to make.
About hungarian notation - it's not just the standard library, there's also all the 3rd-party libraries, such as the VCL, STL or MFC. All these IIRC favour hungarian notation in some form, or like to use other forms of cryptic abbreviations for their member names.
Admin
VCL I don't know, and a google for it gives, as a first hit, a library of 'furry' art which I dare not click. MFC is MS, so doesn't count as 'third party'
STL may have some oddish naming conventions internally, but externally it's not hungarian..
Admin
VCL is Borland's Visual Component Library. Basically the STL for graphical controls. The STL I seem to remember having lots of strange names - didn't they have T (for Type) as a prefix to most class names?. MFC I count as 3rd party, unless you're saying it's standard C++. When "standard libray" was mentioned earlier, I assumed it was talking about standard C++ as defined by Bjarne Stroustrup, which MFC is not.
Admin
Ah, right. I know nothing of it.
Nope. That would be mfc or root or something like that. STL is nice clean 'vector<foo>' and the suchlike. There is '_Tp', which is a pointer to a type, but I'm fairly sure it's only used internally. STL is actually very readable and usable once you get the hang of it, but these days I don't torture myself with C++ unless I absolutely have to :)
RogueWave was all 'Collectable' and so on, IIRC, can't remember any hungarian there either. It was still horrible to use, though, and a monstrous pain if you upgraded versions.
It's been a while though.
Indeed. What I posted was somewhat misleading, I was trying to make the distinction between 'standard', '3rd party' and 'Microsoft'. My bad. I was discounting MFC due to its very microsoftness.
In summary, I'd say that, yes, there are 3rd party non-MS class libraries that use hungarian out there, but probably not the majority.
Simon
Admin
<FONT face="Courier New" size=2>s/abbreviate/abuse</FONT>
Admin
Microsoft Soviet-Era C++ programming:
BOOL CVeryBigClass::GetResult()
{
BOOL bBoolResult = (
GetScaryApiInfoEx(
m_hHandle,
m_nNumber,
m_lpszString,
NULL,
NULL,
NULL,
NULL,
// Many more lines of NULLs
// omitted for brevity
NULL)) ? TRUE : FALSE;
if (bBoolResult == TRUE)
return TRUE;
else if (bBoolResult != TRUE)
return FALSE;
else
return FALSE;
}
Admin
Well, at least this code can easily be changed to add very large hex numbers, much bigger than the largest value of <b>int</b>. That's something you can't do with Int.Parse(). It is obviously superfluous for 8 digit hex numbers; maybe the code was taken from a library that handles larger numbers, e.g. for calculating RSA keys. BTW: Building a solution with Int32.Parse()+Int32.Parse() would be faster, but give you wrong results when you have an integer overflow. To savely add two 8-digit hex numbers, you need at least 33 bit.
Admin
@tufty: We are talking about the same STL, right? As in Borland's Standard Template Library (IIRC, and it has been a while, Borland did pretty much the same thing as MFC - a component library, but split into visual and non-visual components). Guess it must just be the VCL and whatever they called their non-visual library (which I thought was called the STL but I guess not). I know that they have a lot of stuff like TData, TQuery and so on.
No worries
I dunno, both MS and Borland do it (though MS has indeed strated to ditch this strategy, if only for cross-language compatibility and cleanness in .NET) and most people take their cue from them. Certainly, the software (written in Delphi with a bit of C++) we produce at work uses it extensively, and it was what I was always taught to do. It might be different in the non-Windows world, though.
Admin
No, then. Most certainly not. If Borland have called their abortion 'STL', they are asshats of a fairly major calibre.
http://www.sgi.com/tech/stl/ is the STL
:)
Admin
A number is an abstract concept and does NOT have a base. A base only comes into play when your want to represent a number physically as a sequence of digits. Internally, all numbers are in fact stored in binary. Other bases only become relevant when there is a conversion between the internal format and a string. That's what happens to the number definition in the source code: the compiler's or interpreter's parser turns the string "<font face="Lucida Console" size="2">0x0f</font>" into the internal binary representation, using base 16 because it starts with 0x.. When you want to turn it back into a string, that's when the base again becomes relevant, the default being 10.
Admin
It isn't clear if the sign of the hex strings is to be treated like VB normally treats them. It appears that the two hex strings are added as unsigned (positive) values.
Unfortunately, VB only supports hex literals and operations that can be represented as a long integer. Long Integers are always signed.
There are at least three datatypes with enough bits to accomodate the largest possible value of the sum of two 8 hex digit strings (Double, Currency, Decimal). The problem with this approach comes in when trying to convert the sum back to hex characters (see prior point).
(snicker) <FONT face="Courier New">"= Right(h3,16)"</FONT>
I wonder if this code wasn't originally part of Excel VBA, with the <FONT face="Courier New">hex2dec()</FONT> function reference. ;-)
With no specific datatype declaratives, I assume that either this code is run in a scripting environment (ASP, VBScript) or performance has never been a concern of the programmer or the IT staff usually concerned with such things.
==========================
For brevity and efficiency, and sticking within the VB language, I would suggest the following solution.
<FONT color=#000000>My implementation example:</FONT>
<FONT face="Courier New">Function AddHex(hex1, hex2)
'Adds two hexadecimal strings (up to 8-digits each)</FONT>
<FONT face="Courier New"> Dim h1, h2, hRight6, Carry, hLeft4
'Step1 - convert to fixed length (8) strings
h1 = Right("00000000" & hex1, 8)
h2 = Right("00000000" & hex2, 8)
'Step2 - add right most 6 hex digits
hRight6 = Right("00000000" & Hex(CLng("&h" & Right(h1, 6)) + CLng("&h" & Right(h2, 6))), 8)
Carry = Left(hRight6, 2)
hRight6 = Right(hRight6, 6)
'Step3 - add the high-order digits and the carry (if any)
hLeft4 = Hex(CLng("&h" & Left(h1, 2)) + CLng("&h" & Left(h2, 2)) + CLng("&h" & Carry))
'Step4 and Step5 - concatenate the hex strings and make fixed length (20)
AddHex = Right("0000000000" & hLeft4 & hRight6, 10)
End Function</FONT>
=========================
If this needed to be extended to larger hex strings, I would probably use three numeric arrays, with each position holding the value of pairs of hex digits. The third array would be used for carry data. Output would consist of individual array item conversion from numeric value to hexidecimal string equivalence using the <FONT face="Courier New">Right("0" & Hex(),2)</FONT> function combinations as shown above.
Admin
Ah, VCL, STL and MFC. Third party libraries. One of these things is not like the others.
You WERE making some sort of obscure joke, right? Right?
*cries*
Admin
Read the exchanges between me and tufty.
I remembered where I got that idea from, I think. It was a college tutor. We were doing basic C++, and I asked if I should read about the STL. He said no, that was just a bunch of libraries written by Borland, similar to MFC, and the knowledge would be useless if you weren't using C++ Builder. I left that course soon after because they were cutting off the programming side in the second year (maybe that was why). That was the last time (apart from an abortive attempt at a game with some other guys) that I looked at C++. I decided I preferred C#.
Admin
In VB, the "&H" is the hex identifier. So whereas you would write something like this in C#:
int i = 0xFA;
You would write this in VB.NET:
dim i as integer = &HFA
Or this in VB6:
dim i as integer
i = &HFA
As for C#'s conversion of hex strings:
string hexValue = "FA";
int i = Convert.ToInt32(hexValue,16); // second parameter indicates the base to convert from
Admin
Sorry ... I'm new to the board and still not sure how to include the original message in my reply. The above message was meant as a reply to the message that said:
"Ok I'm not familiar with VB at all...what does "&H" accomplish? And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language. Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"..."