Get Array values using a class in c# - c#

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.

Related

How can I use Enum in switch statement

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

Wrong override called?

I'm new to programming and this class-things are doing my head in.
Here is my code (type is based on userinput)
public static Account CreateAccount(int type)
{
switch (type)
{
case 1:
SaveAcc savings = new SaveAcc();
break;
default:
Console.WriteLine("No such choice");
break;
}
return new Account();
}
These are my classes:
class Account
{
protected int balance;
protected int accountnr = 1;
protected bool credit;
public Account()
{
newNr++;
accountnr = newNr;
}
public override string ToString()
{
return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C");
}
}
class SaveAcc: Account
{
public int rate;
public SaveAcc()
{
credit = true;
rate = 0.03;
}
public override string ToString()
{
return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C") + credit.ToString();
}
}
When I create a SavAcc object the "wrong" override is being called. My goal is to display all the information given by the override ToString method located in the SavAcc inherited class. Am I missing something obvious?
Change this:
switch (type)
{
case 1:
SaveAcc savings = new SaveAcc();
break;
default:
Console.WriteLine("No such choice");
break;
}
return new Account();
To this:
switch (type)
{
case 1:
return new SaveAcc();
default:
Console.WriteLine("No such choice");
return new Account();
}

how to take a value from an object and turn it to string?

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;
}
}

Use Class property value for a switch statement

am using generated protobuf code (see http://code.google.com/p/protobuf/)
I have a generated class looks like this :
public class Fruits{
private int _ID_BANANA = (int)1;
private int _ID_APPLE = (int)2;
public int ID_BANANA
{
get { return _ID_BANANA; }
set { _ID_BANANA = value; }
}
public int ID_APPLE
{
get { return _ID_APPLE; }
set { _ID_APPLE = value; }
}
}
Then they are constant values but I can't use then as such in my code.
For example I want to do a mapper like this :
public static Color GetColor(int idFruit) {
switch (idFruit)
{
case new Fruits().ID_BANANA:
return Color.Yellow;
case new Fruits().ID_APPLE:
return Color.Green;
default:
return Color.White;
}
}
I have the error : a constant value is expected.
I thougth about creating an enum, but seems to be the wrong way, tryed something like :
public const int AppleId = new Fruits().ID_APPLE;
Not working either...
Someone have an idea?
Why not to use if else if statements here?
var fruits = new Fruits();
if (idFruit == fruits._ID_BANANA)
return Color.Yellow;
else if (idFruit == fruits._ID_APPLE)
return Color.Green;
else
return Color.White;
Or dictionary:
this.fruitsColorMap = new Dictionary<int, Color>
{
{ fruits._ID_BANANA, Color },
{ fruits._ID_APPLE, Green }
};
And then:
public static Color GetColor(int idFruit) {
if (this.fruitsColorMap.ContainsKey(idFruit) {
return this.fruitsColorMap[idFruit];
}
return Color.White;
}
The case values in a switch statement have to be constants - that's part of the switch statement's definition:
switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
}
as per the switch (C#) documentation from Microsoft.
you will notice the constant-expression bit. That means that it can be determined at compile time. So calls to functions (and a reference to a property property is really a function call in disguise) are not allowed here.

Classes with String Switches

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;
}
}

Categories