Is there any way to create a function that can change the data type of a variable?












0















I 'm new to C#. Is there any way to create a function that can change the datatype of a variable to an another datatype and return the changed value.



For example :



Let's assume that ChangeDataType is the name of the function. It should also be possible to do the same thing with all datatypes.





int a = 5;

float b = ChangeDataType(a, "float"); // 5.0




Thanks.










share|improve this question


















  • 2





    Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

    – Fabjan
    Jan 1 at 10:16













  • Fabjan, I want create a function like that without using the built-in.

    – Roshan Kumar
    Jan 1 at 10:19






  • 4





    Well, look at the source code of the built-in, and do the same...

    – GSerg
    Jan 1 at 10:24











  • Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

    – Manoj Choudhari
    Jan 1 at 10:25






  • 2





    Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

    – PMF
    Jan 1 at 10:56
















0















I 'm new to C#. Is there any way to create a function that can change the datatype of a variable to an another datatype and return the changed value.



For example :



Let's assume that ChangeDataType is the name of the function. It should also be possible to do the same thing with all datatypes.





int a = 5;

float b = ChangeDataType(a, "float"); // 5.0




Thanks.










share|improve this question


















  • 2





    Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

    – Fabjan
    Jan 1 at 10:16













  • Fabjan, I want create a function like that without using the built-in.

    – Roshan Kumar
    Jan 1 at 10:19






  • 4





    Well, look at the source code of the built-in, and do the same...

    – GSerg
    Jan 1 at 10:24











  • Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

    – Manoj Choudhari
    Jan 1 at 10:25






  • 2





    Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

    – PMF
    Jan 1 at 10:56














0












0








0


1






I 'm new to C#. Is there any way to create a function that can change the datatype of a variable to an another datatype and return the changed value.



For example :



Let's assume that ChangeDataType is the name of the function. It should also be possible to do the same thing with all datatypes.





int a = 5;

float b = ChangeDataType(a, "float"); // 5.0




Thanks.










share|improve this question














I 'm new to C#. Is there any way to create a function that can change the datatype of a variable to an another datatype and return the changed value.



For example :



Let's assume that ChangeDataType is the name of the function. It should also be possible to do the same thing with all datatypes.





int a = 5;

float b = ChangeDataType(a, "float"); // 5.0




Thanks.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 10:09









Roshan KumarRoshan Kumar

218




218








  • 2





    Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

    – Fabjan
    Jan 1 at 10:16













  • Fabjan, I want create a function like that without using the built-in.

    – Roshan Kumar
    Jan 1 at 10:19






  • 4





    Well, look at the source code of the built-in, and do the same...

    – GSerg
    Jan 1 at 10:24











  • Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

    – Manoj Choudhari
    Jan 1 at 10:25






  • 2





    Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

    – PMF
    Jan 1 at 10:56














  • 2





    Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

    – Fabjan
    Jan 1 at 10:16













  • Fabjan, I want create a function like that without using the built-in.

    – Roshan Kumar
    Jan 1 at 10:19






  • 4





    Well, look at the source code of the built-in, and do the same...

    – GSerg
    Jan 1 at 10:24











  • Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

    – Manoj Choudhari
    Jan 1 at 10:25






  • 2





    Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

    – PMF
    Jan 1 at 10:56








2




2





Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

– Fabjan
Jan 1 at 10:16







Well, you could use function from class Convert, for example Convert.ChangeType() or Convert.ToSingle()

– Fabjan
Jan 1 at 10:16















Fabjan, I want create a function like that without using the built-in.

– Roshan Kumar
Jan 1 at 10:19





Fabjan, I want create a function like that without using the built-in.

– Roshan Kumar
Jan 1 at 10:19




4




4





Well, look at the source code of the built-in, and do the same...

– GSerg
Jan 1 at 10:24





Well, look at the source code of the built-in, and do the same...

– GSerg
Jan 1 at 10:24













Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

– Manoj Choudhari
Jan 1 at 10:25





Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: this link

– Manoj Choudhari
Jan 1 at 10:25




