Getting Error: return statement is missing in switch statement - c#-3.0

I thought my switch case was written correctly but I got an error:
return statement is missing in the last bracket.
Please find below my code:
public string getUrl(string code )
{
switch (code)
{
case "1":
return "#";
case "2":
return "#";
case "3":
return "#";
case "4":
return "#";
case "5":
return "#";
case "6":
return "#";
case "7":
return "#";
}
}
Thanks in advance

What you are missing is a default case. What string should this function return if none of the cases you mentioned occur.
e.g. I pass "unknown" to this method getUrl.
So just add a default case and things should work fine.
The code should look something like
public string getUrl(string code )
{
switch (code)
{
case "1":
return "#";
case "2":
return "#";
case "3":
return "#";
case "4":
return "#";
case "5":
return "#";
case "6":
return "#";
case "7":
return "#";
default:
return "Unknown case occured!";
}
}

Related

Flutter error related with body_might_complete_normally

Hi I'm practicing flutter.
It's from some open source below. It makes error about body_might_complete_normally.
How can I work around it?
Widget _buildBody(dynamic index) {
switch(index) {
case 0:
return MainPage();
case 1:
return CatalogPage();
case 2:
return NotificationPage();
case 3:
return MorePage();
}
}
If the above case doesn't satisfy, widget will return nothing. You can include default on switch.
Widget _buildBody(int index) {
switch (index) {
case 0:
return MainPage();
case 1:
return CatalogPage();
case 2:
return NotificationPage();
case 3:
return MorePage();
default: //this
return Text("default");
}
// return Text("aa"); or this
}
More about switch-and-case

DART : I have a switch case of enums, which are buttons, but all cases are executing only once. It wont work the second time i click the same button

Below is the switch case code, the main problem I have is, the code is fine for the first time that is appropriate output is rendered, but when I click the button (enum for example Scholarships) the code wont execute
PopupMenuButton(
onSelected: (Category selectedOffer) {
switch (selectedOffer) {
case Category.Scholarships:
{
offersData.showScholarshipsOnly();
}
break;
case Category.Investing:
{
offersData.showInvestingOnly();
}
break;
case Category.Courses:
{
offersData.showCoursesOnly();
}
break;
case Category.Discounts:
{
offersData.showDiscountsOnly();
}
break;
case Category.Finance:
{
offersData.showFinanceOnly();
}
break;
case Category.All:
{
offersData.showAll();
} break;
}
These are the functions which are linked to the above conditions
List<Offers> get offerList {
if (_showScholarships) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Scholarships')).toList();
}
else if (_showCourses) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Courses')).toList();
}
else if (_showInvesting) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Investing')).toList();
}
else if (_showFinance) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Finance')).toList();
}
else if (_showDiscounts) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains('Discounts')).toList();
}
else if (_showAll) {
return _offerList.where((selOffer) =>
selOffer.category.name.contains(
'Investing,Finance,Discounts,Insurance,Courses,Scholarships'))
.toList();
}
return _offerList;
}

How can I convert english numbers into another language numbers (I am using Bengali language) in Flutter?

I am getting english numbers (e.g. 123) as integer, now I want to show it in another language form (e.g. ১২৩).
I used a function and it also worked. But, it doesn't work in same stateful class more than once.
It worked for the first Text widget but not for the second one
Column(
children<Widget>:[
Text(
"${convertNumber(123)}"
),
Text(
"${convertNumber(5630)}"
)
]
)
String convertNumber(int eng){
String bengali = '';
for(int i = 0; i < eng.toString().length; i ++){
setState(() {
switch(eng.toString()[i]){
case '1':
bengali = bengali + '১';
break;
case '2':
bengali = bengali + '২';
break;
case '3':
bengali = bengali + '৩';
break;
case '4':
bengali = bengali + '৪';
break;
case '5':
bengali = bengali + '৫';
break;
case '6':
bengali = bengali + '৬';
break;
case '7':
bengali = bengali + '৭';
break;
case '8':
bengali = bengali + '৮';
break;
case '9':
bengali = bengali + '৯';
break;
default:
bengali = bengali + '0';
}
});
}
return bengali;
}
While I am using it for other numbers in the same stateful class, it doesn't work. And error was :
setState() or markNeedsBuild() called during build
This LowerHalf widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. The widget on which setState() or markNeedsBuild() was called was: LowerHalf
Do not call setState inside your convert function, just use your function to get a value.
Widgets rebuild after you call setState function. In your case, build calls setState that triggers build again.
Create new function and return string after converting it.
Below function is for converting number from English to Hindi or Gujarati based on users selected language for application.
translateMobileNumber(String mobileNumber) {
var mobileNumberTrim = mobileNumber.toString().trim();
var translatedNumber = '';
var currentLan = 'gu';
switch (Get.locale.toString().substring(0, 2)) {
case 'gu':
currentLan = 'gu';
break;
case 'hi':
currentLan = 'hi';
break;
case 'en':
currentLan = 'en';
break;
default:
currentLan = 'en';
break;
}
if (currentLan == 'en') {
return mobileNumberTrim;
}
for (int i = 0; i < mobileNumberTrim.length; i++) {
switch (mobileNumberTrim[i]) {
case '0':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૦' : '$translatedNumber૦';
break;
case '1':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૧' : '$translatedNumber१';
break;
case '2':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૨' : '$translatedNumber२';
break;
case '3':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૩' : '$translatedNumber३';
break;
case '4':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૪' : '$translatedNumber४';
break;
case '5':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૫' : '$translatedNumber५';
break;
case '6':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૬' : '$translatedNumber६';
break;
case '7':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૭' : '$translatedNumber७';
break;
case '8':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૮' : '$translatedNumber८';
break;
case '9':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૯' : '$translatedNumber९';
break;
}
}
return translatedNumber;
}

