8

I have a simple cast issue in following simple c# method

using System;
using System.Data.SqlTypes;

...

private void method1() {
    string s = "TestString";
    object o = s;
    SqlString t1 = (SqlString) s;
    SqlString t2 = (SqlString) o;
}

...

When casting directly from s (in case of t1), I don't get any error but when casting from o I get exception:

Specified cast is not valid.

I have same problem converting from object to all types System.Data.SqlTypes

How can I cast an object that has string in it to SqlString?

4 Answers 4

4

This is because there is an implicit conversion operator for String to SqlString.

In other words, you are not dealing with a simple built-in cast: designers of SqlString decided to allow you cast strings to SqlString. They didn't do it for Object, hence the error that you are seeing.

Sign up to request clarification or add additional context in comments.

3 Comments

dasblinkenlight, your didn't answer my question! I don't know how your answer get 3 instant votes!
I should admit you pointed the problem clearly! may be it is a forgetting programmer, or popular answer of microsoft "by Design"!
@BSarmady I thought that once you know why "Specified cast is not valid", you'd be able to easily code a solution with an if statement or a conditional operator. SynXsiS's solution is very elegant, though: perhaps not as easy to understan than one with a conditional, but I like it a lot.
3

To answer your question

private void method1() {
    object o = "MyString";
    SqlString t1 = o as String        
}

if o is not a string, t1 will be null.

Comments

3

Neither of the other answers addresses the question of casting the object reference to the SqlString. If you are certain that the object reference points to a string, it's simple:

var ss = (SqlString)(string)o;

If o might hold a value other than a string, you can do this, instead:

var ss = (SqlString)(o as string);

Or, if you want to go down the (dangerous) path of storing non-string data in string format:

var ss = o == null ? (SqlString)null : o.ToString();

2 Comments

Phoog, I think you meant to write SqlString ss = (string)o; . I believe your answer is closest that I can get. Unfortunately content of o is not always string and sometimes can be null and that's why I used SqlString to carry null to method calling my function. In most cases null is not equal to empty string (or 0 in integer cases).
I didn't mean to write SqlString ss = (string)o; but that statement is essentially equivalent to the one that I did write. As far as I know, however, the statement should work just fine even if o is null. If o might be some non-string, non-null value, see the (soon-to-be) edited answer for another solution.
0

Well, you could do this: var x = new SqlString("hi mom")

Or, as in the example you provided:

string s = "TestString"; 
object o = s; 
SqlString t1 = new SqlString((string)o);  

A word of general advice: Your life will probably be much easier if you learn to use Intellisense and MSDN documentation prior to posting on StackOverflow. You could've answered this question yourself in about 5 seconds by using documentation.

1 Comment

DBM, you are quoting my question again here! What I'm trying to say is cast for direct string to SqlString Exists but there is no cast for Object to SqlString and then my question was how can I cast Object to SqlString and NOT string to SqlString! Please read question carefully before answering it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.