"The modifier 'public' is not valid for this item" [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
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!");
}

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

Encapsulationg strings in an enum 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
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
};
}

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

Why i can not input set in my code? [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 8 years ago.
Improve this question
When writing out a property in visual studio, the IDE autocompletes the wrong text and interrupts my flow.
class Person
{
private int age;
public int Age;
{
AppDomainSetup //
}
AppDomainSetup is given when I've typed set. Why is the IDE confused?
You should remove ; after "Age".
It is:
internal class Person
{
private int age;
public int Age { set; get; }
}
Try
internal class Person
{
private int age;
public int Age { set; get; }
}
The more complete answer is that you've ended the field Age and the IDE is reading what you've written and is expecting a Type (among a few other contextually based options, of which set is NOT one). You've only written set, and the best match for set for a type in the given context is AppDomainSetup because it is the first Type in whose name the substring set is found. The autocomplete behavior is to input the selected suggestion when you press space.
To correct this (or to stop confusing intellisense), don't put a colon after the property name.
public int Age { set // and continue typing

Categories

Resources