Difference of coding in vb.net and C# issue - c#

Guys I am working with an API integration project in vb.net. Result from API is returned in XML format and I have been provided with some some of the classes which help me to navigate through xml. It's one of the class is WSGetFareQuoteRequest which has many properties. And one of its properties is Result which is defined in the integration document as:
Response Description
Structure of WSGetFareQuoteResponse is as follows:
Field Name DataType Remarks
Result* WSResult This result will
comprise of the new
fare(if any of the
component of fare
gets updated),
otherwise it will
remain the same.
Status WSStatus It will be having the
status or error
information.
When I asked API team regarding the initialisation of this result property of this object they provided me c# code as shown below :
WSGetFareQuoteRequest wsFareQuoteRequest = new WSGetFareQuoteRequest();
WSGetFareQuoteResponse wsFareQuoteResponse = new WSGetFareQuoteResponse();
int nor = 1;
if (searchResponse.Result != null && searchResponse.Result.Length 0 &&
objResult[i].IsLcc)
{
wsFareQuoteRequest.Result = new WSResult[nor];
wsFareQuoteRequest.Result[0] = new WSResult();
wsFareQuoteRequest.Result[0] = objResult[i];
wsFareQuoteRequest.SessionId = searchResponse.SessionId;
wsFareQuoteResponse = bApi.GetFareQuote(wsFareQuoteRequest);
Now my question is that I am doing the project in vb.net.I tried its vb.net equivalent it is showing me error at first line of object creation of Result property.I tried its vb.net equivalent as:
wsFareQuoteRequest.Result = New WSResult(nor)
'Only above line is creating error as Too many arguments to Pub Sub New'
wsFareQuoteRequest.Result(0) = New WSResult()
wsFareQuoteRequest.Result(0) = searchresponse.Result(i)
wsFareQuoteRequest.SessionId = searchresponse.SessionId
wsFareQuoteResponse = bapi.GetFareQuote(wsFareQuoteRequest)

Arrays in Visual Basic > Creating an Array
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
and in your case it would be:
wsFareQuoteRequest.Result = New WSResult(nor) {}
you are missing the {} at the end.

Related

How to add encounter using Fhir R4 by c#

I've tried add new encounter but it shows "System.NullReferenceException: 'Object reference not set to an instance of an object'". Heare is my code:
private static void AddEncounter()
{
var encount = new Encounter();
encount.Identifier.Add(new Identifier
{
Use = (Identifier.IdentifierUse?)1,
System = "http://www.amc.nl/zorgportal/identifiers/visits",
Value = "tek001"
}) ;
encount.Class = new Coding("http://terminology.hl7.org/CodeSystem/v3-ActCode", "SS", "Lưu trú ngắn hạn");
encount.Status = 0;
encount.Priority = new CodeableConcept("http://terminology.hl7.org/CodeSystem/v3-ActPriority", "R", "routine");
encount.Subject = new ResourceReference("Patient/a07b880381ec44ad8f80743f396c8011/_history/1", "Lâm");
encount.Length.Value = 120;
encount.Length.Unit = "min";
encount.Length.System = "http://unitsofmeasure.org";
encount.Length.Code = "min";
encount.ReasonCode.Add(new CodeableConcept("http://snomed.info/sct", "184004", "Rối loạn nhịp tim rút"));
encount.Hospitalization.PreAdmissionIdentifier.Use = (Identifier.IdentifierUse?)1;
encount.Hospitalization.PreAdmissionIdentifier.System = "http://www.amc.nl/zorgportal/identifiers/pre-admissions";
encount.Hospitalization.PreAdmissionIdentifier.Value = "1598753";
encount.Hospitalization.AdmitSource = new CodeableConcept("http://terminology.hl7.org/CodeSystem/admit-source", "outp", "Khoa ngoại trú");
encount.Hospitalization.DischargeDisposition = new CodeableConcept("http://terminology.hl7.org/CodeSystem/discharge-disposition", "hosp", "Bệnh nhân đã được xuất viện và chăm sóc giảm nhẹ");
Console.WriteLine("Successful");
Console.ReadLine();
}
How can i add an exactly?
Just like you do with creating the Identifier for the Identifier field and the other complex objects for the Class, Priority and Subject fields, you will need to create a Duration for the Length field:
encount.Length = new Duration();
encount.Length.Value = 120;
// etc.
You will have to do this for all of the complex objects you use in your code, so also for the Hospitalization field and the PreAdmissionIdentifier:
encount.Hospitalization = new Encounter.HospitalizationComponent();
encount.Hospitalization.PreAdmissionIdentifier = new Identifier();
// etc.
Another change I would like to advice, is to make use of the values provided in the enum for the identifier use, to make your code more readable and your intention clear:
encount.Hospitalization.PreAdmissionIdentifier.Use = Identifier.IdentifierUse.Official;
It's difficult to say for certain without seeing the entire class but System.NullReferenceException is thrown when trying to access something that has not been instantiated.
encount.Hospitalization is likely a reference to another class which must first be instantiated. Try first creating and instance before accessing (example below).
encount.Length = new Length();
encount.Hospitalization = new Hospitalization();
Note: I'm guessing the class names (Length and Hospitalization) so you may need to adjust for your code. The main idea is that you must create the class (new ...()) before utilizing\accessing.

How to populate XML object with data, based on XSD

I need to export data into XML file, using XSD. There are many examples how to do it, but most of them do not show how to popuate the actual data, but to save the object as an XML. The one I could find didn't work for me.
1) I use an xsd file of Agresso http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.html
which I have successfully downloaded and generated a class with xsd.exe command.
2) I have added this class to my project. ABWInvoice is the class for the complexType element Invoice. The xml may contain more than one invoice, hence its maxOccurs is set to "unbounded". Each Invoice can have InvoiceNo element and Header complex element.
3) I have started to write the code and first thought I can use a list, as the number of invoices is dynamic. But List<ABWInvoice> list = new ABWInvoice(); didn't work "Cannot implicitly convert type 'abc.Agresso.ABWInvoice' to 'System.Collections.Generic.List'", so I have decided to at least try to have one record and go from there, but oAgresso.Invoice[0].Header fails in runtime with System.NullReferenceException: 'Object reference not set to an instance of an object.'
private void CreateXMLHeader()
{
var oAgresso = new ABWInvoice { };
oAgresso.Invoice[0] = new ABWInvoiceInvoice
{ InvoiceNo = "1" };
oAgresso.Invoice[0].Header = new ABWInvoiceInvoiceHeader()
{
OrderRef = "5678",
InvoiceDate = Date.Now
};
//var agressoXMLImport = Shared.XMLHelper.ReadXml<ABWInvoice>(#"E:\temp\ABW_Invoice_Test.xml");
Shared.XMLHelper.SaveXml<ABWInvoice>(oAgresso, #"e:\temp\ABW_Export_Test.xml");
}
Can you advise on how
1) build a dynamic array (I do not know the amount of invoices, when I start building the XML;
2)What is wrong with my current code?
Much appreciated!
Member arrays need to be initialized with known size, so its easier to make a List of ABWInvoiceInvoice then populate it with your data by using add method and at the end assign whole list to your member array
private void CreateXMLHeader()
{
var oAgresso = new ABWInvoice { };
List<ABWInvoiceInvoice> invlist = new List<ABWInvoiceInvoice>();
invlist.Add(new ABWInvoiceInvoice { InvoiceNo = "1" ,
Header= new ABWInvoiceInvoiceHeader()
{
OrderRef = "5678",
InvoiceDate = DateTime.Now
}
});
oAgresso.Invoice = invlist.ToArray();

Looking for examples how to use the Google Sheets API FilterCriteria object

I am trying to understand how to construct an AddFilterViewRequest in the Google Sheets API. However, there don't seem to be any good examples that I can locate in any programming language which demonstrate how it is used.
Specifically, I am trying to understand the FilterCriteria object, and what I need to set hiddenValues and condition to.
In my application I am trying to construct a filter that will only show the rows where the cell in the column I have selected is not empty. I can do this manually in the Google Sheets editor, and I want to replicate the same settings in my program.
This is the code as it stands...
Request request = new Request();
request.AddFilterView = new AddFilterViewRequest();
request.AddFilterView.Filter = new FilterView();
request.AddFilterView.Filter.FilterViewId = 0;
request.AddFilterView.Filter.Title = "Hide rows with errors";
request.AddFilterView.Filter.Range = new GridRange();
request.AddFilterView.Filter.Range.SheetId = 0;
request.AddFilterView.Filter.Range.StartColumnIndex = 8;
request.AddFilterView.Filter.Range.EndColumnIndex = 9;
FilterCriteria criteria = new FilterCriteria();
//criteria.Condition = BooleanCondition;
criteria.HiddenValues = new List<string>();
//criteria.HiddenValues.Add("item");
IDictionary<string, FilterCriteria> criteriaDictionary = new Dictionary<string, FilterCriteria>();
//criteriaDictionary.Add("string", criteria);
request.AddFilterView.Filter.Criteria = criteriaDictionary;
The lines that are commented out at the moment are the ones that I can seeking assistance with. I am also trying to find out what the string variable should be for the criteriaDictionary.
After posting this question I realised one way I could answer it myself would be to reverse engineer an existing spreadsheet where this filter was already applied. Based on that, I now have the following working code...
FilterCriteria criteria = new FilterCriteria();
criteria.Condition = new BooleanCondition();
criteria.Condition.Type = "NOT_BLANK";
IDictionary<string, FilterCriteria> criteriaDictionary = new Dictionary<string, FilterCriteria>();
criteriaDictionary.Add("8", criteria);
Request request = new Request();
request.AddFilterView = new AddFilterViewRequest();
request.AddFilterView.Filter = new FilterView();
request.AddFilterView.Filter.FilterViewId = 0;
request.AddFilterView.Filter.Title = "Hide rows with errors";
request.AddFilterView.Filter.Range = range1;
request.AddFilterView.Filter.Criteria = criteriaDictionary;
requests.Add(request);
I don't know why they key value was set to 8, but that's what it was in the existing spreadsheet (and that sheet only had one Filter View in it). Anyway I copied it, and it works, so I haven't found a need to change it.
I don't have a good setup right now to construct the appropriate code here (perhaps someone else can amend this or add an answer with the code inline), but in general your best bet right now for figuring out the "right way" to construct complex objects like this in the API is to create one with the UI and then do a spreadsheets.get call. You can look at the resulting object and mimic the FilterView (with your own modifications as necessary).

VB WPF to C# WPF

I'm attempting to move from using VB WPF to C# WPF, What I have attempted so far is using an online converter because of the amount of code I have. The problem being that I have come into some troubles understanding some errors presented and being a beginner in C# I'm a little lost.
The code below is what I'm currently using with standard VB WPF and work perfectly fine and a copy of what the c# converter changes it into. (Note I have added Bing Maps WPF Reference to both VB and C#)
Private Sub Aberdeen() Handles BTNCounty.Click
If TXTCounty.Text = "Aberdeen" Then
Dim CountyLocation(2) As Microsoft.Maps.MapControl.WPF.Location
CountyLocation(0) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
CountyLocation(1) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
CountyLocation(2) = New Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633)
Dim names = New String() {"Aberdeen Central", "Aberdeen Lochnagar", "Aberdeen Kincorth"}
For index = 0 To CountyLocation.Length - 1
Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
Dim CoordinateTip = New ToolTip()
CoordinateTip.Content = names(index)
Pin.Location = CountyLocation(index)
Pin.ToolTip = CoordinateTip
BingMap.Children.Add(Pin)
Next
End If
End Sub
Below is the example of the converted code into c#
private void Aberdeen()
{
if (TXTCounty.Text == "Aberdeen") {
Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];
CountyLocation(0) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
CountyLocation(1) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
CountyLocation(2) = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);
dynamic names = new string[] {
"Aberdeen Central",
"Aberdeen Lochnagar",
"\tAberdeen Kincorth"
};
for (index = 0; index <= CountyLocation.Length - 1; index++) {
dynamic Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();
dynamic CoordinateTip = new ToolTip();
CoordinateTip.Content = names(index);
Pin.Location = CountyLocation(index);
Pin.ToolTip = CoordinateTip;
BingMap.Children.Add(Pin);
}
}
}
I recieve 3 errors which I was wondering if you could tell me what they mean and how to resolve the issue?
CountyLocation is a variable but used like a method?
2 The name index does not exist in the current context?
3 System.Windows.FrameworkElement.ToolTip is a property but is used like a type?
Any help would be much appreciated as this very much foreign territory for me.
Please see the answers inline.
The main issue is that the converter has converted all your type inference calls (Dim variable = ...) to dynamics, which is incorrect. You should use var for type inference.
private void Aberdeen()
{
if (TXTCounty.Text == "Aberdeen") {
Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];
// Error 1: Setting array variables is done using square brackets, otherwise it's considered a method invocation
CountyLocation[0] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
CountyLocation[1] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
CountyLocation[2] = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);
// extra: you don't need dynamic here, just var will do
var names = new string[] {
"Aberdeen Central",
"Aberdeen Lochnagar",
"\tAberdeen Kincorth"
};
// Error 2: you need to declare the index variable (added var)
for (var index = 0; index <= CountyLocation.Length - 1; index++) {
// Error 3: don't need dynamic here
var Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();
// don't need dynamic here
var CoordinateTip = new ToolTip();
// Same as error 1: Array access is done with square brackets
CoordinateTip.Content = names[index];
// Same as error 1: Array access is done with square brackets
Pin.Location = CountyLocation[index];
Pin.ToolTip = CoordinateTip;
BingMap.Children.Add(Pin);
}
}
}

