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
VulpesPosted Apr 22, 2011, 10:58 AM
Suthish NairPosted Apr 25, 2011, 10:21 AM
Hi S,
Why don't you update your profile name with a meaningful name.
Jonathas SucupiraPosted Apr 22, 2011, 11:42 AM
VulpesPosted Apr 22, 2011, 11:31 AM
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 SucupiraPosted Apr 22, 2011, 11:11 AM
What is the difference between
string st = (string)...
and
string st = convert.tostring(...)
Thank you,
Posted Apr 22, 2011, 11:11 AM
I am aware the examples I gave were not "true to life" or "true to coding practice" they were simply intended as demos.
VulpesPosted Apr 22, 2011, 11:04 AM
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, 11:01 AM
Posted Apr 22, 2011, 10:59 AM
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
Jonathas SucupiraPosted Apr 22, 2011, 10:57 AM
int i = new int();
string str = (string)i.somemethod();
and not.
int i = new int();
string str = (int)i.somemethod();
Posted Apr 22, 2011, 10:43 AM
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
VulpesPosted Apr 22, 2011, 10:23 AM
byte[] enc = rsa.Encrypt();