• Password Protect - An Open Source Application
    44 replies, posted
[QUOTE=Senney;17143392][code] if (txt_password.Text == "My1337Password") { MessageBox.Show("Password Correct."); } else { MessageBox.Show("Password Wrong."); } [/code] Why make a new method just to do a simple equal or not equal? I gave you a box to put your function in.[/QUOTE] I don't know how stupid C# is, but I doubt it's that stupid (Is it?). In C++ if you did that, you would be comparing only the first characters of each string. What the function should do is loop through the strings character by character and match them together.
If txt_password.Text would be a char* of some sort, then in C++ you'd be comparing two addresses. If it would be a (more or less) good string-class (like std::string or anything with a properly overloaded operator==), then it would compare each character. I'm pretty sure, since C# is a high-level language, that you can compare strings like that without any problems.
This kind of sting comparison is fine in C#
[QUOTE=Spoco;17158686]I don't know how stupid C# is, but I doubt it's that stupid (Is it?). In C++ if you did that, you would be comparing only the first characters of each string. What the function should do is loop through the strings character by character and match them together.[/QUOTE] What kind of shit are you on about. It's a simple string comparison.
I think it's comparing the references. It's like that in Java and C# and Java are very similar when it comes to these things. [b]Edit:[/b] I'm wrong. Disregard this post.
[QUOTE=Robber;17160424]I think it's comparing the references. It's like that in Java and C# and Java are very similar when it comes to these things.[/QUOTE] You can treat it as a string comparison: [cpp] using System; class Program { static void Main(string[] args) { string a = "abcd"; a += "ef"; if (a == "abcdef") { Console.WriteLine("It works"); } } } [/cpp]
[QUOTE=Robber;17160424]I think it's comparing the references. It's like that in Java and C# and Java are very similar when it comes to these things.[/QUOTE] No, String's operator == is overloaded in C#. (Java doesn't overload it by design).
[QUOTE=gparent;17160858]No, String's operator == is overloaded in C#. (Java doesn't overload it by design).[/QUOTE] You learn something new every day.
Ah right, I completely assumed (stupid me) that txt_password.Text would just be a reference to the character data and not a string object of any kind. But [code] ("comparison" == "comparison") [/code] So that would compare the whole strings? In other words, do literal strings automatically get handled like ( or get turned into ) objects in C#?
[QUOTE=gparent;17160858]No, String's operator == is overloaded in C#. (Java doesn't overload it by design).[/QUOTE] This. However, the == method of comparison will usually work, because of how java.lang.String does String interning (keeping a cache of already built, immutable String objects that have previously been created). This however should not be relied upon, and use of String's equals method is recommended.
[QUOTE=Spoco;17161752]Ah right, I completely assumed (stupid me) that txt_password.Text would just be a reference to the character data and not a string object of any kind. But [code] ("comparison" == "comparison") [/code] So that would compare the whole strings? In other words, do literal strings automatically get handled like ( or get turned into ) objects in C#?[/QUOTE] Not quite, I believe it uses a string pool, meaning the same strings always point to the same string data. You don't ever change a string directly, a new string is created for concatenation operations etc, if it matches an existing string, the pointers are the same. At least to my understanding that's how it works, correct me if I'm wrong.
[QUOTE=bean_xp;17164347]I believe it uses a string pool, meaning the same literal strings always become the same string object.[/QUOTE] Fixed. Correct me if I'm wrong again. :/
[QUOTE=tjl;17162628]This. However, the == method of comparison will usually work, because of how java.lang.String does String interning (keeping a cache of already built, immutable String objects that have previously been created). This however should not be relied upon, and use of String's equals method is recommended.[/QUOTE] My cs teacher has said this many times.
[QUOTE=Spoco;17161752]Ah right, I completely assumed (stupid me) that txt_password.Text would just be a reference to the character data and not a string object of any kind. But [code] ("comparison" == "comparison") [/code] So that would compare the whole strings? In other words, do literal strings automatically get handled like ( or get turned into ) objects in C#?[/QUOTE] C# only has pointers inside blocks of "unsafe" code, which should only really be used when marshalling code. String literals are always of the string class.
[url=http://www.albahari.com/valuevsreftypes.aspx]Value types vs. Reference types[/url]
Sorry, you need to Log In to post a reply to this thread.