syntax error in New Form() With {} vb.net - vb.net

Dim name As String = "hello"
If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}.Show()
End If
Syntax error in New , if i remove all code and write Dim F As New Faker() With {} and F.show() no error but not work and give me error while running the program object reference not set to an instance of an object
can any one here help me pls

There are two answers here Constructing an object and calling a method without assignment in VB.Net that should help.
Use With ... End With
Use Call
I think you're having an issue because you're trying to use the C# style of creating an instance of an object without assigning it before chaining a method call.
Your code becomes:
Dim name As String = "hello"
If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
With New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}
.Show()
End If
Or
Dim name As String = "hello"
If CType(My.Application.OpenForms(name), Faker) Is Nothing Then
Call New Faker() With {.Name = name, .Title = String.Format("{0} - ID:{1}", "hello", Me.ClassClient.ClientAddressID)}.Show()
End If

Related

Error when creating a List object in VB.Net

I am new to .Net framework and I am getting an error message
Value of type 'List(Of AdminSetEmployeeParams)' cannot be converted to
'AdminSetEmployeeParams'"
Dim SetNewEmployee As New List(Of AdminSetEmployeeParams)
SetNewEmployee.Add(New AdminSetEmployeeParams With {
.departmentId = ddlDept.SelectedValue,
.familyName = txtLastOrSurname.Text,
.firstName = txtFirstOrGivenName.Text,
.secondName = txtSecondName.Text,
.contactPhone = txtPhone.Text,
.user = ""})
SetNewEmployee = EmployeeAPIService.AdminSetEmployee(SetNewEmployee).Result
How would I resolve this error?
Update:
System.Threading.Tasks
Public Class Task(Of TResult)
Public ReadOnly Property Result As TResult
It's probably this, but it's hard to be sure:
Dim newEmployee As New AdminSetEmployeeParams
newEmployee = New AdminSetEmployeeParams With {
.departmentId = ddlDept.SelectedValue,
.familyName = txtLastOrSurname.Text,
.firstName = txtFirstOrGivenName.Text,
.secondName = txtSecondName.Text,
.contactPhone = txtPhone.Text,
.user = ""})
Dim newEmployeeResult As List(Of AdminSetEmployeeParams) = EmployeeAPIService.AdminSetEmployee(newEmployee).Result
I'm not sure why your API returns a list of employees in its result (I'm assuming it does because you don't say you have an error message complaining about the assignment of the result to a list(of...) ) but the error as given would reasonably only occur if the API call demanded a single new employee and you handed it a list of new employees
If it doesn't work out, try this last line
Dim newEmployeeResult As AdminSetEmployeeParams = EmployeeAPIService.AdminSetEmployee(newEmployee).Result
And if that doesn't work out, edit your question to give more info on what kind of arguments AdminSetEmployee takes and what kind of object .Result is

query an exchange distribution list

