PageRenderTime 42ms CodeModel.GetById 22ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/SimpleDemo/Program.vb

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