Cancel Or Exit from PromptDialog.Choice Flow in Microsoft Bot Framework

i have a Prompt with 4 options, the last option is user can quit the Prompt,
i want to implement some code so that the bot exit the Prompt
Image
PromptDialog.Choice(context, this.OnOptionSelected, new List<string>() { FlightsOption, HotelsOption, TrainOption, GobackOption }, "Sure..! Tell me what booking would like to make..?", "Not a valid option", 3);
in above image i had implemented quit option on which if user selects quit it goes to Switch case of quit.
i had also tried context.quit but it throws error
private async Task OnOptionSelected(IDialogContext context, IAwaitable<string> result)
{
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
//want some code here to quit the form
break;
}
}
First of all this is not a Form Flow. This is prompt.
Now You can do something like, either you exit the dialog from the stack like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
context.Done<object>(null);
break;
}
}
Or,
You can tell something and then wait for other message in the same dialog like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
await context.PostAsync("Ok, you came back. Now tell something new.");
context.Wait(MessageReceivedAsync);
break;
}
}
And the next message will go here
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
context.Wait(MessageReceivedAsync);
}

Replace number with name of months

I have sort of data which contains number.
1,2,3,4,5,6,7,8,9,10,11,12, to represent months.
I want to change it so when I preview the report it shows
January
February
March
April
May
June
July
August
November
December
I've tried using replace()
.replace("1", "January")
When I run it, it doesn't work
Any suggestion?
You can use Text Field Expression, Replace $V{Month} with your field Name
<textfieldexpression class="java.lang.String">
(
$V{Month}.equals("1") ? "January" :
$V{Month}.equals("2") ? "February" :
$V{Month}.equals("3") ? "March" :
$V{Month}.equals("4") ? "April" :
$V{Month}.equals("5") ? "May" :
$V{Month}.equals("6") ? "June" :
$V{Month}.equals("7") ? "July" :
$V{Month}.equals("8") ? "August" :
$V{Month}.equals("9") ? "September" :
$V{Month}.equals("10") ? "October" :
$V{Month}.equals("11") ? "November" : "December"
)
</textfieldexpression>
This should solve your problem. What I have done here is split the source string into an ArrayList of String and then used switch case to concatenate the names of corresponding months to a new string that is shown as the output.
import java.util.*;
class abc
{
public static void main (String[] args)
{
String str = "1,2,3,4,5,6,7,8,9,10,11,12,";
String res = "";
List<String> list = new ArrayList<String>(Arrays.asList(str.split(",")));
for (String s : list)
{
switch (s)
{
case "1":
res += "January\n";
break;
case "2":
res += "February\n";
break;
case "3":
res += "March\n";
break;
case "4":
res += "April\n";
break;
case "5":
res += "May\n";
break;
case "6":
res += "June\n";
break;
case "7":
res += "July\n";
break;
case "8":
res += "August\n";
break;
case "9":
res += "September\n";
break;
case "10":
res += "October\n";
break;
case "11":
res += "November\n";
break;
case "12":
res += "December\n";
break;
default:
System.err.println("Invalid output");
}
}
System.out.println(res);
}
}