I'm trying to find some code that I can use in vb.net 4.0 to query the our exchange 2013 server. It will be housed on a web server and that server does not have outlook installed on it. Looks like I need to use EWS to do this but I've tried a lot of code snippets and still have not been able to figure this out. The distribution list i'm trying to query is in the public folders/Office Contacts. I've tried examples that use nesting to go through the public folder seen there is no deep traversal but I'm not doing something right there. I am not posting code because i'm not sure it would help. I was hoping someone has already done this and would give me some nuggest of info to get me started.
The examples I've found do not query the distribution list but rather add to it. It's not that I haven't tried... I've got hundreds of lines of code from different places that I've tried and tried to learn from.. but i'm not getting it done. Anyway.. help would be great.
Sorry about not posting any code.. I actually thought I deleted this post.. but i'll post the code that is now working for me. This code does a query to the public folder and then grabs some of the data about each contact in that contact list.
Public Sub MS()
Dim oTheListS As New List(Of TheList)
Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
service.Credentials = New WebCredentials("userid", "password")
service.AutodiscoverUrl("email#address")
'Get Public Folder
Dim sf As SearchFilter = New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Office Contacts")
Dim rrRes As FindFoldersResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, sf, New FolderView(1))
Dim OfficeContacts As Folder = rrRes.Folders(0)
'Find the Distribution List
Dim dlSearch As SearchFilter = New SearchFilter.IsEqualTo(ContactGroupSchema.DisplayName, "Merit Board")
Dim ivItemView As New ItemView(1)
Dim fiResults As FindItemsResults(Of Item) = OfficeContacts.FindItems(dlSearch, ivItemView)
If fiResults.Items.Count = 1 Then
'Enumeate Members
Dim cg As ContactGroup = DirectCast(fiResults.Items(0), ContactGroup)
cg.Load()
For Each gm As GroupMember In cg.Members
Dim o As New TheList
o = MS2(gm.AddressInformation.Address)
oTheListS.Add(o)
'Dim o As New TheList
'Dim ncCol As NameResolutionCollection = service.ResolveName(gm.AddressInformation.Address, ResolveNameSearchLocation.ContactsOnly, True)
'With o
' .Name = gm.AddressInformation.Name
' .Email = gm.AddressInformation.Address
'End With
'oTheListS.Add(o)
Next
End If
End Sub
Public Function MS2(pEmail As String) As TheList
Dim o As New TheList
Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
service.Credentials = New WebCredentials("userid", "password")
service.AutodiscoverUrl("email#address")
Dim sf As SearchFilter = New SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Office Contacts")
Dim rrRes As FindFoldersResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, sf, New FolderView(1))
Dim OfficeContacts As Folder = rrRes.Folders(0)
'Find the Distribution List
Dim dlSearch As SearchFilter = New SearchFilter.IsEqualTo(ContactSchema.EmailAddress1, pEmail)
Dim ivItemView As New ItemView(1)
Dim fiResults As FindItemsResults(Of Item) = OfficeContacts.FindItems(dlSearch, ivItemView)
If fiResults.Items.Count = 1 Then
Dim con As Contact = fiResults.Items(0)
'Dim ncCol As NameResolutionCollection = service.ResolveName(gm.AddressInformation.Address, ResolveNameSearchLocation.ContactsOnly, True)
With o
If con.DisplayName IsNot Nothing Then
.Name = con.DisplayName
End If
Dim em As New EmailAddress
If con.EmailAddresses.TryGetValue(EmailAddressKey.EmailAddress1, em) = True Then
.Email = con.EmailAddresses(EmailAddressKey.EmailAddress1).ToString
End If
If con.JobTitle IsNot Nothing Then
.Title = con.JobTitle
End If
Dim phy As New PhysicalAddressEntry
If con.PhysicalAddresses.TryGetValue(PhysicalAddressKey.Business, phy) = True Then
.Address = con.PhysicalAddresses(PhysicalAddressKey.Business)
End If
If con.PhoneNumbers.TryGetValue(PhoneNumberKey.BusinessPhone, String.Empty) = True Then
.PhoneBusiness = con.PhoneNumbers(PhoneNumberKey.BusinessPhone)
End If
If con.PhoneNumbers.TryGetValue(PhoneNumberKey.MobilePhone, String.Empty) = True Then
.PhoneMobile = con.PhoneNumbers(PhoneNumberKey.MobilePhone)
End If
If con.CompanyName IsNot Nothing Then
.Comapny = con.CompanyName
End If
End With
End If
Return o
End Function
Public Class TheList
Public Property Name As String
Public Property Email As String
Public Property PhoneMobile As String
Public Property PhoneBusiness As String
Public Property Comapny As String
Public Property Title As String
Public Property Address As PhysicalAddressEntry
End Class
I just got it working so I haven't started to refine it yet.. but hopefully this will help someone else as I didn't find any code that did this

LINQ Query Causing Exit Sub or Swallowing Error

My code is as follows:
Using _EntityModel As New AboveNemaSalesDatabaseEntities()
Dim _SelectActiveOptionCodes = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select _OptCodes.fdDescription, _OptCodes.fdOptionCode).ToList()
Dim _SelectActiveOptionCodes2 = (From _OptCodes In _EntityModel.tblOptionCodes
Where _OptCodes.fdStatus = "A"
Select New optionCodes With {.description = _OptCodes.fdDescription,
.optionCode = _OptCodes.fdOptionCode})
sortableOptionCodes = _SelectActiveOptionCodes2
sortedOptionCodes = _SelectActiveOptionCodes2
OptionCodeListBox.DataSource = sortedOptionCodes
OptionCodeListBox.DisplayMember = "fdDescription"
OptionCodeListBox.ValueMember = "fdOptionCode"
End Using
The first query works fine and returns a list in the format [index]{description = "descritption here", optionCode = "option code here"}
The second query creates but when it is called to save to my custom class the program exits the sub or swallows an error. Stepping through the code, the line starting with sortedOptionCodes and after never runs.
The main issue I was dealing with is that my query was producing a list of optionCodes and my variable was not prepared to store this.
Old variables:
Dim sortableOptionCodes As optionCodes
Dim sortedOptionCodes As optionCodes
New variables:
Dim sortableOptionCodes As List(Of optionCodes)
Dim sortedOptionCodes As List(Of optionCodes)
I also added a .ToList() function to the end of the second query.

Vb.net Code syntax error

I have following vb.net code and i am getting syntax error in it
Update
Protected Sub OpenLogin_Click(ByVal src As Object, ByVal e As CommandEventArgs)
Dim StrUri As String = e.CommandArgument.ToString()
Dim openid As New OpenIdRelyingParty()
Dim b = New UriBuilder(Request.Url)
With Key
.Query = ""
End With
'var b = new UriBuilder(Request.Url) { Query = "" };
Dim req = openid.CreateRequest(StrUri)
Dim fetchRequest = New FetchRequest()
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email)
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First)
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last)
req.AddExtension(fetchRequest)
req.RedirectToProvider()
End Sub
Now the error in code is "Key is Not declared" What should i do now for this
Two problems:
The Key reserved word is only to be used when identifying a multi-part key for grouping using Enumerable.GroupBy. It is not required for setting object properties inline with their constructor.
You are experiencing a problem whereby you have separated the With decorator onto a new line, which is syntactically incorrect because you are now treating it as a With block, which means that every dot access will target the variable immediately following the With statement. You need to use a line continuation character or put With on the same line as the object constructor:
Ex 1
Dim b = New UriBuilder(Request.Url) With {
.Query = ""
}
Ex 2
Dim b = New UriBuilder(Request.Url) _
With { .Query = "" }
EDIT:
You cannot use this syntax with a Visual Studio 2005/.NET 2.0 project. Just construct the object then initialize the property:
Dim b As New UriBuilder(Request.Url)
b.Query = ""
AFAIK, you don't need the curly braces.
With Something
.Property1 = True
.Property2 = "Inactive"
' And so on
End With
UPDATE
With Key
.Query = ""
End With
Why do you need a With clause here anyway ? The above is syntactically equivalent to
Key.Query = ""
Declare the Key variable or else remove it from your code.

Strange error reported in this code, please explain?

I put together this bit of code from a few other samples, and I am getting an error I cant understand. On this line in the code below, on the word Observer,
Dim Results As ManagementObjectCollection = Worker.Get(Observer)
I get the error
"Value of type 'System.Management.ManagementOperationObserver' cannot be converted to 'Integer'"
Can somebody explain what this means?
There are two signatures for ManagementObjectSearcher.Get(), one has no parameters and the other has one parameter, a ManagementOperationObserver for async operation. That is what I am providing, yet the error indicates conversion involving an integer?
Public Shared Sub WMIDriveDetectionASYNC(ByVal args As String())
Dim Observer As New ManagementOperationObserver()
Dim completionHandler As New MyHandler()
AddHandler Observer.Completed, AddressOf completionHandler.Done
Dim Machine = "192.168.0.15"
Dim Scope = New ManagementScope("\\" & Machine & "\root\cimv2")
Dim QueryString = "select Name, Size, FreeSpace from Win32_LogicalDisk where DriveType=3"
Dim Query = New ObjectQuery(QueryString)
Dim Worker = New ManagementObjectSearcher(Scope, Query)
Dim Results As ManagementObjectCollection = Worker.Get(Observer) 'use parameter to make async
For Each item As ManagementObject In Results
Console.WriteLine("{0} {2} {1}", item("Name"), item("FreeSpace"), item("Size"))
Dim FullSpace As Long = (CLng(item("Size")) - CLng(item("FreeSpace"))) \ 1000000
Console.WriteLine(FullSpace)
Next
End Sub
Public Class MyHandler
Private _isComplete As Boolean = False
Public Sub Done(sender As Object, e As CompletedEventArgs)
_isComplete = True
End Sub 'Done
Public ReadOnly Property IsComplete() As Boolean
Get
Return _isComplete
End Get
End Property
End Class
Thanks for any advice!
I think that uses a reference type to get the result and put it in the object you sent as a parameter. So I think it just needs to look like:
Worker.Get(Observer)
instead of trying to set something = to that since it isn't a function that returns a value.
Then use the events you hook up to the object to handle whatever you need to do with the items you find.