Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert from 'System.Guid?' to 'System.Guid'

Tags:

Does anyone know how to deal with this error?

cannot convert from 'System.Guid?' to 'System.Guid'
like image 870
Dooie Avatar asked Jan 01 '10 11:01

Dooie


3 Answers

See MSDN.

In that case, merely use myNullableVariable.Value (if you're sure it has a value), or (myNullableVariable.HasValue)?myNullableVariable.Value:somedefaulthere if you're not.

One can also use GetValueOrDefault() if one doesn't care if the default is a specific value when the nullable really is null.

The last way to do it is this: myNullableVariable.Value ?? defaultvalue.

See, technically a MyType? variable is a Nullable<MyType> under the covers. There are no implicit or explicit casts between the two, so what you need to do is extract the value out of the Nullable manually. The third way i listed is the most succinct (and best?) way to do it, to that would probably be best in most cases.

like image 116
RCIX Avatar answered Oct 01 '22 16:10

RCIX


Use Guid?.Value property to convert 'System.Guid?' to 'System.Guid'. as following example

 Obj GetValue(Guid yourID)
{
return FetchObject(yourID)
}
Void main()
{
Guid? passvalue;
Obj test = GetValue(passvalue.Value);
}
like image 24
Niraj Trivedi Avatar answered Oct 01 '22 15:10

Niraj Trivedi


First intialize the guid variable.

Guid yourGuid= Guid.NewGuid()

then set value in that which you want for eg:

 Guid defaultId = Guid.NewGuid();
 if (customerRow.GuardianState.Length > 2) {
 Guid StateId = record.StateId ?? defaultId;}
like image 36
Sandy Avatar answered Oct 01 '22 16:10

Sandy