I’m creating an expo sender application which is web based, but I have a problem compiling the project. It keeps me saying
“Type or namespace definition, or end-of-file expected”
My code looks like this and I’ve got it from here
using MobileServices.Client;
using MobileServices.Models;
using System;
using System.Collections.Generic;
using System.Linq;
var expoSDKClient = new PushApiClient();
var pushTicketReq = new PushTicketRequest()
{
PushTo = new List<string>() { "..." },
PushBadgeCount = 7,
PushBody = ""
};
var result = expoSDKClient.PushSendAsync(pushTicketReq).GetAwaiter().GetResult();
if (result?.PushTicketErrors?.Count() > 0)
{
foreach (var error in result.PushTicketErrors)
{
Console.WriteLine($"Error: {error.ErrorCode} - {error.ErrorMessage}");
}
}
var pushReceiptResult = expoSDKClient.PushGetReceiptsAsync(pushReceiptReq).GetAwaiter().GetResult();
if (pushReceiptResult?.ErrorInformations?.Count() > 0)
{
foreach (var error in result.ErrorInformations)
{
Console.WriteLine($"Error: {error.ErrorCode} - {error.ErrorMessage}");
}
}
foreach (var pushReceipt in pushReceiptResult.PushTicketReceipts)
{
Console.WriteLine($"TicketId & Delivery Status: {pushReceipt.Key} {pushReceipt.Value.DeliveryStatus} {pushReceipt.Value.DeliveryMessage}");
}
This is my first C# application that I’m building and I have searched the error but with no result. Can you please help me how to solve this. I know that is something simple but I really need help because I’m stuck.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to declare both a namespace and a class like this, and then put it in a function.
Every piece of code in c# needs to be declared inside a type (that could be a class, or a struct or an interface etc.
All types in turn are declared inside interfaces, which are logical groupings of types and other interfaces.
All (or most of it) of your code modeling behavior, should then be declared inside functions.
using MobileServices.Client;
using MobileServices.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace mynamespace {
public class ExpoClient {
public void DoPushSend() {
var expoSDKClient = new PushApiClient();
var pushTicketReq = new PushTicketRequest()
{
PushTo = new List<string>() { "..." },
PushBadgeCount = 7,
PushBody = ""
};
var result = expoSDKClient.PushSendAsync(pushTicketReq).GetAwaiter().GetResult();
if (result?.PushTicketErrors?.Count() > 0)
{
foreach (var error in result.PushTicketErrors)
{
Console.WriteLine($"Error: {error.ErrorCode} - {error.ErrorMessage}");
}
}
var pushReceiptResult = expoSDKClient.PushGetReceiptsAsync(pushReceiptReq).GetAwaiter().GetResult();
if (pushReceiptResult?.ErrorInformations?.Count() > 0) {
foreach (var error in result.ErrorInformations)
{
Console.WriteLine($"Error: {error.ErrorCode} - {error.ErrorMessage}");
}
}
foreach (var pushReceipt in pushReceiptResult.PushTicketReceipts)
{
Console.WriteLine($"TicketId & Delivery Status: {pushReceipt.Key} {pushReceipt.Value.DeliveryStatus} {pushReceipt.Value.DeliveryMessage}");
}
}
}
}
Be careful, the code in the repository is a snippet of the whole, possibly needing to be broken down to more functions. My fix only intends to make your code compile.
Method 2
It looks like the code sample you’re using is written in C# 9, which is currently in preview. This version introduces a new feature called top-level statements, which allows you to write code without enclosing it in a method of a class. You’re probably using an earlier version, which expects the entry point to be in a static method named Main.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0