Mosho Mulan
Mosho Mulan

Reputation: 101

How to change a variable type in C#?

I wanted to use something like this:

if(x==5)
{
    var mydb= ........ ;
}
else 
{
    var mydb = ........ ;
}

but it didn't work because I can't declare a variable inside if statement.

So I tried to do this:

var mydb;

if (x==5)
{
    mydb= ............. ;
}
else 
{
    mydb=.............;
}

but id didn't work either because I had to initialize the variable (mydb).

So the question is: I don't necessarily know the type of the variable, can I declare it anyway and then change the type inside the if statement?

Upvotes: 10

Views: 34984

Answers (7)

AirPlanets
AirPlanets

Reputation: 1

You can use like this bool result = (condition) ? true : false ;

var myDB = (x==5) ? true : false ;
or
var myDB = x==5 ? "doing something when case is true" : "doing something when case is false" ;

Upvotes: 0

user1080247
user1080247

Reputation: 1166

YOU CAN USE SYSTEM.CONVERT CLASS LIKE THIS OR USE DYNAMIC

string possibleInt = "1234"; 
int count = Convert.ToInt32(possibleInt);

OR USE Explicit Conversions

In C#, you can use a cast operator to perform explicit conversions. A cast specifies the type to convert to, in round brackets. The syntax for performing an explicit conversion is shown in the following code example.

DataType variableName1 = (castDataType) variableName2;

Upvotes: 0

Michael Buen
Michael Buen

Reputation: 39393

you can use:

object mydb = null;

if(x==5)
{
    mydb= ........ ;
}
else 
{
    mydb = ........ ;
}

but you have to unbox or cast the object back to its proper type when you want to access the object's fields,properties,methods. unless you will wait for C# 4 which can facilitate dynamic method (exact terminology: dynamic dispatch?) invocation

Upvotes: 7

helium
helium

Reputation: 1078

I assume that you have two incompatible types from two different libraries that both represent a database. You could write an interface with the common operations and write adapters that wrap the classes coming from the libraries and implement you interface. Than the type of your variable mydb could be that interface.

Of course you could use objectas type for mydb and use dynamic type tests and casts, but that would be a very bad design decision in this case.

Upvotes: 2

Hun1Ahpu
Hun1Ahpu

Reputation: 3355

You can declare base type and inherit both types from it. But the main question is: How you gonna use it if you don't know its type?

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798516

C# is statically typed unless you're running 4.0 with the dynamic specifier, so changing the type is classically impossible except via polymorphism.

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1499950

No, you can't. Variables never change their types. What types are you actually interested in? Could you declare the variable to be some common base type or an interface that both of them implement? If you can tell us more about your situation, we may be able to help you more.

C# is a statically typed language (leaving aside C# 4; that introduces dynamic typing where you really need it, but it's worth understanding the "normal" C# way of doing things first). The compiler needs to know the type of the variable so that if can work out what each reference to it means. For example, if you use

string x = "text";
int y = x.Length;

the compiler needs to know that x is of type string so that it can check that the type has a Length property and emit a call to it in the IL.

Upvotes: 12

Related Questions