|
txtName being a text field:
txtName.Text.ToString()
Noman Muhammad Aftab,
Software Mechanic
|
|
|
|
|
Yeah, that's indeed quite insecure. Obviously you should Cstr() the thing and then Ctype it to string for good practice.
|
|
|
|
|
I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.
|
|
|
|
|
I wonder if the person is trying to be paranoid-protective about the possibility that a property might return a type which is not a string, but features bidirectional widening conversions? As a hypothetical example, one might have a private string-interning pool. Properties of a class which would normally return string could instead return items from the pool. If the properties returned strings, then setting one such property to another would require generating a new string object and then doing some sort of dictionary lookup to locate the string in the pool. By contrast, if the objects returned intern-pool references, those steps could be eliminated.
One hazard with such an approach is that if such an object were passed to a function that expected something of type object, things would appear to work normally (the string would be extracted when the object was cast to a string) but the object that was kept would be a pool reference. This could cause an entire pool of interned strings to be kept alive even if there were no live references to anything that cared about the pool being a pool (just a reference to something thought to be a string).
|
|
|
|
|
The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.
|
|
|
|
|
J4amieC wrote: The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.
Naturally. On the other hand, they might have heard of some vaguely-similar situation in which a competent programmer who did understand what was going on had applied some seemingly-unnecessary cast, and figured that if a cast was needed there and they didn't understand any distinction between that case and the one at hand, they should apply a cast (or explicit ToString) in the present case as well.
|
|
|
|
|
Now that reminds me of a time I had to maintain a (batch) program that accessed RDB. It had the equivalent of ToChar(ToChar(somestring)) all throughout the SQL code. The guy who had been maintaining it said it was that way when got it and that he was told it had to be that way.
I called BS, removed the extraneous ToChar, and proved that it was unnecessary. I made some other changes and added some functionality and the resulting program ran in a quarter of the time.
|
|
|
|
|
J4amieC wrote: I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.
To true. Great for catching newbies when conducting job interviews.
Luckily though, the framework protects them from themselves:
public override string ToString()
{
return this;
}
Not perfect but at least there's not a whole redundant complex conversion of some kind taking place...
Before .NET 4.0,
object Universe = NULL;
|
|
|
|
|
I have being trying to modify a legecy vb com code. First of all, when it is vb you thing nothing can be right. Then you come across something that is shear madness
Public Function UpdateOrderState(ByVal oldState As Long, ByVal newState As Long)
Dim StockDir As Long, StockChange As Boolean
StockChange = True
Select Case 10 * oldState + newState
'Case 12, 32 added more options
Case 12, 32, 42, 52, 62, 72
StockDir = -1
'Case 21, 23 added more options
Case 21, 23, 24, 25, 26, 27
StockDir = 1
Case Else
StockChange = False
End Select
If StockChange Then
'Code removed
End If
End Function
What it is doing is that, it is trying to figure out a state change from one to another. All applicable states have values. Now, what if the value of the states change?
|
|
|
|
|
Yusuf wrote: Now, what if the value of the states change?
The code changes too! And on the plus side you get to recompile and deploy the new version.
That's what pros do
Just an irritated, ranting son of ... an IT guy.
At your trolling services
|
|
|
|
|
It's not even doing that much.... All it's doing is checking whether the state changed to/from 2 (Or neither)...
If oldState = newState Then
StockChange = False
Else if oldState = StockStates.WhateverTheHell2Means Then
StockDir = 1
Else if newState = StockStates.WhateverTheHell2Means Then
StockDir = -1
End If
Don't ya just hate legacy code?
|
|
|
|
|
Ian Shlasko wrote: Don't ya just hate crappy legacy code?
ftfy
|
|
|
|
|
I wonder if you ran code obfuscation on it, would it produce more readable code?
|
|
|
|
|
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
This looks crazy ... but I would not blame the programming language, the original developer of this piece of ... code? ... will write bad code in any language.
|
|
|
|
|
|
Every time you add a new state you need to change the code, recompile and release. And if that weren't enough the third time you'd need to rewrite the function to handle a State of 10.
Did you add the "Code removed" comment, or is this function actually doing nothing at all?
|
|
|
|
|
Jeremy Hutchinson wrote: Did you add the "Code removed" comment, or is this function actually doing nothing at all?
I did that only for CP. There is some code in that block.
|
|
|
|
|
I spent a VERY productive day today refactoring some code which I originally wrote in 1988 (22 years between code reviews!). The code reduced in size by about 90% once all the superfluous code was removed - superfluous now, vital when the code was originally written to get around 16 bit memory addressing and CPU speed issues. I can't believe the hoops I used to have to jump through to get things working.
You youngsters don't know how easy you have it!
Luckily, I had the forethought to surround large sections of code with //HACK tags explaining why some of these strange things were being done. This turned out to be one of the most time-saving coding guidelines I ever implemented!
<edit> Spelling
Paul Hooper
If you spend your whole life looking over your shoulder, they will get you from the front instead.
|
|
|
|
|
Paul Hooper wrote: You youngsters don't know how easy you have it!
Well in my day we had to program using binary switches. And we liked it!
|
|
|
|
|
My first computer's main improvement over older models was a kex keypad to replace the switches, but nothing beats typing in machine code programs directly
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'.
I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
|
|
|
|
|
ricmil42 wrote: in my day we had to program using binary switches
You had binary! You lucky get! When I started out I had to write an entire batch processes using only 1's. Scratched onto papyrus. Using Mr Babage's pocket knife!
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
|
|
|
|
|
Given your age, your memory still serves you well. Or are you having another episode?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
In my day, we had to whistle into a modem!
Before .NET 4.0,
object Universe = NULL;
|
|
|
|
|
So what's the horror?
|
|
|
|