Hi,
With any kind of covert error ie "Cannot convert string to byte"
What is the best practice way of implementing a cast conversion?
For arguments sake:-
byte enc = rsa.Encrypt();
Ive tried the below which obviously doesnt work:-
byte enc = (byte)rsa.Encrypt();
Thanks,
S
Loading

VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 6:58 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 6:23 AM EDT
byte[] enc = rsa.Encrypt();
Posted Apr 22, 2011, 6:43 AM EDT
Apologies but my explanation of the question probably wasnt the best example I could of given to get my point across...
I was enquiring re the cast conversion in a more generic way of implementation...
Am I right in saying that usual practice of implementing a cast would require putting (type) followed by the variable of the "to be" casted type, followed by the dot notation for function member access ie:-
int i = new int();
string str = (int)i.somemethod();
Thanks
S
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 6:57 AM EDT
int i = new int();
string str = (string)i.somemethod();
and not.
int i = new int();
string str = (int)i.somemethod();
Posted Apr 22, 2011, 6:59 AM EDT
Think ive figured it out for example:-
int i;
i = new int();
string str;
str = (int)i.ToString(); // doesnt compile as errors with same "Cannot convert type string to int"
However...
str = (string)i.ToString(); // compiles fine as the method ToString is of type string obviously.
Thanks
Posted Apr 22, 2011, 7:01 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 7:04 AM EDT
1. You never need to call int's constructor which always returns 0. Just do:
int i = 0;
2. There's no point casting or converting something which is already a string, or returns a string, to a string. Just do:
string s = i.ToString();
Posted Apr 22, 2011, 7:11 AM EDT
I am aware the examples I gave were not "true to life" or "true to coding practice" they were simply intended as demos.
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 7:11 AM EDT
What is the difference between
string st = (string)...
and
string st = convert.tostring(...)
Thank you,
VulpesRank #55 · 29.2K reputationPosted Apr 22, 2011, 7:31 AM EDT
In the case of numbers and other simple types, there is no implicit or explicit conversion to string. So you need to use Convert.ToString - or just ToString(). A simple cast will not work:
string st = Convert.ToString(5); // works fine
string st = 5.ToString(); // also works fine
string st = (string)5; // doesn't compile
Otherwise, the overload of Convert.ToString() which takes an 'object' parameter is used. This just returns the result of applying the ToString() method to the object or, if the parameter is null, returns an empty string.
The only cases where a cast will work for objects in general is if there is a used-defined explicit (or implicit) conversion to string for the object's type.
As the string type is sealed and inherits directly from object, there is no possibility of any sub-types or super-types (except object) existing.
Jonathas Sucupira122 reputationPosted Apr 22, 2011, 7:42 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 25, 2011, 6:21 AM EDT
Hi S,
Why don't you update your profile name with a meaningful name.