I have a VS2012 Visual Basic.NET application and am wanting to create some unit tests for this application.
In my application, I have a module that contains the following structure:
<Serializable()> Public Structure MediaItem
Dim longMediaID As Long
Dim MediaObject As WMPLib.IWMPMedia
Dim FileInfoObject As FileInfo
Dim stringGenre As String
Dim stringMediaDuration As String
Dim doubleMediaLength As Double
End Structure
How do I 'Import' the above MediaItem structure so that I can do some Unit Testing using this structure? Currently, the TestClass cannot 'see' this structure.
UPDATE
This structure is custom made by myself. It is not actually in a class, it is in a module.
UPDATE 2
Here is the module code:
Imports System.IO
Namespace modCanMediaStructures
Module modCanMediaStructures
<Serializable()> Public Structure MediaItem
Dim longMediaID As Long
Dim MediaObject As WMPLib.IWMPMedia
Dim FileInfoObject As FileInfo
Dim stringGenre As String
Dim stringMediaDuration As String
Dim doubleMediaLength As Double
End Structure
<Serializable()> Public Structure MediaPlayList
Dim stringMediaPlayListName As String
Dim stringMediaPlayListDescription As String
Dim stringMediaPlayListComments As String
Dim mediaItemList As List(Of MediaItem)
Dim stringPlayListDuration As String
Dim doublePlayListDuration As Double
Dim booleanShuffle As Boolean
Dim booleanLoop As Boolean
End Structure
Public Structure genreItem
Dim stringGenreName As String
Dim longGenreCount As Long
Dim doubleGenreDuration As Double
End Structure
<Serializable()> Public Structure ScheduleItem
Dim stringItemName As String
Dim stringItemPlaylist As String
Dim stringItemDuration As String
Dim stringItemComments As String
Dim doubleItemDuration As Double
Dim booleanStartDateIsDerivedFromEndOfOtherScheduleItem As Boolean
Dim stringScheduleItemThatStartDateIsDerivedFrom As String
Dim dateItemStartDateTime As Date
Dim dateItemEndDateTime As Date
Dim booleanEndDateIsDerivedFromPlayListDuration As Boolean
End Structure
End Module
End Namespace
In my main form, I declare a variable as follows:
Dim testMediaItem As modCanMediaStructures.MediaItem
After importing the following statement:
Imports modCanMediaStructures
This works. However, when I do the same statement in the UnitTest test, the following error occurs:
Type 'modCanMediaStructures.MediaItem' is not defined
Here is my code:
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports CanMediaPlayer
Imports modCanMediaStructures
<TestClass()> Public Class MediaListTests
<TestMethod()> Public Sub TestaddMediaItemToList()
Dim myStructure As New modCanMediaStructures.MediaItem
End Sub
End Class
I believe that this is happeneing because the UnitTest Project cannot actually 'see' the code in the other solution files.
May I please have some help to get this working?
Related
I'm trying to make a tool to list and change users group on active directory based on groups that manager has control. I'm stuck on listing function, and it keep getting me this error:
System.DirectoryServices.DirectoryServicesCOMException
Hresult=0x80072032 Message=Invalid distinguished name (DN) syntax specified
Line: Dim searchResults As Search...
Public Class Form1
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim managerID As String
managerID = mngID.Text
Dim employeeID As String
employeeID = empID.Text
Dim emptybox
emptybox = mngID.Text.Length
If emptybox < 8 Then
MsgBox("ManagerID Inválido")
End If
Dim emptybox2
emptybox2 = empID.Text.Length
If emptybox2 < 8 Then
MsgBox("EmployeeID Inválido")
End If
If emptybox = 8 Then
Dim domain = New PrincipalContext(ContextType.Domain)
Dim user = UserPrincipal.FindByIdentity(domain, managerID)
Dim userDN
userDN = user.DistinguishedName
Dim ADEntry As New DirectoryEntry
ADEntry.Path = "LDAP://domain/CN:Users;DC:domain"
Dim Groups As New Collection
Dim mySearcher As DirectorySearcher = New DirectorySearcher(ADEntry)
Dim arrList As New ArrayList()
mySearcher.Filter = "(&(ObjectClass=user)(DistinguisedName=" & userDN & "))"
mySearcher.PropertiesToLoad.Add("MemberOf")
Dim searchResults As SearchResultCollection = mySearcher.FindAll()
If searchResults.Count = 0 Then
MsgBox("ManagerID inválido2")
End If
If searchResults.Count > 0 Then
Dim group As New DirectoryEntry(searchResults(0).Path)
For Each member As Object In group.Properties("MemberOf")
groupbox.Items.Add(member)
Next
End If
End If
End Sub
Your Error
Error 0x80072032 - ERROR_DS_INVALID_DN_SYNTAX
An invalid 'dn' syntax has been specified
Here's something to get you going.
Example VB Script To Connect to Active Directory
Imports System
Imports System.Data
Imports System.Linq
Imports System.IO
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory
Imports System.Security
Imports System.Security.Permissions
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Public Function EnumerateDomains() As ArrayList
Dim alDomains As New ArrayList()
Dim currentForrest As Forest = Forest.GetCurrentForest()
Dim myDomains As DomainCollection = currentForrest.Domains
For Each objDomain As Domain In myDomains
alDomains.Add(objDomain.Name)
Next
Return alDomains
End Function
Public Function EumerateDomainUsers() As ArrayList
Dim domainUsers As New ArrayList()
Dim usr As String
'Dim fqdns As String = DropDownList1.SelectedItem.ToString()
Dim fqdns As String = DropDownList1.SelectedItem.Text
Dim adStrng As String = "LDAP://" & fqdns
Dim adEntry As DirectoryEntry = GetObject(adStrng)
Dim searcher As DirectorySearcher = New DirectorySearcher(adEntry)
searcher.Sort.PropertyName = "cn"
Dim results As SearchResultCollection
Dim result As SearchResult
searcher.PropertiesToLoad.Add("cn")
results = searcher.FindAll
For Each result In results
usr = result.GetDirectoryEntry().Properties("cn").Value
domainUsers.Add(usr)
Next
Return domainUsers
End Function
There is a spelling mistake and should be "DistinguishedName" instead of "DistinguisedName":
mySearcher.Filter = "(&(ObjectClass=user)(DistinguishedName=" & userDN & "))"
Upon trying to run/debug code of a WindowsForm I run into the following exception:
System.InvalidOperationException HResult=0x80131509 Message=An
error occurred creating the form. See Exception.InnerException for
details. The error is: Object reference not set to an instance of an
object. Source=Automated SoundPower Testing StackTrace: at
Automated_SoundPower_Testing.My.MyProject.MyForms.Create__Instance__[T](T
Instance) in :line 190 at
Automated_SoundPower_Testing.My.MyProject.MyForms.get_Form1() at
Automated_SoundPower_Testing.My.MyApplication.OnCreateMainForm() in
C:\Users\M0110823\source\repos\Automated SoundPower Testing\My
Project\Application.Designer.vb:line 35 at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine) at
Automated_SoundPower_Testing.My.MyApplication.Main(String[] Args) in
:line 81
Inner Exception 1: NullReferenceException: Object reference not set to
an instance of an object."
This exception references:
Me.MainForm = Global.Automated_SoundPower_Testing.Form1
I'm thinking this could possibly be due to the fact that I am trying to call Me.Mainform before it has been created? Or is there possibly a naming miscommunication between the directory and the project properties? I have created blank project and transferred code/vb form over and run into the same error.
This leads me to believe that there is some error in my code?
This is the first section of my code, everything after this is categorized into regions under Class Form1.
Imports System.Windows.Forms
Imports System.Windows.Controls.Label
Imports System.IO
Imports System
Imports System.Text.RegularExpressions
Imports Microsoft.Office.Interop
Imports LMSTestLabAutomation
Imports System.IO.File
Imports System.IO.Path
Imports System.Threading
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Class Form1
Dim TL As LMSTestLabAutomation.Application
Public tlDb As IDatabase
Dim SaveDataName As String
'Declare UA variables
Dim UAmodel, UAtype, UAsn, UAoperator, UAdate, UAcomment,
UAtesttype, UAtestpurpose, UAtime, UAtestduration As String
Dim UAlist As New SortedList(Of String, String)
Dim AcqState As String = "Acquisition Offline"
Dim aRecordTime As Boolean() = New Boolean() {}
Dim aRunNames As String() = New String() {}
Dim aProjectNames As String() = New String() {}
Dim aStatus As String() = New String() {}
Dim aStartTrigger As Single() = New Single() {}
Dim aStopTrigger As Single() = New Single() {}
Dim aTriggerCondition As String() = New String() {}
Dim aSequences As Integer() = New Integer() {}
Dim newTestIncrement As Integer
Dim newTest As String() = aRunNames
'Declarations of the used objects
Dim database As LMSTestLabAutomation.IDatabase
Dim datawatch_pictManag As LMSTestLabAutomation.IDataWatch
Dim WithEvents gDataWatch_AcqState As LMSTestLabAutomation.DataWatch
Dim pictManag As LMSTestLabAutomation.IPictureManager
Dim myPicture As LMSTestLabAutomation.IPicture
Dim myBlock As LMSTestLabAutomation.IBlock2
Dim myDisplay As LMSTestLabAutomation.IDisplay
Dim Measure_Port As LMSTestLabAutomation.DataWatch
Dim Open_book = TL.ActiveBook
Dim sequence_number = Open_book(0, 2)
runtime exception
module module1
imports System.Collections.Generic
imports System.Io
sub main()
dim subject as string
dim topic(5) as string
dim question(5) as string
dim foundfile as List
dim directory as string
console.writeline("enter the subject your revising")
subject=console.readline()
directory="C:\Users\Zak\Documents\revisionhog\subjects\physics\imaging"
For each file as String In IO.directory.Getfiles(directory, "*.pdf", IO.SearchOption.AllDirectories)
foundfile.add(file)
Next
for x = 0 to 5
console.writeline(foundFile)
next
end sub
end module
tyring to add file that i have found after a search to a list of strings.
i thought that i had placed import before declarations???
help???
imports System.Collections.Generic
imports System.Io
module module1
sub main()
dim subject as string
dim topic(5) as string
dim question(5) as string
dim foundfile as List
dim directory as string
console.writeline("enter the subject your revising")
subject=console.readline()
directory="C:\Users\Zak\Documents\revisionhog\subjects\physics\imaging"
For each file as String In IO.directory.Getfiles(directory, "*.pdf", IO.SearchOption.AllDirectories)
foundfile.add(file)
Next
for x = 0 to 5
console.writeline(foundFile)
next
end sub
end module
The imports come first, like you mentioned. This fixes the problem with the import statements, but your question is a bit unclear. If you have other issues, please state those clearly.
I'm getting this error when trying to debug my VB code in Visual Studio to interact with a specific WebService .
Im not very familiar with Visual Basic.
The error is on the line Dim ticket_handle As String = " CR 1001 " ws.closeTicket ( Sid , " closed ticket " ticket_handle )
The complete code:
Imports System.IO Imports System.Xml
Imports System.Xml.Serialization
Imports WebReference
Partial Class _Default
Inherits System.Web.UI.Page
Dim ws As New USD_WebService
Dim sid As String
Dim userhandle, username, password As String
Dim attrVal(5), attr(0), prop(0) As String
Dim requestHandle, requestNumber As String
Dim persistent_id As String
Dim catAttrib(5) As String
Sub Main()
Dim ws As New USD_WebService
ws.Url = "http://hummer:8080/axis/services/USD_R11_WebService?wsdl"
Dim username, password
Dim sid
username = "servicedesk"
password = "password"
sid = ws.login(username, password)
Dim userhandle
userhandle = ws.getHandleForUserid(sid, username)
Dim USD As New WebReference.USD_WebService
sid = USD.login(username, password)
Dim ticket_handle As String = “cr:1001” ws.closeTicket(Sid,“ticket fechado”, ticket_handle)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ws.Url = "http://hummer:8080/axis/services/USD_R11_WebService?wsdl"
End Sub
End Class
Can anyone help me plis?!?!
In VB.NET, only one statement may be executed per line. Other languages, like Java or C# use a ';' to denote the end of a statement, however in VB.NET it is the end of a line. The compiler is trying to tell you that you have two statements on a single line and it expects there to only be one.
Dim ticket_handle As String = “cr:1001” ws.closeTicket(Sid,“ticket fechado”, ticket_handle)
Should be
Dim ticket_handle As String = “cr:1001”
ws.closeTicket(Sid,“ticket fechado”, ticket_handle)
I wrote an OPC client using interop.opcautomation.dll. I wrote it as a windows form in vb 2010 express. everything is currently working well. I wanted to make it a windows service though. The problem I am having is that it will not connect to the opc server when I run the service. I get the following exception "Object reference not set to an instance of an object.- OPC Connect Error" I get the exception at the end of the code where I am trying to connect. Below is the code. Thanks for any input, it is appreciated.
Imports System.ServiceProcess
Imports System.Threading
Imports System.Text
Imports System.Windows.Forms
Imports System
Imports System.IO
Imports System.IO.File
Imports System.Net
Imports OPCAutomation
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim WithEvents OPCServer As OPCAutomation.OPCServer
Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroup
Dim Batch As String
Dim Group As String
Dim Press As String
Dim Line As String
Dim sWidth As String
Dim sHeight As String
Dim sBifold As String
Dim sCore As String
Dim StartUp As Integer
Dim iHandle As Integer
Dim sError As String
Dim ItemIds(60) As String
Dim ServerHandles As Array
Dim ClientHandles(60) As Integer
Dim ServerErrors As Array
Dim SnapServerURL As String = "snapserver"
Private trd As Thread
Private Stopping As Boolean = False
Private StoppedEvent As New ManualResetEvent(False)
Protected Overrides Sub OnStart(ByVal args() As String)
EventLog.WriteEntry("PressControlService in OnStart.")
trd = New Thread(AddressOf ServiceMain)
trd.IsBackground = True
trd.Start()
End Sub
Protected Overrides Sub OnStop()
EventLog.WriteEntry("PressControlService in OnStop.")
Me.Stopping = True
Me.StoppedEvent.WaitOne()
End Sub
Private Sub ServiceMain()
Dim strOPCServerName As String
Dim strOPCServerNode As String
Dim strOPCGroupName As String
Dim i As Integer
StartUp = 0
strOPCServerName = "Kepware.KEPServerEX.V5"
strOPCServerNode = ""
strOPCGroupName = "MyGroup"
Try
Dim OPCServer As New OPCAutomation.OPCServer
Catch ex As Exception
EventLog.WriteEntry(ex.Message & " - New OPC Server")
End Try
Try
OPCServer.Connect(strOPCServerName, strOPCServerNode)
Catch ex As Exception
EventLog.WriteEntry(ex.Message & "- OPC Connect Error")
End Try
End Sub
End Class