PDA

View Full Version : VBA - Visual Studio 2010 issue



brstorrie
09-27-2010, 03:14 PM
Hi, I have a macro, that goes a list of strings, separates them by ";" into an array. Each time through, it uses the next array item, and this works fine. My problem happens when I try to use a case statement... I've been banging my head against a wall for some time now, any ideas are appreciated.


Sub TestSplitJoin()
Dim myStr As String
Dim myArray() As String

'string with values, delimited by comma
myStr = "Address;AddressType;Advertiser;AdvertiserType;Article;ArticleType;BaseAdver tisement;BaseEntity;BasePayment;BasePaymentMethod;BasePersonEntity;BasePrin tAdvertisement;BaseUser;BaseWebAdvertisement;CashPaymentMethod;CheckPayment Method;Contact;Country;CreditCardPaymentMethod;Donation;Donor;DonorDonorSta tus;DonorStatus;EmailAddress;EmailAddressType;Enrollment;Family;FamilyMembe r;FamilyMemberType;NamePrefix;NameSuffix;PaymentCategory;PaymentStatus;Pers onInformation;PhoneNumber;PhoneNumberType;PrintPublication;RecurringPayment ;ResourceAdvertisement;ResourceCategory;School;SchoolFamily;SinglePayment;S iteAdType;SiteAdvertisement;StateProvince;Volunteer;VolunteerCategory;Volun teerHours;VolunteerYear;WebSource;WebSourceType;"
'split string into array of substrings
myArray = Split(myStr, ";")
'display array elements
'MsgBox(myArray(0) & vbCr & myArray(1) & vbCr & myArray(2))
'concatenate all elements of array into one string,
'with " and " connecting them
'myStr = Join(myArray, " and ")
'display string
'MsgBox(myStr)

'Dim strArray(0 To 58), strNextItem As String
Dim LoopCount As Long



For LoopCount = LBound(myArray) To UBound(myArray)

strNextItem = myArray(LoopCount + 1)

MsgBox("Current item: " & myArray(LoopCount) & " Next item: " & strNextItem)
'If LoopCount > 70 Then
'Exit For
'End If
If Len(myArray(LoopCount)) = 0 Then Exit For 'if it reached an empty item, quit the loop



'FileToArray("C:\Users\Benjamin Storrie\Downloads\CS\CHEC.git\CHEC.DAL\filelist.txt")

'Create InputBox Messages and Titles
Dim imessage, ititle As String

'Create the Variables as Strings
Dim ix, iXa, iy, iz, ixl, iplural, ipluralmessage, iname, iiname As String
Dim strings As String()
Dim i As Integer
Dim ent As String
Dim quotes As Char = ChrW(34)
ent = ix

strings = New String() {ent}

'Input Box Messages
imessage = "Enter Class Name (e. g. Address)"
ititle = "Class Name"
ipluralmessage = "Enter the correct form of plural(es, s, etc)"

'Get InterfaceName and Create file
iiname = myArray(LoopCount) 'InputBox("Enter Interface Name")
iname = "I" + iiname 'Right(iiname, Len(iiname) - 1)
ix = iiname
'iplural = InputBox(ipluralmessage)
ixl = ix.ToLower

DTE.ExecuteCommand("View.SolutionExplorer")
DTE.ActiveWindow.Object.GetItem("CHEC\CHEC.DAL").Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ItemOperations.AddNewItem("Visual C# Items\Code\Interface", iname + ".cs")
Console.WriteLine(" test every string to see if it starts with 'st'")
'For i = 0 To strings.GetUpperBound(0)

'If strings(i).StartsWith("st") Then
'Console.WriteLine(quotes & strings(i) & quotes & _
' " starts with " & quotes & "st" & quotes)
'End If

'Next

For i = 0 To strings.GetUpperBound(0)
Select Case ix
Case strings(i).EndsWith("y")
ent = Left(ent, Len(ent) - 1)
ent = ent + "ies"
MsgBox(ent)

Case strings(i).EndsWith("ss")
ent = ent + "es"
MsgBox(ent)
Case strings(i).EndsWith("s")
ent = ent
Case Else
ent = ent + "s"
End Select



Next
MsgBox(ent)

'Variable Values
'x = InputBox(message, title)

iy = ent 'ix + iplural
iz = "IQueryable<"








'Select All and Delete
DTE.ActiveDocument.Selection.SelectAll()
DTE.ActiveDocument.Selection.DeleteLeft()

'Begin Coding
DTE.ActiveDocument.Selection.Text = "using System;"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "using System.Collections.Generic;"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "using System.Linq;"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "using System.Text;"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "namespace CHEC.DAL"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "{"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "public interface I"
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "{"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = iz
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "> Get"
DTE.ActiveDocument.Selection.Text = iy
DTE.ActiveDocument.Selection.Text = "();"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = iz
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "> Get"
DTE.ActiveDocument.Selection.Text = iy
DTE.ActiveDocument.Selection.Text = "(int page, int pageSize);"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = iz
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "> Get"
DTE.ActiveDocument.Selection.Text = iy
DTE.ActiveDocument.Selection.Text = "(int page, int pageSize, out int pageCount);"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = " Get"
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "ById(Guid id);"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "Guid Insert"
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "("
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = " "
DTE.ActiveDocument.Selection.Text = ixl
DTE.ActiveDocument.Selection.Text = ");"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "bool Update"
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "("
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = " "
DTE.ActiveDocument.Selection.Text = ixl
DTE.ActiveDocument.Selection.Text = ");"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "bool Delete"
DTE.ActiveDocument.Selection.Text = ix
DTE.ActiveDocument.Selection.Text = "(Guid id);"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "}"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "}"
DTE.ActiveDocument.Save()
'Call ImplementationMacro()

Next LoopCount

End Sub

So, when I run it, it tells me "Reference to an object not set" When I debug, it gives me this: An exception of type 'System.NullReferenceException' occurred in VBAssembly but was not handled in user code

Additional information: Object reference not set to an instance of an object.

With the highlight on the line:
Case strings(i).EndsWith("y")

Thanks!