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);
Related
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
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!");
}
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
};
}
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;
}
}
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
I'm already aware that there are many other similar questions regarding this topic, however looking at the answers and adapting my code in relation to those has proved unsuccessful.
The code below is part of my Artist class, where I use CompareTo to compare between the artist name and the artist name (obj) passed in..
class Artists : IComparable
{
private string artistName;
private string artistMembers;
public int CompareTo(Object obj)
{
Artists otherArtist = (Artists)obj;
return artistName.CompareTo(otherArtist.ArtistName);
}
public Artists(string artist, string members){
ArtistName = artist;
Members = members;
}
public string ArtistName
{
set { artistName = value; }
get { return artistName; }
}
public string Members
{
set { artistMembers = value; }
get { return artistMembers; }
}
}
I really want to avoid making the variables public, which is a solution offered elsewhere, so I was wondering what I need to do to sort this problem out, and what I am doing wrong so I can learn from mistakes.
Thanks in advance.
EDIT 2
Closed VS and recompiled, and suddenly worked. Sorry for time wasting.
I'm assuming from your error that CompareTo is not public in your real code. Implicit interface implementations must be public.
You could implement the interface explicitly, and then clients would have to cast to IComparable to see the method:
int IComparable.CompareTo(Object obj) // will be private unless explicitly using the interface
{
Artists otherArtist = (Artists)obj;
return artistName.CompareTo(otherArtist.ArtistName);
}
Artists a1 = new Artists("Beatles", "Paul, Ringo");
Artists a2 = new Artists("U2", "Bono");
// this will fail:
//int i = a1.CompareTo(a2);
// this will work:
int i = ((IComparable)a1).CompareTo(a2);
However note that your class is internal by default, so the class is not even public.