class AddItemOption
{
//Fields that contain class data
string input = Console.ReadLine();
//Methods that define class functionality
public string myFunction(string Value)
{
switch (input)
{
case "Umbrella":
Console.WriteLine("Umbrella is selected");
break;
case "Rain Coat":
Console.WriteLine("Raincoat is selected");
break;
case "Boots":
Console.WriteLine("Boots is selected");
break;
case "Hood":
Console.WriteLine("Hood is selected");
break;
default:
Console.WriteLine("Input not reconized, please choose another item");
break;
}
}
I get the error "Not all code paths return a value". It is from myFunction(string Value). I'm not sure how to return this, or what to put in the parameter to get it to work. Do I need something below it too? I am new to classes. Please tell me if I'm doing this all wrong or if that is where the switch statement should go!
public AddItemOption(string input)
{
}
FROM Shyju I changed it to:
class AddItemOptionOne
{
//Fields that contain class data
string input = Console.ReadLine();
//Methods that define class functionality
public string myFunction(string Value)
{
switch (input)
{
case "Key Chain":
return "Key Chain is selected";
break;
case "Leather Armor":
return "Leather Armor is selected";
break;
case "Boots":
return "Boots is selected";
break;
case "Sword":
return "Sword is selected";
break;
default:
return "Input not reconized, please choose another item";
break;
}
}
However, it does not recognize the breaks. "Unreachable Code Detected"
Your method has a return type of string. That means your method should always return some string. But you are not returning a string. instead you are Writing it to the console using WriteLine method.
So Change
Console.WriteLine("Umbrella is selected");
to
return "Umbrella is selected";
OR
You can change your method's return type to void.
public void myFunction(string Value)
{
//your current fancy stuff here
Console.WriteLine("My method dont have a return type");
}
EDIT : AS per the question edit.
return statement will take the control out from your method. that means the next line (break) won't be executed. So remove the break statement.
case "Key Chain":
return "Key Chain is selected";
Well... You don't return anything from your function, so change it to
public void myFunction(string Value)
Try this:
class AddItemOptionOne
{
//Methods that define class functionality
public string myFunction(string input)
{
string result = string.Empty;
switch (input)
{
case "Key Chain":
result = "Key Chain is selected";
break;
case "Leather Armor":
result = "Leather Armor is selected";
break;
case "Boots":
result = "Boots is selected";
break;
case "Sword":
result = "Sword is selected";
break;
default:
result = "Input not reconized, please choose another item";
break;
}
return result;
}
}
Related
What I am trying to do is detecting the type of "item" and using it inside of a switch statement and based on that, choosing correct equipment slot.
My code:
public EquipmentSlot WeaponSlot;
public EquipmentSlot ChestplateSlot;
public EquipmentSlot LeggingsSlot;
public EquipmentSlot HelmetSlot;
public void EquipItem(Item item)
{
EquipmentSlot chosenSlot = null;
switch(item.type)
{
case item.type.weapon:
chosenSlot = WeaponSlot;
break;
case item.type.chestplate:
chosenSlot = ChestplateSlot;
break;
case item.type.leggings:
chosenSlot = LeggingsSlot;
break;
case item.type.helmet:
chosenSlot = HelmetSlot;
break;
}
chosenSlot.EquippedItem = item;
}
Item class:
public enum Type
{
weapon,
chestplate,
leggings,
helmet
}
public Type type;
This error is showing up:
error CS0176: Member 'Item.Type.helmet' cannot be accessed with an instance reference; qualify it with a type name instead
(four times for each case in switch)
Instead of case item.type.weapon: it should be case Item.Type.weapon:
try this
switch (item.type)
{
case Type.weapon:
chosenSlot = WeaponSlot;
break;
case Type.chestplate:
chosenSlot = ChestplateSlot;
break;
.....
}
but it will be much more readable if you rename Type to ArmoryType for example
EDIT:
The program still does not work as it is supposed to. I tried to implement the suggested changes, which seemed to be the solution indeed. However, now any input into the programme leads to the default statement. What are we doing wrong?
NEW CODE WITH CHANGES BELOW:
public class Processor
{
public void DisplayEmployers()
{
Console.WriteLine("Select an option");
Console.WriteLine("1. Lawyer");
Console.WriteLine("2. Admin");
Console.WriteLine("3. Receptionist");
Console.ReadLine();
}
public enum Staff { Lawyer, Admin, Receptionist, UnsupportedValue }
public void ChooseTypeOfEmployer()
{
Staff s = (Staff.UnsupportedValue);
switch (s)
{
case Staff.Lawyer:
ProvideLogin();
break;
case Staff.Admin:
ProvideLogin();
break;
case Staff.Receptionist:
ProvideLogin();
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
public void ProvideLogin()
{
string username, password;
Console.WriteLine("Please provide username to access the system");
{
Console.WriteLine("Input a username: ");
username = Console.ReadLine();
Console.WriteLine("Input as password: ");
password = Console.ReadLine();
{
Because you already handled all available values.
Try the next case:
public enum Staff { Lawyer, Admin, Receptionist, UnsupportedValue }
public void ChooseTypeOfEmployer()
{
Staff s = (Staff.UnsupportedValue);
switch (s)
{
case Staff.Lawyer:
ProvideLogin();
break;
case Staff.Admin:
ProvideLogin();
break;
case Staff.Receptionist:
ProvideLogin();
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
++ you may to simplify you code
public void ChooseTypeOfEmployer()
{
Staff s = (Staff.UnsupportedValue);
switch (s)
{
case Staff.Lawyer:
case Staff.Admin:
case Staff.Receptionist:
ProvideLogin();
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
i have declared 2 string values eg: ChName1, ChName2 and 2 int values eg: Delay1, Delay2 in the settings.
i would like to implement an array in class so life will be more easier. can somebody correct my code with necessary explanations
public class GetDefaultValues
{
public string Name[int i]
{
get
{
switch (i)
{
case 0:
return Name.Properties.Settings.Default.ChName1;
case 1:
return Name.Properties.Settings.Default.ChName2;
default:
return "Not Implemented";
}
}
set
{
switch (i)
{
case 0:
{
Name.Properties.Settings.Default.ChName1 = value;
break;
}
case 1:
{
Name.Properties.Settings.Default.ChName2 = value;
break;
}
}
Name.Properties.Settings.Default.Save();
}
}
public int Value[int i]
{
get
{
switch (i)
{
case 0:
return Name.Properties.Settings.Default.Delay1;
case 1:
return Name.Properties.Settings.Default.Delay2;
default:
return 0;
}
}
set
{
switch (i)
{
case 0:
{
Name.Properties.Settings.Default.Delay1 = value;
break;
}
case 1:
{
Name.Properties.Settings.Default.Delay2 = value;
break;
}
}
Name.Properties.Settings.Default.Save();
}
}
}
then in my main code i could do like this
GetDefaultValues Vlues = new GetDefaultValues();
Vlues.Name[0] = "SomeName";
string SomeString = Vlues.Name[1];
Vlues.Value[0] = 125;
int SomeInt = Vlues.Value[1];
this code is generating errors in my code.
Error 2 Bad array declarator: To declare a managed array the rank
specifier precedes the variable's identifier. To declare a fixed size
buffer field, use the fixed keyword before the field type
will be happy if i could know why??
please help!!
I would recommend not to use properties for this because properties should not have that logic. You should use methods instead.
This could be look like this for example (just added the name as a sample).
public class GetDefaultValues
{
public bool SetName(int i, string value)
{
switch (i)
{
case 0:
{
Name.Properties.Settings.Default.ChName1 = value;
break;
}
case 1:
{
Name.Properties.Settings.Default.ChName2 = value;
break;
}
default:
return false;
}
Name.Properties.Settings.Default.Save();
return true;
}
public string GetName(int i)
{
switch (i)
{
case 0:
return Name.Properties.Settings.Default.ChName1;
case 1:
return Name.Properties.Settings.Default.ChName2;
default:
return "Not Implemented";
}
}
}
This would be called like this
GetDefaultValues Vlues = new GetDefaultValues();
bool success = Vlues.SetName(0, "SomeName");
string SomeString = Vlues.Name(1);
You can make the SetName method a void method if you don't want to have the info if it worked or not.
public UcCard CardValue()
{
UcCard current = pack[index];
UcCard.Type cardvalue = 0;
current.Value = cardvalue;
edit:return current.Value.ToString(cardvalue);
//////return UcCard.ToString(cardvalue);
}
edit:now it says that it cannot convert the "***.UcCard.Type" to string
im getting the error "No overload for method 'ToString' takes 1 arguments"
what im trying to do is take a value from UcCard and then print it, now i know that UcCard is an object, but im not sure on how to take the value and transfer it to string
im new to coding so be gentle T_T i tried searching for an answer before posting but i just couldnt find an answer that fitted my need
also, this is where im trying to take the value from:
public int Volume
{
get
{
switch (lblValue.Text)
{
case "2": return 2;
case "3": return 3;
case "4": return 4;
case "5": return 5;
case "6": return 6;
case "7": return 7;
case "8": return 8;
case "9": return 9;
case "10":
case "K":
case "Q":
case "J": return 10;
case "A": return 1;
}
return 0;
}
}
Every class or struct in C# implicitly inherits the Object class.
This class contains the ToString method which is called when you prints out strings .
And you should customize it for your use AKA override it because the program will not guess what Attributes it should print not mention in what format.
(e.g from MSDN )
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return "Person: " + Name + " " + Age;
}
}
I have the following protected method in my base class:
protected bool ExecuteInteraction(string identifier,
Action<object> interactionFinished, params object[] args)
{
Contract.Requires(!string.IsNullOrEmpty(identifier));
Contract.Requires(interactionFinished != null);
bool result = false;
if (!string.IsNullOrEmpty(identifier) && (interactionFinished != null))
{
// Find the matching interaction behavior.
IInteractionBehavior interactionBehavior =
InteractionBehaviorCollection.Instance.Get(identifier);
if (interactionBehavior != null)
{
try
{
interactionBehavior.Execute(interactionFinished, args);
result = true;
}
catch (Exception)
{
}
}
else
{
Debug.WriteLine(string.Format(
"InteractionBehavior with identifier [{0}] is unknown",
identifier));
}
}
return result;
}
It's basically from this article: http://www.codeproject.com/Articles/708346/Dialogs-the-MVVM-Way ...
This method is used in my derived class like that:
protected override void Delete()
{
try
{
if (PrimaryModel.Id != 0 && PrimaryModel.AddressId != 0)
{
ExecuteInteraction("DeleteConfirmation", (result) =>
{
MessageBoxResult messageBoxResult = (MessageBoxResult)result;
if (messageBoxResult == MessageBoxResult.Yes)
{
//Do something ...
}
});
}
}
catch (Exception exc)
{
Message = exc.Message;
}
}
This doesn't work in my unit test because I don't simulate that messageBoxResult is MessageBoxResult.Yes. This is the reason why I'd like my protected method to always returning true in the unit test. How can I do that (with moq)?
You just need to interface your MessageBox actions so that you can provide a MockMessageBox when unit testing that always returns MessageBoxResult.Yes. So, in your code, don't reference any MessageBoxes directly. Create a WindowManager or MessageBoxService class that you call instead, kind of like a proxy message box.
For example, try something like this:
public string ShowMessageBox(string message, string title, string buttons, string icon)
{
MessageBoxButton messageBoxButtons;
switch (buttons.ToLower())
{
case "ok": messageBoxButtons = MessageBoxButton.OK; break;
case "okcancel": messageBoxButtons = MessageBoxButton.OKCancel; break;
case "yesno": messageBoxButtons = MessageBoxButton.YesNo; break;
case "yesnocancel": messageBoxButtons = MessageBoxButton.YesNoCancel; break;
default: messageBoxButtons = MessageBoxButton.OKCancel; break;
}
MessageBoxImage messageBoxImage;
switch (icon.ToLower())
{
case "asterisk": messageBoxImage = MessageBoxImage.Asterisk; break;
case "error": messageBoxImage = MessageBoxImage.Error; break;
case "exclamation": messageBoxImage = MessageBoxImage.Exclamation; break;
case "hand": messageBoxImage = MessageBoxImage.Hand; break;
case "information": messageBoxImage = MessageBoxImage.Information; break;
case "none": messageBoxImage = MessageBoxImage.None; break;
case "question": messageBoxImage = MessageBoxImage.Question; break;
case "stop": messageBoxImage = MessageBoxImage.Stop; break;
case "warning": messageBoxImage = MessageBoxImage.Warning; break;
default: messageBoxImage = MessageBoxImage.Stop; break;
}
return MessageBox.Show(message, title, messageBoxButtons, messageBoxImage).ToString();
}
This would be called like this:
string result = WindowManager.ShowMessageBox(message, title, "OkCancel", "Question");
Note how there are no direct relations to any UI dlls... all properties are string based. Now you need to interface the class that this method is in (WindowManager). So now we have an IWindowManager that forces us to write a ShowMessageBox method in all implementing classes.
Now we implement this interface in a MockWindowManager class:
public string ShowMessageBox(string message, string title, string buttons, string icon)
{
switch (buttons)
{
case "Ok":
case "OkCancel": return "OK";
case "YesNo":
case "YesNoCancel": return "Yes";
default: return "OK";
}
}
When running the application, we use the WindowManager class and when testing, we use the MockWindowManager class. This can be achieved in a number of ways, but the easiest is to pass an IWindowManager into the view model constructor:
public YourViewModel(IWindowManager windowManager)
{
this.windowManager = windowManager;
}
I really suggest you to follow the concept explained by #Sheridan; but if you want to go your way, here is the solution how to mock a protected method:
Mock<YourBaseClass> baseClassMock = new Mock<YourBaseClass>();
baseClassMock.Protected().Setup<bool>(
"ExecuteInteraction",
ItExpr.IsAny<string>(),
ItExpr.IsAny<Action<object>>(),
ItExpr.IsAny<object[]>())
.Returns(true);