Results 1 to 19 of 19

Thread: plz explain this code

  1. #1
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Question plz explain this code

    if (i.ToString().Length > 2)
    {
    i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    }

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    This seems to me, to be some sort of assignment. Noone in their right mind would use code like this to do, what it does.
    Try putting this code into a function like so:

    CSharp Code:
    1. private void myfunction(int i)
    2.         {
    3.             Console.WriteLine(String.Format("Value of i before: {0}", i));
    4.  
    5.             if (i.ToString().Length > 2)
    6.             {
    7.                 i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    8.             }
    9.  
    10.             Console.WriteLine(String.Format("Value of i after: {0}", i));
    11.         }

    and try executing the function with various values of i (ranging from 1 to 10000 to see a pattern).

    Pretty soon you'll realize, that the function can be written quite simply as:
    CSharp Code:
    1. i = i {some C# operator} {some value};
    (you will ofc have to figure out the operator and value by yourself ).

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain this code

    Quote Originally Posted by ThomasJohnsen View Post
    This seems to me, to be some sort of assignment. Noone in their right mind would use code like this to do, what it does.
    Try putting this code into a function like so:

    CSharp Code:
    1. private void myfunction(int i)
    2.         {
    3.             Console.WriteLine(String.Format("Value of i before: {0}", i));
    4.  
    5.             if (i.ToString().Length > 2)
    6.             {
    7.                 i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    8.             }
    9.  
    10.             Console.WriteLine(String.Format("Value of i after: {0}", i));
    11.         }

    and try executing the function with various values of i (ranging from 1 to 10000 to see a pattern).

    Pretty soon you'll realize, that the function can be written quite simply as:
    CSharp Code:
    1. i = i {some C# operator} {some value};
    (you will ofc have to figure out the operator and value by yourself ).

    Regards Tom
    @ThomasJohnsen: If we're trying to convert i to an Integer here (int) then why is your function asking for an int param which is variable i? I would be giving a few of my opinions about this code here if that was really what was going on. Trying to cast over to a string using the ToString() method just to analyze the length of the int value, and if longer than 2, then only take the last 2 digits from the string and cast over to an int. Furthermore if that was the case then I would be using Integer.Parse() as it's more optimized for string input and faster than Convert.ToInt32().

    My explanation: If variable i cast to a string, has a length (based on number of chars within the string), which is greater than 2 then... Convert the substring of variable i's last 2 char's to a 32 bit signed integer.

    Although with this code you wouldn't even need the second param of the SubString function here, and depending on what variable i is, I may be able to explain further anomalies here too. Hopefully i isn't already of type String... Otherwise that's even worse lol.
    Last edited by AceInfinity; Jul 19th, 2012 at 12:31 AM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Question plz explain the each term

    thanks @aceinfinity for explanation
    Last edited by addycoolindia1; Jul 19th, 2012 at 12:38 AM.

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain the each term

    Quote Originally Posted by addycoolindia1 View Post
    sLong = sLong.Substring(sLong.Length - 13, 13);
    How do you want people to respond to this? Are you just asking for explanations? Is this homework? Do you need further help with the above code?
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Re: plz explain this code

    thanks

  7. #7
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Re: plz explain this syntax

    i just not able to understand
    (s.Length - 13, 13);
    syntax

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain this code

    Code:
    (s.Length - 13, 13);
    There's 2 parameters here:
    1) s.Length - 13
    2) 13

    The first param indicates the start index where we want to start our SubString from. First i'll give you an example of what a SubString is...

    Word: apple

    "pple" is a SubString of "apple" starting from index 1. Index 0 is the beginning, where the 'a' char is. Index 1 is the first 'p', Index 2 is the second 'p' and so on.

    The Length of the string "apple" would be 5, because "apple" has 5 chars.

    The second param of the SubString method, indicates how many chars we want to retrieve from the start position (index).

    Example:
    "AceInfinity"

    Lets look at the bolded bit, this would be Substring(ace.Length - 8, 8).

    The starting index is the Length (11 chars), - 8 = 3. The 3rd index of "AceInfinity" is where the "I" char is. When we input the second param as 8, it means we count forward a length of 8.

    n = 1
    f = 2
    i = 3
    n = 4
    i = 5
    t = 7
    y = 8

    Counting forward 8 from the start index of 3, brings us up to the "y" in "AceInfinity" Therefore we are left with "Infinity".

    The way it's done here is not necessary though, the same result can be achieved by this (if you're just going to the end of the string):

    Code:
    SubString(3)
    That SubString of "AceInfinity" would still be "Infinity", without specifying the second param it assumes we just start from that index and go to the end of the string, so from the "I" to the end of the string in "AceInfinity".
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Re: plz explain this code

    mindblowing explanation and thanks aceinfinity.

    is you have any blog or something in which you share your concepts or teach to the begineers.
    if it is then plz send the link

  10. #10
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    Quote Originally Posted by AceInfinity View Post
    @ThomasJohnsen: If we're trying to convert i to an Integer here (int) then why is your function asking for an int param which is variable i? I would be giving a few of my opinions about this code here if that was really what was going on. Trying to cast over to a string using the ToString() method just to analyze the length of the int value, and if longer than 2, then only take the last 2 digits from the string and cast over to an int. Furthermore if that was the case then I would be using Integer.Parse() as it's more optimized for string input and faster than Convert.ToInt32().

    My explanation: If variable i cast to a string, has a length (based on number of chars within the string), which is greater than 2 then... Convert the substring of variable i's last 2 char's to a 32 bit signed integer.

    Although with this code you wouldn't even need the second param of the SubString function here, and depending on what variable i is, I may be able to explain further anomalies here too. Hopefully i isn't already of type String... Otherwise that's even worse lol.
    I chose int as paramater due to the .ToInt32 method being invoked. This made me assume that the local i was of an integer type of 32-bits or more. And the 3 lines in the OP actually does something meaningful (although in a plainly idiotic way) if i is of an integer type (ie. calculates the remainder of a division by 100 - or i = i &#37; 100 as I wrote in the 1st post). As far as I can tell having i as any other type may cause an exception upon the call to ToInt32.

    #EDIT: @AceInfinity: Please elaborate, how you expect the code in the OP to compile and run without possible exceptions for any other basic type besides int (UInt32, Int32) or long (Int64, UInt64); or any non-basic type for that matter. Double and Single will ofc work in most cases. but a 1-digit fraction will throw a first-chance exception.
    Last edited by ThomasJohnsen; Jul 19th, 2012 at 05:17 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  11. #11
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    ...it seems that this is not an assignment. He's probably trying to analyze the code by some guy Arun Kakkar (quick search on google). The variable i is indeed an int, and Arun Kakkar is indeed using the code in the OP to find the remainder with a division of 100.

    The reason for using odd code like this, apparently, is his desire to focus on strings and refrain from using complicated math. This he claims makes the code simpler and easier to convert to another language.
    (how anyone can think that
    Code:
    if (i.ToString().Length > 2) i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    is easier to comprehend than
    Code:
    i %= 100;
    is beyond me).

    ..and just to prove, that I'm not making this up: http://arunkakkar1.blogspot.dk/2011/...nto-words.html
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  12. #12
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain this code

    Quote Originally Posted by ThomasJohnsen View Post
    I chose int as paramater due to the .ToInt32 method being invoked. This made me assume that the local i was of an integer type of 32-bits or more. And the 3 lines in the OP actually does something meaningful (although in a plainly idiotic way) if i is of an integer type (ie. calculates the remainder of a division by 100 - or i = i &#37; 100 as I wrote in the 1st post). As far as I can tell having i as any other type may cause an exception upon the call to ToInt32.

    #EDIT: @AceInfinity: Please elaborate, how you expect the code in the OP to compile and run without possible exceptions for any other basic type besides int (UInt32, Int32) or long (Int64, UInt64); or any non-basic type for that matter. Double and Single will ofc work in most cases. but a 1-digit fraction will throw a first-chance exception.
    This does not make much sense to me whatsoever:
    I chose int as paramater due to the .ToInt32 method being invoked. This made me assume that the local i was of an integer type of 32-bits or more
    You know int and Int32 are the same thing? int is just short for Int32 (a 32 bit signed integer value). You can use one or the other, so if it's already int, then why do we need to, or why should we be trying to cast to a type that it's already a type of?

    As far as I can tell having i as any other type may cause an exception upon the call to ToInt32.
    Uhh nope The whole point of Convert.ToInt32 is to convert from other types to a 32 bit integer value type... You can convert a string to an int, etc...

    "A one digit fraction" - Where did you get the idea of fractions from, or any other type other than Integer here? These are integer based parameters for indexing of the SubString method. Why would we use a double or any other type for these parameters in the first place? No exceptions should happen if you just stick to the standards here.

    I think you'd better take a look here:
    - http://msdn.microsoft.com/en-us/library/hxthx5h6.aspx
    - http://msdn.microsoft.com/en-us/library/aka44szs.aspx

    Int32/int is the only param I see there, using either of the 2 overloads in those 2 links...

    Quote Originally Posted by ThomasJohnsen View Post
    ...it seems that this is not an assignment. He's probably trying to analyze the code by some guy Arun Kakkar (quick search on google). The variable i is indeed an int, and Arun Kakkar is indeed using the code in the OP to find the remainder with a division of 100.

    The reason for using odd code like this, apparently, is his desire to focus on strings and refrain from using complicated math. This he claims makes the code simpler and easier to convert to another language.
    (how anyone can think that
    Code:
    if (i.ToString().Length > 2) i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    is easier to comprehend than
    Code:
    i %= 100;
    is beyond me).

    ..and just to prove, that I'm not making this up: http://arunkakkar1.blogspot.dk/2011/...nto-words.html
    If this is true, then why are we using Convert.ToInt32 here? I'll leave that up to OP to reply, as this is an assumption until that point in time...

    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  13. #13
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Re: plz explain this code

    yes @thomas ,this code is made by arun kakkar , and for learning and practicing c# i used this code .

  14. #14
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    Re: plz explain this code

    hello everyone , let me introduce myself , i am an computer science engineering passout fresher, just got job and i am on under training , i spend mostly two weaks in company , on first day , i dont know the single syntaxes and meaning of it and even ho to use that , i studied a lot and now able understand the code,

    now , i learn all the syntaxes of c # , so please its my request to u all that plz somebody become my trainer as no body is my trainer .
    plz reply

  15. #15
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    @Ace: Fine then . Please show me a compileable example using the 3 lines of code in the OP with a variable, i, that isn't of integer type, and that won't cause a possible exception for any input.
    I.e. code along the lines:
    CSharp Code:
    1. <your suggested type here> i = <whatever>;
    2.  
    3. if (i.ToString().Length > 2)
    4. {
    5.     i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    6. }

    #EDIT: No rush - feel free to browse your own posts here and the links you provided me (to mock me obviously) for ideas .
    Last edited by ThomasJohnsen; Jul 20th, 2012 at 04:07 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  16. #16
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain this code

    Quote Originally Posted by ThomasJohnsen View Post
    @Ace: Fine then . Please show me a compileable example using the 3 lines of code in the OP with a variable, i, that isn't of integer type, and that won't cause a possible exception for any input.
    I.e. code along the lines:
    CSharp Code:
    1. <your suggested type here> i = <whatever>;
    2.  
    3. if (i.ToString().Length > 2)
    4. {
    5.     i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    6. }

    #EDIT: No rush - feel free to browse your own posts here and the links you provided me (to mock me obviously) for ideas .
    You missed my point.

    Here's what I said:
    Quote Originally Posted by Me
    Uhh nope The whole point of Convert.ToInt32 is to convert from other types to a 32 bit integer value type... You can convert a string to an int, etc...
    Here's what you said:
    Quote Originally Posted by You
    As far as I can tell having i as any other type may cause an exception upon the call to ToInt32.
    Now based on this, this has to do with the function ToInt32 from the Convert methods.

    You try this now and see if it gives you an exeception:
    Code:
    int i = Convert.ToInt32("32");
    But... if you really want me to do what you're asking here:
    Code:
    object i = 3234;
    
    if (i.ToString().Length > 2)
    {
    	i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    }
    That substring method is also unnecessary, there's no need to cast to string and then back to integer especially if you're trying to make a number to word representations of numbers.

    Code:
    int val = 1234;
    
    if (val > 99)
    {
    	//something
    }
    For as simplistic as that code in that page is, this would be much better. I don't see a reason to limit it over to dealing with only one variable to calculate a new value for the same variable either, you can use a temp variable to help in assisting calculating a new value instead of dealing with typecasting all the way through.

    @ThomasJohnsen - You can look at my reply whatever way you want, I will admit I thought you were talking about the param inside the ToInt32 function, giving an exception if the param is not a value type of int.
    Last edited by AceInfinity; Jul 20th, 2012 at 11:57 PM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  17. #17
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    Quote Originally Posted by AceInfinity View Post
    @ThomasJohnsen - You can look at my reply whatever way you want
    I'll look at it as a poor attempt to avoid admitting that you were wrong.

    You continuously made derogatory remarks about my posts and provided me with beginners links. Instructing me about datatype basics, while I clearly specified in my previous post that I was quite familiar with them (try rereading it). My first book on C was the Kernighan+Ritchie ANSI C bible 2nd ed. bought in 88 (when you were 6 and in kinder-garden most likely).
    The fact is that you didn't read neither the OP nor my posts properly.

    Keep referring to parameters within the Convert.ToInt32() to prove a point that can't be proved since the issues I was talking about was the actual assignment of i = Convert.To.... (which you would've known had you bothered reading my posts). Your comment:
    Quote Originally Posted by AceTheMVP
    Although with this code you wouldn't even need the second param of the SubString function here, and depending on what variable i is, I may be able to explain further anomalies here too. Hopefully i isn't already of type String... Otherwise that's even worse lol.
    shows that you didn't try out the code in the OP. You didn't even read it properly. Instead you felt like you had a firm grasp on it just from a quick glance and kept insulting one that actually did read and try it.

    I have seen many posts by MVPs here, and a couple have had occasion to help me from time to time. They all seem very friendly and highly intelligent, and I seriously doubt, these fora would be the same without them. They contribute massively to the codebank; examples I personally have learned alot from. In the past, I have seen you made intelligent postings and help others as well; but this thread is an example of how an MVP shouldn't act IMHO.

    Just my $0.05
    Tom


    #EDIT: Oh and your answer of object as type shows that you didn't read my post - again ("won't cause an exception for any input"). Also using an object as an int really isn't using a non-integer type now is it??? And you can keep rewriting the code from the OP and come up with better examples - we're dealing with the actually posted code, since that's what the question was regarding.


    #EDIT2: Even in your last post:
    Quote Originally Posted by AceInfinity
    You try this now and see if it gives you an exeception:
    Code:
    int i = Convert.ToInt32("32");
    you couldn't resist being sarcastic.

    Well let me respond:
    You said:
    Quote Originally Posted by AceInfinity
    "A one digit fraction" - Where did you get the idea of fractions from, or any other type other than Integer here? These are integer based parameters for indexing of the SubString method. Why would we use a double or any other type for these parameters in the first place? No exceptions should happen if you just stick to the standards here.
    I could reply:
    Code:
    float i = 32.4f;
    
    if (i.ToString().Length > 2)
    {
    	i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
    }
    or even:
    Code:
    i = Convert.ToInt32(".9")
    if the one above is too complex.

    and provide you with these links to clarify (like you said to me: "I think you better have a look here"):
    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
    http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx - especially the part about exceptions that can get thrown.
    http://msdn.microsoft.com/en-us/libr...exception.aspx


    #EDIT3: Let me finish this with quotes from Maneman from another forum where you also apparently displayed quite the arrogant tone:
    Quote Originally Posted by Maneman
    There is a certain amount of camaraderie, and a loyalty bond expected between MVP's, and an expected behaviour level towards other non MVP I.T professionals that should be 'seen' to be projected.
    ...
    Yet with all of the gifts and skills that you have, you lack the most important. - Humility.
    Last edited by ThomasJohnsen; Jul 21st, 2012 at 04:00 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  18. #18
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    579

    Re: plz explain this code

    Let me finish this with quotes from Maneman from another forum where you also apparently displayed quite the arrogant tone
    With good reason, I provided good working example, and somebody else tried to degrade my MVP status for posting it. Everybody has their off days, and that doesn't only correspond to the MVP's in the community regardless of where you're looking. I'm sure you've let the odd bad post slip once or twice too, continually keeping at it for this thread I don't see the point. I've met worse MVP's even before I became one, but I don't feel like comparing myself here. Fact of the matter is, MVP's were chosen because of their helpful activity and content within the online communities out there, and not because of their perfect interpersonal skills. Sadly i'll admit that I am an example of this...

    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  19. #19
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: plz explain this code

    Quote Originally Posted by AceInfinity View Post
    I'm sure you've let the odd bad post slip once or twice too
    Oh plenty of times.

    ------

    Time to let this thread die, I reckon.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •