Encapsulationg strings in an enum c# [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 2 years ago.
Improve this question
I'm new to C# and i'm trying to encapsulate certain states in an enum and trying to add them to a list but I am getting an error missing ;
Here is what I have coded out:
namespace abc.Models
{
public enum GroupState
{
FINISHED,
SKIPPED,
ERROR
}
public static class GroupStates
{
public static List<GroupState> TerminalStates = new List<GroupState>{
GroupState.FINISHED, GroupState.SKIPPED, GroupState.ERROR
}
}
}
I intend to use these states in my controller file. What am I doing wrong here?

You are missing a semicolon ; after the statement
public static List<GroupState> TerminalStates = new List<GroupState>{
GroupState.FINISHED, GroupState.SKIPPED, GroupState.ERROR
}
and "ERROR" is not in your enum "GroupState" it is ERROR3. Both changes need to be added:
Code snippet will be like:
public static class GroupStates
{
public static List<GroupState> TerminalStates = new List<GroupState>{
GroupState.FINISHED, GroupState.SKIPPED, GroupState.ERROR3
};
}

Related

Having trouble with an Error CS1031: Type 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 1 year ago.
Improve this question
Having trouble. Can't find where I messed up. If anyone can spot the issue I'd appreciate it greatly. The error is: Error CS1031: Type expected
I've tried running the Unity Debug thing on the script with no luck. I just genuinely don't see an issue.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AiStateMachine :
{
public AiState[] states;
public AiAgent agent;
public AiStateId currentState;
public AiStateMachine(AiAgent agent)
{
this.agent = agent;
int numStates = System.Enum.Getnames(typeof(AiStateId)).Length;
states = new AiState[numStates];
}
public void RegisterState(AiState state)
{
int index = (int)state.GetId();
states[index] = state;
}
public AIStateMachine GetState(AiStateId stateId)
{
int index = (int)stateId;
return states[index];
}
public void Update()
{
GetState(currentState)?.Update(agent);
}
public void ChangeState(AiStateId newState)
{
GetState(currentState)?.Exit(agent);
currentState = newState;
GetState(currentState)?.Enter(agent);
}
}
You have a dangling colon (:) after the class name. Since you aren't inheriting any class, you shouldn't have the colon there:
public class AiStateMachine
{
// ":" removed here ----^
While it isn't necessarily helpful in this particular case I want to mention that if you google the error code there is usually some info from Microsoft to help some. Here's the one for this error:
https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1031
In this case though it looks like you have a class that is expecting to derive from something by having a colon at the end of the class declaration without any info after it:
public class AiStateMachine :
Try removing the colon or adding whatever the class should inherit from. This page also has some great info to brush up on inheritance if needed:
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/inheritance

"The modifier 'public' is not valid for this item" [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
Here is my code
I tried removing the public but it just shows more errors
It shows The modifier public/private is not valid for this item
public string GetPlayerSide();
{
return "?";
}
public void EndTurn();
{
Debug.Log("EndTurn is not implemented!");
}
}
You have to remove ';'
Incorrect version:
public void EndTurn();
Correct version:
public void EndTurn()
Complete code:
public string GetPlayerSide()
{
return "?";
}
public void EndTurn()
{
Debug.Log("EndTurn is not implemented!");
}

C# : Error CS9010 "Primary constructor body is not allowed" [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 4 years ago.
Improve this question
class staff:schoolMember
{
private int _Salary=60000;
public int getSalary();
{
return _Salary;
}
public void setSalary(int e)
{
_Salary=e;
}
}
This is part of my C# code, and an error occurs at line 2 and I'm not sure what this error means, and I don't see the error in my code. Please help! Thankyou!
remove ; after getSalary()
and make _Salary type consistent with SetSalary() parameter type
class staff : schoolMember
{
private int _Salary = 60000;
public int GetSalary()
{
return _Salary;
}
public void SetSalary(int e)
{
_Salary = e;
}
}

Get Property Name by lambda within generic class [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
This is my generic Container class:
public class Container<T> : IContainer<T> where T : BaseModel, new()
{
private string include;
public Container()
{
}
public Container<T> Include(Expression<Func<T>> property)
{
include = GetMemberName(property);
return this;
}
}
Now I want to set the include value like this:
var container = new Container<TestClass>();
// doesn't work
container.Include(x => x.SomeProperty);
// also doesn't work
container.Include(() => TestClass.SomeProperty);
And as result the include should habe the value SomeValue. I also tried a parameterless Function, in the latter case VS says it's missing an object reference for the non-static property.
I got the GetMemberName from this thread: [Retrieving Property name from lambda expression
Change your func definition:
public Container<T> Include(Expression<Func<T, object>> property)
{
include = GetMemberName(property);
return this;
}
This is the correct usage:
container.Include(x => x.SomeProperty);

Correct Description of "class" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
public class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.WriteLine(test.GetType()); // -> it´s Test, of course
int a = 5;
Console.ReadLine();
}
}
public class Test
{
public string Name;
}
In the above case
the Type of "test" is Test, right?
And in this case the Type of "a" is int, named primitive data type,
public is an access modifier
So my Question now is, what is class then? A keyword? A primitive Datatype? Neither, Nor?
Please, don't explain my, what a class is doing or what's the difference between class or object, I know a class is like a construction plan.
Class is a keyword that signifies to the C# compiler that a class is being declared. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

Categories

Resources