PageRenderTime 92ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/SimpleDemo/Program.vb

#
Visual Basic | 129 lines | 113 code | 9 blank | 7 comment | 1 complexity | 55811d70145d5044940706be036c2833 MD5 | raw file
Possible License(s): LGPL-2.1
  1. Module Program
  2. Sub Main()
  3. Console.WriteLine("SPSS file writing demo:")
  4. If File.Exists("example.sav") Then File.Delete("example.sav")
  5. Using doc As SpssDataDocument = SpssDataDocument.Create("example.sav")
  6. CreateMetaData(doc)
  7. CreateData(doc)
  8. End Using
  9. Console.WriteLine("Examine example.sav for the results.")
  10. Console.WriteLine("SPSS file reading demo:")
  11. Using doc As SpssDataDocument = SpssDataDocument.Open(GetFileName, SpssFileAccess.Read)
  12. PrintMetaData(doc)
  13. PrintData(doc)
  14. End Using
  15. Console.WriteLine("Exporting a DataTable demo... (the source code is interesting)")
  16. Dim dt As DataTable = SpssConvert.ToDataTable(GetFileName)
  17. If File.Exists("example2.sav") Then File.Delete("example2.sav")
  18. SpssConvert.ToFile(dt, "example2.sav", AddressOf MetaDataCallback)
  19. Console.WriteLine("SPSS dictionary copying demo:")
  20. If File.Exists("example3.sav") Then File.Delete("example3.sav")
  21. Using doc As SpssDataDocument = SpssDataDocument.Create("example3.sav", GetFileName)
  22. PrintMetaData(doc)
  23. End Using
  24. Console.WriteLine("Demo concluded. Press any key to end.")
  25. Console.ReadKey()
  26. End Sub
  27. Function GetFileName() As String
  28. If File.Exists("..\..\demo.sav") Then Return "..\..\demo.sav"
  29. If File.Exists("..\..\..\demo.sav") Then Return "..\..\..\demo.sav"
  30. If File.Exists("..\..\..\..\demo.sav") Then Return "..\..\..\..\demo.sav"
  31. If File.Exists("demo.sav") Then Return "demo.sav"
  32. Throw New ApplicationException("Cannot find demo.sav file.")
  33. End Function
  34. Sub CreateMetaData(ByVal doc As SpssDataDocument)
  35. ' Define dictionary
  36. Dim v1 As New SpssStringVariable()
  37. v1.Name = "v1"
  38. v1.Label = "What is your name?"
  39. doc.Variables.Add(v1)
  40. Dim v2 As New SpssNumericVariable
  41. v2.Name = "v2"
  42. v2.Label = "How old are you?"
  43. doc.Variables.Add(v2)
  44. Dim v3 As New SpssNumericVariable
  45. v3.Name = "v3"
  46. v3.Label = "What is your gender?"
  47. v3.ValueLabels.Add(1, "Male")
  48. v3.ValueLabels.Add(2, "Female")
  49. doc.Variables.Add(v3)
  50. Dim v4 As New SpssDateVariable
  51. v4.Name = "v4"
  52. v4.Label = "What is your birthdate?"
  53. doc.Variables.Add(v4)
  54. ' Add some data
  55. doc.CommitDictionary()
  56. End Sub
  57. Sub CreateData(ByVal doc As SpssDataDocument)
  58. Dim case1 As SpssCase = doc.Cases.[New]()
  59. case1("v1") = "Andrew"
  60. case1("v2") = 24
  61. case1("v3") = 1
  62. case1("v4") = DateTime.Parse("1/1/1982 7:32 PM")
  63. case1.Commit()
  64. Dim case2 As SpssCase = doc.Cases.[New]()
  65. case2("v1") = "Cindy"
  66. case2("v2") = 21
  67. case2("v3") = 2
  68. ' SW - datetime parse fails on any non-US locale system changed
  69. ' to international date format
  70. case2("v4") = DateTime.Parse("2002-12-31")
  71. case2.Commit()
  72. End Sub
  73. Sub MetaDataCallback(ByVal var As SpssVariable)
  74. ' In a real application, you would probably draw this metadata out of
  75. ' some repository of your own rather than hard-coding the labels.
  76. Select Case var.Name
  77. Case "v1"
  78. var.Label = "What is your name?"
  79. Case "v2"
  80. var.Label = "How old are you?"
  81. Case "v3"
  82. var.Label = "What is your gender?"
  83. ' Set the value labels
  84. Dim numericVar As SpssNumericVariable = CType(var, SpssNumericVariable)
  85. numericVar.ValueLabels(1) = "Male"
  86. numericVar.ValueLabels(2) = "Female"
  87. Case "v4"
  88. var.Label = "What is your birthdate?"
  89. End Select
  90. End Sub
  91. Sub PrintMetaData(ByVal doc As SpssDataDocument)
  92. Console.WriteLine("Variables:")
  93. For Each var As SpssVariable In doc.Variables
  94. Console.WriteLine("{0}" & vbTab & "{1}", var.Name, var.Label)
  95. If TypeOf var Is SpssNumericVariable Then
  96. Dim varNum As SpssNumericVariable = CType(var, SpssNumericVariable)
  97. For Each label As KeyValuePair(Of Double, String) In varNum.ValueLabels
  98. Console.WriteLine(vbTab & label.Key.ToString & vbTab & label.Value.ToString)
  99. Next
  100. End If
  101. Next
  102. End Sub
  103. Sub PrintData(ByVal doc As SpssDataDocument)
  104. For Each var As SpssVariable In doc.Variables
  105. Console.Write(var.Name & vbTab)
  106. Next
  107. Console.WriteLine()
  108. For Each row As SpssCase In doc.Cases
  109. For Each var As SpssVariable In doc.Variables
  110. If (row(var.Name) Is Nothing) Then
  111. Console.Write("<SYSMISS>")
  112. Else
  113. Console.Write(row(var.Name))
  114. End If
  115. Console.Write(vbTab)
  116. Next
  117. Console.WriteLine()
  118. Next
  119. End Sub
  120. End Module