Pass in an array of PaymentDetailsItemType to PayPalAPI

I am using the PayPal sandbox in ASP.Net C# 4.0. I added the following web reference:
https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl
If i have 1 item (gimme 2 t-shirts please) i can pass it to the PayPal object and complete my transactions with no problems whatsoever:
var item = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType
{
Quantity = 2, //etc...etc
};
var paymentDetails = new PayPalAPIHelper.PayPalWS.PaymentDetailsType();
paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{item};
Now my dilemma is when a customer wants different items (1 t-shirt, 1 pair of pants). Now i need multiple PaymentDetailsItemType objects to pass to the paymentDetails object. I tried this:
//now i have an array of different items
var items = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[3];
items[0].Description = "T-shirt";
items[1].Description = "Jeans";
paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{items};
I get this error:
Cannot implicitly convert type 'PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[]'
to 'PayPalAPIHelper.PayPalWS.PaymentDetailsItemType'
I can do this and it compiles:
paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{items[0],items[1]};
But when I run it, i get Data type '' mismatch in element 'Quantity' when i call the PayPal service:
PayPalAPIHelper.PayPalWS.PaymentDetailsType paymentDetails = new PayPalAPIHelper.PayPalWS.PaymentDetailsType();
paymentDetails.PaymentDetailsItem = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[] { items[0],items[1] };
reqDetails.PaymentDetails = new PayPalAPIHelper.PayPalWS.PaymentDetailsType[] { paymentDetails };
PayPalAPIHelper.PayPalWS.SetExpressCheckoutReq req = new PayPalAPIHelper.PayPalWS.SetExpressCheckoutReq()
{
SetExpressCheckoutRequest = new PayPalAPIHelper.PayPalWS.SetExpressCheckoutRequestType()
{
Version = Version,
SetExpressCheckoutRequestDetails = reqDetails
}
};
// query PayPal and get token
//error occurs here
PayPalAPIHelper.PayPalWS.SetExpressCheckoutResponseType resp = BuildPayPalWebservice().SetExpressCheckout(req); //this is where the error occurs
How do i pass multiple types to the PayPal API web service so the user can order different items? Thanks in advance
First, you already have an array, so it makes no sense to create a new one just for the sake of assignment into paymentDetails.PaymentDetailsItem:
var items = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[3];
…
paymentDetails.PaymentDetailsItem = items; // this is wrong: new PaymentDetailsItemType[]{items};
By writing new PaymentDetailsItemType[]{items} you create a new array and try to initialize its first item (of type PaymentDetailsItemType) with the contents of another array items (of type PaymentDetailsItemType[] — hence incompatible).
Second, make sure you fill all required properties for all items in the array. Most likely, in your example of one t-shirt and one jeans you should do:
items[0].Description = "T-shirt";
items[0].Quantity = 1;
items[1].Description = "Jeans";
items[1].Quantity = 1;

Categories

Resources