Changing enum value with index number [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 days ago.
Improve this question
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;
public class VariableDelegate : MonoBehaviour
{
public enum Day
{
Morning,
Afternoon,
Evening
}
public Day day;
public Day AdjustDay
{
get
{
return day;
}
set
{
if (value != day)
{
day = value;
Debug.Log(day);
}
}
}
[ContextMenu("RandomDay")]
void RandomDay()
{
AdjustDay = (Day)2;
}
}
if I do AdjustDay = (Day)2; i am able to change it to evening. However, if I do AdjustDay = (Day)Random.Range(0,2); then it doesn't work. What I want to do is to random a number(since the enum has an index), then change the AdjustDay value. If the random number can start from the min value of the enum, and the max value is the length of the enum that will be great.

You can write it this way:
void RandomDay()
{
var values = Enum.GetValues(typeof(Day));
AdjustDay = values.GetValue(Random.Range(0,values.GetLength(0))));
}
With help of Enum.GetValues(typeof(Day)) you can get whole range of values.

Related

Why IEqualityComparer doesn't work as expected? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Why EqualityComparer MyEqComp doesn't work?
It should left in arr only 2 elements with equal Values - myDic["1"]and myDic["3"] with Value="456". But instead it consists of myDic["1"] and myDic["2"] but their Values are not equal.
Function GetHashCode returns the same hash code for 1st and 3rd elements!
public class Program
{
public static Dictionary<string,string> myDic = new Dictionary<string,string>();
public static void Main()
{
myDic.Add("1","456");
myDic.Add("2","567");
myDic.Add("3","456");
var arr=myDic.Distinct(new MyEqComp());
foreach (var n in arr)
{
Console.WriteLine("key="+n.Key+",value="+n.Value+"\n");
}
}
class MyEqComp:IEqualityComparer<KeyValuePair<string,string>>{
public bool Equals(KeyValuePair<string,string> pair1, KeyValuePair<string,string> pair2){
Console.WriteLine("pair1="+pair1+",pair2="+pair2);
return pair1.Value == pair2.Value;
}
public int GetHashCode(KeyValuePair<string,string> pair){
var code = pair.Value.GetHashCode();
Console.WriteLine("code="+code);
return code;
}
}
}
And the output
code=-121858068
key=1,value=456
code=437991364
key=2,value=567
code=-121858068
pair1=[1, 456],pair2=[3, 456]
Answer: Code is correct and works as expected.

C# using an IF statement to check if user enters correct DateTime date which is a birthday [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a Customer class, and I used the DatTime constructor and set the Date of Birth(bday).
In the main program I have an instance of the Customer class and would like to check if user enters the same birthday that was was originally set in the class.
public class Customer
{
public string Name;
public int PinNumber = 2321;
public double AccBalance = 250.00;
public DateTime bday = new DateTime(1875,05,22);
main program
person1.bday = Convert.ToDateTime(Console.ReadLine());
if (person1.bday == 1875,05,22)
{
Console.WriteLine("");
}
I get an error saying ''==' operator cannot be be applied to operands of type 'DateTime' and 'int'
You need to convert the three integers in your if statement to a DateTime:
if (person1.bday.equals(new DateTime(1875,05,22)) {
Console.WriteLine("");
}
When doing a comparison the data type on both end should be same
You should create a date object when doing comparison in your code
if (person1.bday == new DateTime(1875,5,22))
{
Console.WriteLine("");
}

Can't print Stack<T> C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi I have a question about my code. Everything should work without a problem, but when I try to print on the console, the stack gives me the following
input:
4
Output:
System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.Stack1 [System.UInt64] System.Collections.Generic.
Stack1 [System.UInt64]
my question is if i need to add a new library because even with int it gives me the same.
using System;
using System.Collections.Generic;
namespace ConsoleApp29
{
class Program
{
static void Main(string[] args)
{
Stack<ulong> nm = new Stack<ulong>();
ulong p = ulong.Parse(Console.ReadLine());
for(ulong i = 0; i < p; i++)
{
nm.Push(i);
}
foreach(int i in nm)
{
Console.Write(nm);
}
}
}
}
nm, which you are printing, is the entire Stack object; you want to be printing i, the current element of nm.

Why are my LINQ queries returning zero? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am new to LINQ and no matter how hard I try to get these two queries to work I always end up with zero as the result. I've tried everything to calculate the business days in a month and the total number of days. MDate is binded to a datepicker, BusinessDays to a textbox and TotalDaysInMonth to another textbox.
Can someone please help me fix these queries to show up the right values for a selected month? (e.g. September: 20 BusinessDays, 30 TotalDays)
public class LocationViewModel : INotifyPropertyChanged
{
public LocationViewModel()
{
SetBusinessDays();
SetTotalDaysInMonth();
}
public DateTime mDate = DateTime.Now;
public DateTime MDate
{
get { return mDate; }
set
{
if (value == mDate)
{
return;
}
else
{
mDate = value;
OnPropertyChanged("MDate");
SetBusinessDays();
SetTotalDaysInMonth();
}
}
}
int _businessDays;
public int BusinessDays
{
get { return _businessDays; }
set
{
_businessDays = value;
OnPropertyChanged("BusinessDays");
}
}
int _totalDaysInMonth;
public int TotalDaysInMonth
{
get { return _totalDaysInMonth; }
set
{
_totalDaysInMonth = value;
OnPropertyChanged("TotalDaysInMonth");
}
}
private void SetBusinessDays()
{
int BusinessDays = Enumerable.Range(1, DateTime.DaysInMonth(MDate.Year, MDate.Month))
.Select(day => new DateTime(MDate.Year, MDate.Month, day))
.Count(d => d.DayOfWeek != DayOfWeek.Sunday &&
d.DayOfWeek != DayOfWeek.Saturday);
}
private void SetTotalDaysInMonth()
{
int TotalDaysInMonth = Enumerable.Range(1, DateTime.DaysInMonth(MDate.Year, MDate.Month))
.Select(day => new DateTime(MDate.Year, MDate.Month, day))
.Count();
}
}
Thank you.
Inside both of your methods you are declaring a local variable rather then setting your property. In C# you don't need to specify the type of whenever accessing a property. Remove both int literals inside your methods.

EndsWith() not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to add the corresponding index of all the strings in the tres4 array, if they match the end of the input string. Yet, my list gets filled with all the indexes 1-12, as opposed to only those, matching the end of my input string. Only 1 should be added to my List in this case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Encoding
{
class Program
{
static void Main(string[] args)
{
string[] tres4 = {
"CHU",
"TEL",
"OFT",
"IVA",
"EMY",
"VNB",
"POQ",
"ERI",
"CAD",
"K-A",
"IIA",
"YLO",
"PLA"
};
string message = "CHUTEL";
List<int> digits = new List<int>();
for (int i = 0; i < tres4.Length; i++)
{
if (message.EndsWith(tres4[i]));
{
digits.Add(i);
}
}
Console.WriteLine(String.Join(", ", digits));
}
}
}
You have an extra semicolon:
if (message.EndsWith(tres4[i]));
See the semicolon at the end? Remove it and it'll work.
Next time you should give a debugger a try, it would show you the problem right away.
Remove ; from if condition
if (message.EndsWith(tres4[i]))

Categories

Resources