Click here to Skip to main content
15,913,055 members
Home / Discussions / C#
   

C#

 
GeneralRe: Magnificent Pin
Spacix One1-May-08 13:26
Spacix One1-May-08 13:26 
GeneralRe: Magnificent Pin
Luc Pattyn1-May-08 13:46
sitebuilderLuc Pattyn1-May-08 13:46 
GeneralRe: Magnificent Pin
George_George1-May-08 21:35
George_George1-May-08 21:35 
GeneralRe: Finalization and Performance Pin
George_George1-May-08 21:18
George_George1-May-08 21:18 
QuestionC# compiler expand our code? Pin
George_George1-May-08 3:05
George_George1-May-08 3:05 
AnswerRe: C# compiler expand our code? Pin
Christian Graus1-May-08 3:16
protectorChristian Graus1-May-08 3:16 
GeneralRe: C# compiler expand our code? Pin
George_George1-May-08 3:45
George_George1-May-08 3:45 
GeneralRe: C# compiler expand our code? Pin
Christian Graus1-May-08 3:55
protectorChristian Graus1-May-08 3:55 
GeneralRe: C# compiler expand our code? Pin
George_George1-May-08 4:08
George_George1-May-08 4:08 
GeneralRe: C# compiler expand our code? Pin
Simon P Stevens1-May-08 4:49
Simon P Stevens1-May-08 4:49 
GeneralRe: C# compiler expand our code? Pin
George_George1-May-08 20:34
George_George1-May-08 20:34 
GeneralRe: C# compiler expand our code? Pin
Simon P Stevens2-May-08 9:37
Simon P Stevens2-May-08 9:37 
GeneralRe: C# compiler expand our code? Pin
George_George2-May-08 21:26
George_George2-May-08 21:26 
QuestionHelp on Writing Image Information(Location/Descriptions) to a txtfile Pin
Thomas Toh1-May-08 2:12
Thomas Toh1-May-08 2:12 
AnswerRe: Help on Writing Image Information(Location/Descriptions) to a txtfile Pin
Christian Graus1-May-08 3:18
protectorChristian Graus1-May-08 3:18 
GeneralRe: Help on Writing Image Information(Location/Descriptions) to a txtfile [modified] Pin
Thomas Toh1-May-08 3:42
Thomas Toh1-May-08 3:42 
GeneralRe: Help on Writing Image Information(Location/Descriptions) to a txtfile Pin
Christian Graus1-May-08 3:45
protectorChristian Graus1-May-08 3:45 
AnswerRe: Help on Writing Image Information(Location/Descriptions) to a txtfile Pin
Thomas Toh1-May-08 4:25
Thomas Toh1-May-08 4:25 
QuestionReturn Value Pin
MumbleB1-May-08 1:16
MumbleB1-May-08 1:16 
AnswerRe: Return Value Pin
Anthony Mushrow1-May-08 1:24
professionalAnthony Mushrow1-May-08 1:24 
GeneralRe: Return Value Pin
MumbleB1-May-08 1:41
MumbleB1-May-08 1:41 
GeneralRe: Return Value Pin
Anthony Mushrow1-May-08 1:48
professionalAnthony Mushrow1-May-08 1:48 
GeneralRe: Return Value Pin
Spacix One1-May-08 9:00
Spacix One1-May-08 9:00 
RantRe: Return Value Pin
Spacix One1-May-08 9:39
Spacix One1-May-08 9:39 
Kwagga,

I hate to be "that" guy, but you have some common mistakes in those functions I'll gladly help you with Smile | :)

to start with string retval = ""; is bad in a few ways. In .NET strings can't change, each time you "change" it what really happens is a new string is made and the name (which is a point to the string location in memory) gets referenced to the new string. You can see this when you do the following:
string myString = "Hello World!";
string anotherString = myString
What happens is that myString and anotherString are just pointing to the same location in memory.

So when you have
string retval = "";
retval = "my Return value";
return retval;
you have useless string objects, which waste resources. Though the waste is very small normally, it can add up.
If you are looping though strings as in this pointless example:
string retval = "";
for(uint i=0; i < 10000; ++i)
{
    retval += "a";
}
You'd end up with 10000 unreferenced and unaccessible (wasted space) strings, with the 10001 string being your final accessable string pointed to retval
Though this is beside by point, there are better ways to do the above example using a StringBuilder which can change/be changed without making a copy of itself, but this is another topic all together.


You don't want these wasted resources as they can add up and waste memory before they get destroyed by the .NET Garbage Collecter (commonly called the GC)

You don't need to initialize an int's to zero either, even more so since the TryParse() function uses an "out" pass by reference instead of a "ref" means that method doesn't (and can't) use the initial value passed, so the input vale is scrapped and overwritten.

For your first function the following would be about 4 or 5 times better:
private string SearchPcode(string inputStr)
{
    int pcode;
    if (!int.TryParse(inputStr, out pcode))
    {
        return "Invalid Integer!";
    }
    else if ((pcode >= 4731) && (pcode <= 6499))
    {
        return "Eastern Cape";
    }
    else
    {
        return "Invalid PostCode";
    }
}
This is using the common practice of "multiple exit points" some people don't like those, but I do Smile | :)

You could use the following if you don't like the muli exit method:
private string SearchPcode(string inputStr)
{
    string retVal;
    int pcode;
    if (!int.TryParse(inputStr, out pcode))
    {
        retVal = "Invalid Integer!";
    }
    else if ((pcode >= 4731) && (pcode <= 6499))
    {
        retVal = "Eastern Cape";
    }
    else
    {
        retVal = "Invalid PostCode";
    }
    return retVal;
}
The reason I don't like this is that you have the extra string object, but one little string isn't that bad if it is easier for you to follow/program.

I've ranted enough at you I guess, but there at lots of these little optimizations you can do to the other function as well. As for your business code in the display layer. I'm not one of the people that think these are to be 100% separated and need to follow those "strict" (and at times HARD even for experienced coders) rules....


-Spacix
All your skynet questions[^] belong to solved

Questionconvert date formats [modified] Pin
stephan_0071-May-08 0:14
stephan_0071-May-08 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.