创建 GUID 常量?(VB.net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33539932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:32:45  来源:igfitidea点击:

Create GUID constants? (VB.net)

.netvb.netcontrolsguidcom-interop

提问by Dyrdek

Public Const ClassId = "92A1377A-7A3C-4FAF-94DA-C229AFFCFB12" Public Const InterfaceId = "2DE393C7-300A-46DB-B33C-583B2765C2F9" Public Const EventsId = "5452FC4D-C0C2-4E2B-87CA-8F43EAA14998"

Public Const ClassId = "92A1377A-7A3C-4FAF-94DA-C229AFFCFB12" Public Const InterfaceId = "2DE393C7-300A-46DB-B33C-583B2765C2F9" Public Const EventsId = "5452FC4D-C0C2-4E2B-87CA-8F43EAA14998"

I found this in a code snippet and need to create my own GUID in vb.net. But I didn't find a way that the program automatically creates constantes like this and shows them in the code. How can I do that? I'm not yet familiar with GUIDs but I suppose there were created automatically.

我在一个代码片段中发现了这个,需要在 vb.net 中创建我自己的 GUID。但是我没有找到程序自动创建这样的常量并在代码中显示它们的方法。我怎样才能做到这一点?我还不熟悉 GUID,但我想它们是自动创建的。

Thanks for any hints :)

感谢您的任何提示:)

采纳答案by Rahul Tripathi

First point is that you cannot apply constmodifier to GUID's as constis applied on primitive datatype. If you want you can use Shared ReadOnly.

第一点是您不能constconst应用于原始数据类型那样将修饰符应用于 GUID 。如果你愿意,你可以使用Shared ReadOnly.

Second point is you can refer Guid.NewGuid Method ()which is used to create GUID. Something like

第二点是您可以参考用于创建 GUID 的Guid.NewGuid Method()。就像是

Dim str = Guid.NewGuid.ToString()

EDIT:

编辑:

A good MSDN referencewhich you have found yourself can be helpful.

您自己找到的一个很好的MSDN 参考可能会有所帮助。

回答by Matt Wilko

Firstly you can use GuidGento create a new unique GUID for you:

首先,您可以使用GuidGen为您创建一个新的唯一 GUID:

Using menu TOOLS -> External Tools... add:
%Installation Path%\Microsoft Visual Studio 14.0\Common7\Tools\guidgen.exe

e.g.

例如

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\guidgen.exe

Give it a title of GUID Generator

给它一个标题 GUID Generator

Then you can run this to create a Guid String for yourself. Select option 4 (Registry Format)

然后你可以运行它来为自己创建一个 Guid String。选择选项 4(注册表格式)

Once you have created a guid, you can assign it to a constant string in your code:

创建 guid 后,您可以将其分配给代码中的常量字符串:

Const MyGuidString = "{1606C5C9-9ECA-4227-9F7E-7FA032D153BE}"

This is however just a string. If you want to use it as an actual Guid, you can define a variable like this:

然而,这只是一个字符串。如果您想将其用作实际的 Guid,您可以像这样定义一个变量:

Dim guidObject = New Guid(MyGuidString)