铸造字符串转换成的Guid在VB.Net [英] Cast a string into Guid in VB.Net
问题描述
我的GUID用户名,它需要填充会议(用户名),这是一个字符串,但格式的完美转换为GUID。
I have the Guid "UserID" , it needs to be filled with Session("UserID") which is a String , but formatted to convert perfectly to a Guid.
如果我试试这个,无法将类型为字符串类型GUID
If I try this , "Cannot Convert type string into type Guid"
Dim UserID As New Guid()
If HttpContext.Current.Session("UserID") IsNot Nothing Then
UserID = HttpContext.Current.Session("UserID")
End If
如果我尝试这个无法施展字符串类型为类型GUID
If I try this "Cannot Cast type string into type Guid"
Dim UserID As New Guid()
If HttpContext.Current.Session("UserID") IsNot Nothing Then
UserID = DirectCast(HttpContext.Current.Session("UserID"), Guid)
End If
这将字符串转换成的Guid完美,但它是不可访问的。如果声明之外
This converts the string into Guid perfectly , but it is not accessable outside of the If Statement
If HttpContext.Current.Session("UserID") IsNot Nothing Then
Dim UserID As new Guid(HttpContext.Current.Session("UserID"))
End If
我无法弄清楚如何定义if语句的用户ID外,然后将其指定有条件
I can not figure out how to define UserID outside of the If statement , then assign it conditionally
推荐答案
这个尝试
Dim UserID As New Guid()
If HttpContext.Current.Session("UserID") IsNot Nothing Then
UserID = Guid.Parse(HttpContext.Current.Session("UserID").ToString())
End If
万一你困惑,考虑到,除非我的记忆中失败,构造器的Guid将产生一个空的GUID。如果你想生成一个新的GUID使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.guid.newguid\"><$c$c>Guid.NewGuid()$c$c>
这篇关于铸造字符串转换成的Guid在VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!