2




2





Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

– PMF
Jan 1 at 10:56





Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there.

– PMF
Jan 1 at 10:56












1 Answer
1






active

oldest

votes


















2














Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.



Below are different kinds of typecasting options:



1. Implicit Type casting
In this you don't need to use any syntax.
For ex:



int i = 10;
float a = i;


2. Explicit Type casting
You need to specify the type in which you want to convert to. For ex:



class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}


// Output: 1234



3. User Defined Casts
Using implict and explicit keywords in c#.
Please refer



4. Helper Classes
There are many classes which provides method to convert data types.
One of the important class is - Convert.
This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc.
Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer






share|improve this answer





















  • 1





    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

    – GSerg
    Jan 1 at 13:05








  • 2





    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

    – Scott Hannen
    Jan 1 at 16:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994601%2fis-there-any-way-to-create-a-function-that-can-change-the-data-type-of-a-variabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.



Below are different kinds of typecasting options:



1. Implicit Type casting
In this you don't need to use any syntax.
For ex:



int i = 10;
float a = i;


2. Explicit Type casting
You need to specify the type in which you want to convert to. For ex:



class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}


// Output: 1234



3. User Defined Casts
Using implict and explicit keywords in c#.
Please refer



4. Helper Classes
There are many classes which provides method to convert data types.
One of the important class is - Convert.
This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc.
Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer






share|improve this answer





















  • 1





    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

    – GSerg
    Jan 1 at 13:05








  • 2





    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

    – Scott Hannen
    Jan 1 at 16:44
















2














Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.



Below are different kinds of typecasting options:



1. Implicit Type casting
In this you don't need to use any syntax.
For ex:



int i = 10;
float a = i;


2. Explicit Type casting
You need to specify the type in which you want to convert to. For ex:



class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}


// Output: 1234



3. User Defined Casts
Using implict and explicit keywords in c#.
Please refer



4. Helper Classes
There are many classes which provides method to convert data types.
One of the important class is - Convert.
This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc.
Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer






share|improve this answer





















  • 1





    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

    – GSerg
    Jan 1 at 13:05








  • 2





    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

    – Scott Hannen
    Jan 1 at 16:44














2












2








2







Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.



Below are different kinds of typecasting options:



1. Implicit Type casting
In this you don't need to use any syntax.
For ex:



int i = 10;
float a = i;


2. Explicit Type casting
You need to specify the type in which you want to convert to. For ex:



class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}


// Output: 1234



3. User Defined Casts
Using implict and explicit keywords in c#.
Please refer



4. Helper Classes
There are many classes which provides method to convert data types.
One of the important class is - Convert.
This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc.
Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer






share|improve this answer















Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.



Below are different kinds of typecasting options:



1. Implicit Type casting
In this you don't need to use any syntax.
For ex:



int i = 10;
float a = i;


2. Explicit Type casting
You need to specify the type in which you want to convert to. For ex:



class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}


// Output: 1234



3. User Defined Casts
Using implict and explicit keywords in c#.
Please refer



4. Helper Classes
There are many classes which provides method to convert data types.
One of the important class is - Convert.
This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc.
Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 10:43









J Sushil

645720




645720










answered Jan 1 at 10:22









Manoj ChoudhariManoj Choudhari

1,9881616




1,9881616








  • 1





    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

    – GSerg
    Jan 1 at 13:05








  • 2





    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

    – Scott Hannen
    Jan 1 at 16:44














  • 1





    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

    – GSerg
    Jan 1 at 13:05








  • 2





    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

    – Scott Hannen
    Jan 1 at 16:44








1




1





This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

– GSerg
Jan 1 at 13:05







This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the Convert class that the OP rejected before this answer was posted.

– GSerg
Jan 1 at 13:05






2




2





It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

– Scott Hannen
Jan 1 at 16:44





It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works.

– Scott Hannen
Jan 1 at 16:44




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994601%2fis-there-any-way-to-create-a-function-that-can-change-the-data-type-of-a-variabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules