New to VB - A question with static data...
-
29. dubna 2012 14:46
I'm new to VB, I've learnt most of the other languages offered by Visual Studio (there is still some way to go with a few of them however), my question is about what would be called static fields in the terms of more C like languages.
In C# I can do the following
namespace MyNamespace { public class MyDllClass { public static class Version { public static byte Major{get{return 0;}} public static byte Minor{get{return 0;}} public static ushort Revision{get{return 0;}} } } }I often use this idea to supply information with my DLLs, how is the best way to achieve the same result in Visual Basic.
Thanks in advance, RAK....
Všechny reakce
-
29. dubna 2012 15:08
Namespace MyNamespace Public Class MyDllClass Public Class Version Public Shared ReadOnly Property Major As Byte Get Return 0 End Get End Property Public Shared ReadOnly Property Minor As Byte Get Return 0 End Get End Property Public Shared ReadOnly Property Revision As UShort Get Return 0 End Get End Property End Class End Class End Namespace
I think this will do the trick...- Upravený RunAwayKlepto 29. dubna 2012 15:08
- Navržen jako odpověď Heslacher 30. dubna 2012 10:12
- Označen jako odpověď Mike FengMicrosoft Contingent Staff, Moderator 1. května 2012 12:20
-
29. dubna 2012 15:34
"Shared" both terms do in fact not describe the reality.
Be aware that VB has also a "static" keyword, which describes a value which is statically inside a method (in fact a shared member on global level is created for that).
Success
Cor- Upravený Cor LigthertMVP 29. dubna 2012 15:34
- Navržen jako odpověď Heslacher 30. dubna 2012 10:12
- Označen jako odpověď Mike FengMicrosoft Contingent Staff, Moderator 1. května 2012 12:21
-
30. dubna 2012 9:55
Public ReadOnly intVersionMajor As Integer = CInt(My.Application.Info.Version.Major)
A 'Static', read only value assigned at runtimePublic Const intVersionMajor As Integer = 22
A Constant assigned at design timePublic Function GetVersion(ByValue strType As String) as Integer
Select Case StrType
Case is = "Major"
GetVersion = CInt(My.Application.Info.Version.Major)
Case is = "MinorRevision"
GetVersion = CInt(My.Application.Info.Version.MajorRevision)
Case is = "Minor"
GetVersion = CInt(My.Application.Info.Version.Minor)
Case Else
GetVersion = CInt(My.Application.Info.Version.MinorRevision)
End Select
End Function
A Function that returns application propertiesI generally put such variables into a module that is loaded at startup, and the module is used similarly to a dll. It can be seperated later and turned into dll if desired.
- Upravený Dan_Kirk 30. dubna 2012 9:59