if (i.ToString().Length > 2)
{
i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
}
if (i.ToString().Length > 2)
{
i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
}
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:
private void myfunction(int i) { Console.WriteLine(String.Format("Value of i before: {0}", i)); if (i.ToString().Length > 2) { i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2)); } Console.WriteLine(String.Format("Value of i after: {0}", i)); }
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:
(you will ofc have to figure out the operator and value by yourselfCSharp Code:
i = i {some C# operator} {some value};).
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)
@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
thanks @aceinfinity for explanation
Last edited by addycoolindia1; Jul 19th, 2012 at 12:38 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
thanks
i just not able to understand
(s.Length - 13, 13);
syntax
There's 2 parameters here:Code:(s.Length - 13, 13);
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):
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".Code:SubString(3)
<<<------------< 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
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![]()
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 % 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)
...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
is easier to comprehend thanCode:if (i.ToString().Length > 2) i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
is beyond me).Code:i %= 100;
..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)
This does not make much sense to me whatsoever:
You know int and Int32 are the same thing?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 moreint 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?
Uhh nopeAs far as I can tell having i as any other type may cause an exception upon the call to ToInt32.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...
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
yes @thomas ,this code is made by arun kakkar , and for learning and practicing c# i used 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
@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:
<your suggested type here> i = <whatever>; if (i.ToString().Length > 2) { i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2)); }
#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)
You missed my point.
Here's what I said:
Here's what you said:Originally Posted by Me
Now based on this, this has to do with the function ToInt32 from the Convert methods.Originally Posted by You
You try this now and see if it gives you an exeception:
But... if you really want me to do what you're asking here:Code:int i = Convert.ToInt32("32");
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:object i = 3234; if (i.ToString().Length > 2) { i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2)); }
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.Code:int val = 1234; if (val > 99) { //something }
@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
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:
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.Originally Posted by AceTheMVP
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:
you couldn't resist being sarcastic.Originally Posted by AceInfinity
Well let me respond:
You said:
I could reply:Originally Posted by AceInfinity
or even:Code:float i = 32.4f; if (i.ToString().Length > 2) { i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2)); }
if the one above is too complex.Code:i = Convert.ToInt32(".9")
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:
Originally Posted by Maneman
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)
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...Let me finish this with quotes from Maneman from another forum where you also apparently displayed quite the arrogant tone
~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
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)