Click here to Skip to main content
15,888,208 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: convert date formats Pin
stephan_0071-May-08 0:28
stephan_0071-May-08 0:28 
AnswerRe: convert date formats Pin
PIEBALDconsult1-May-08 9:34
mvePIEBALDconsult1-May-08 9:34 
Questionc# form resize speed Pin
leeoze30-Apr-08 23:44
leeoze30-Apr-08 23:44 
AnswerRe: c# form resize speed Pin
Zoltan Balazs1-May-08 0:43
Zoltan Balazs1-May-08 0:43 
QuestionActive Directory - Null Reference Exception Pin
hobbsieoz30-Apr-08 22:59
hobbsieoz30-Apr-08 22:59 
AnswerRe: Active Directory - Null Reference Exception Pin
Anthony Mushrow1-May-08 0:12
professionalAnthony Mushrow1-May-08 0:12 
GeneralRe: Active Directory - Null Reference Exception Pin
hobbsieoz1-May-08 0:33
hobbsieoz1-May-08 0:33 
AnswerRe: Active Directory - Null Reference Exception Pin
Anthony Mushrow1-May-08 0:45
professionalAnthony Mushrow1-May-08 0:45 
Questionfunction conference Pin
asma_pfe30-Apr-08 22:24
asma_pfe30-Apr-08 22:24 
AnswerRe: function conference Pin
Christian Graus30-Apr-08 23:05
protectorChristian Graus30-Apr-08 23:05 
GeneralRe: function conference Pin
asma_pfe1-May-08 0:09
asma_pfe1-May-08 0:09 
GeneralRe: function conference Pin
Christian Graus1-May-08 2:58
protectorChristian Graus1-May-08 2:58 
GeneralRe: function conference Pin
Bert delaVega1-May-08 4:12
Bert delaVega1-May-08 4:12 
GeneralRe: function conference Pin
asma_pfe2-May-08 3:21
asma_pfe2-May-08 3:21 
QuestionI am not able to see large icon in list view Pin
Naveed72730-Apr-08 21:48
Naveed72730-Apr-08 21:48 
AnswerRe: I am not able to see large icon in list view Pin
Christian Graus30-Apr-08 23:07
protectorChristian Graus30-Apr-08 23:07 

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.