简体   繁体   中英

Converting int32 to int64 C#

I have problems with converting these two mentioned types. could somebody give me the solution please?

This is the code:

int INT32ID = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());   
int StudentID = **Convert.ToInt64(INT32ID);**(here is the error)
var Student = DB.Students.Find(StudentID);
DB.Entry(Student).State = EntityState.Deleted;

And here is what the error says:

Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)

int StudentID = Convert.ToInt64(INT32ID);

You are converting an int32 to an int64, and then assigning it to a int32 . Since a int64 do not fit into a int32, you get an error. You should be able to just write:

long StudentID = INT32ID ;

or

var StudentID  = long.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());  

int32 is implicitly convertable to int64 since every 32 bit int can exactly be expressed as a 64 bit int, so there is no risk for data loss.

Replace

int StudentID = ...

with

long StudentID = ...

You can use the code below. It will save you from getting errors.

int64 StudentID = (int64)(INT32ID);

Your conversion is in wrong manner, Try it with below code

var _studenID = long.Parse(_grdStudentDetails.CurrentRow.Cells[2].Value.ToString()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM