Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


c# code improvements.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Wed Sep 30, 2009 12:55 pm    Post subject: c# code improvements. Reply with quote

Is there a better way of doing this.
Code:
        private string FormatFix(int input)
        {
            if (input < 10)
            {
                return "00" + input;
            }
            else if (input < 100)
            {
                return "0" + input;
            }
                return input.ToString();
        }

Code:

        private bool isNumeric(string input)
        {
            foreach (char c in input)
            {
                if (!char.IsDigit(c))
                {
                    return false;
                }
            }
            return true;
        }

_________________
Intel over amd yes.


Last edited by NINTENDO on Wed Sep 30, 2009 1:25 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Wed Sep 30, 2009 1:22 pm    Post subject: Reply with quote

I think this would be a better way to do it:
Code:

private bool IsNumeric (string input)
{
    try
    {
         int.Parse(input);
    }
    catch(FormatExcpetion ex)
    {return false;}
return true;
}

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Wed Sep 30, 2009 1:29 pm    Post subject: Reply with quote

Odecey wrote:
I think this would be a better way to do it:
Code:

private bool IsNumeric (string input)
{
    try
    {
         int.Parse(input);
    }
    catch(FormatExcpetion ex)
    {return false;}
return true;
}

so like
Code:
int lol;
return int.TryParse(input, out lol);

_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Thu Oct 01, 2009 1:24 am    Post subject: Re: c# code improvements. Reply with quote

JesusLovesYou wrote:
Code:
private string FormatFix(int input)
I bet C# has some way to adjust the width and the fill, but since I'm no C# coder, I don't know how to do this in C# :p
Back to top
View user's profile Send private message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Thu Oct 01, 2009 4:22 am    Post subject: Reply with quote

I guess you can make the FormatFix more general, like this:
Code:

private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
     for(int i = 0;dif>i;i++)
          output += "0";
return output += input.ToString();
}

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
NINTENDO
Grandmaster Cheater Supreme
Reputation: 0

Joined: 02 Nov 2007
Posts: 1371

PostPosted: Thu Oct 01, 2009 7:42 am    Post subject: Reply with quote

Odecey wrote:
I guess you can make the FormatFix more general, like this:
Code:

private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
     for(int i = 0;dif>i;i++)
          output += "0";
return output += input.ToString();
}

that way I have to know the number of digitis? Keep in mind that the value of the integer is not hardcoded.

_________________
Intel over amd yes.
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Thu Oct 01, 2009 10:26 am    Post subject: Reply with quote

JesusLovesYou wrote:
Odecey wrote:
I guess you can make the FormatFix more general, like this:
Code:

private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif = input.ToString().Length -numOfDigits;
if(dif>0)
     for(int i = 0;dif>i;i++)
          output += "0";
return output += input.ToString();
}

that way I have to know the number of digitis? Keep in mind that the value of the integer is not hardcoded.

Perhaps you were confused by a mistake I made, it should actually be:
Code:

private string FormatFix(int input,int numOfDigits)
{
string output = "";
int dif =numOfDigits- input.ToString().Length;// Originally I had set numOfDigits as subtrahend.
if(dif>0)
     for(int i = 0;dif>i;i++)
          output += "0";
return output += input.ToString();
}

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Thu Oct 01, 2009 1:49 pm    Post subject: Reply with quote

This is not what I really meant.. More like this:
Code:
stringstream ss;
ss.width( 8 );
ss.fill( '0' );
ss.print( number );
return ss.str();
Back to top
View user's profile Send private message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Thu Oct 01, 2009 4:16 pm    Post subject: Reply with quote

Jani wrote:
This is not what I really meant.. More like this:
Code:
stringstream ss;
ss.width( 8 );
ss.fill( '0' );
ss.print( number );
return ss.str();

What are you on about? This:
Code:
FormatFix(number, 8);

Should give the same output as your pseudocode, and is just as easy.

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
SCRN
Grandmaster Cheater Supreme
Reputation: 1

Joined: 02 Sep 2007
Posts: 1319
Location: BOX

PostPosted: Thu Oct 01, 2009 8:06 pm    Post subject: Reply with quote

There's a built in function that already does what you're trying to do.

In VB.NET you can use Format or PadLeft:
Format(NameOfIntegerVariable, "000")
NameOfIntegerVariable.ToString.PadLeft(3, "0")

In C#:
Strings.Format(NameOfIntegerVariable, "000");
NameOfIntegerVariable.ToString.PadLeft(3, "0");

Note I used a converter for the C# code. I usually use VB.NET and I'm not sure how accurate the converter is. VB.NET and C#.NET are really similar though so the code should work fine.

_________________
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Fri Oct 02, 2009 1:23 am    Post subject: Reply with quote

Odecey wrote:
Should give the same output as your pseudocode, and is just as easy.
I meant my code for the body of the FormatFix, not as a replacement. I find padding zeroes in a loop like that very ugly.

Kumori Guardian: that's more like it.. It might be the thing I'm talking about. Let's say .Format() it's...

I'm sure that C# can fill the string to given length when converting an integer to a string. Knowing nothing about C#, it's very fun to give any advice :P Well, I do know how things should be done..

EDIT: I finally did a bit Googling, well more MSDN browsing, anyway here's how it's done:
Code:
private string FormatFix(int input)
{
  return String.Format("{0:D3}", input);
}
It fills any string to length of 3 padding zeroes in front of the string if needed. Change the 3 to eg. 8 if you want 8 digits... Ha, I do know C#! :p Also remember to catch exceptions. If you want to add an another param to indicate the length, you know how to work with strings, don't you?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites