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 3 years ago.
Improve this question
I'm trying to clear a static list from my "playerStats" script which I use to access static variables globally. Adding elements to the list works just fine (with playerStats.myList.Add(levelNumber)), but when I try to use myList.Clear I get this error:
"Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement".
Here is the relevant code: First the list definition inside the playerStats class:
{
public static List<int> myList = new List<int>();
}
And this is then on another script referencing the playerStats:
public void goBack()
{
playerStats.myList.Clear;
SceneManager.LoadScene(0);
}
Why can't I clear the list like this?
This error essentially says that you are taking a reference to a method, but you are not assigning it to anything that can reference it. The compiler doesn't know you are actually trying to call this method.
You need to add parentheses, as in
playerStats.myList.Clear();
to call the method.
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 2 years ago.
Improve this question
The thing is I need to pass a random variable to optional parameter. Anyone? :)
Something like this:
static void Creature(string optionalParam = randomVariable) {}
Optional parameters are compile time constants, so you can't have a random (runtime generated) value as an optional parameter value.
What you could do, as #madreflection eludes to, is create 2 overloaded methods: one that will accept the randomValue you pass it and second one without that parameter that generates a Random number and then calls the first overload, passing that random value along. Make sense?
You can only do this with overloads
class Foo
{
static Random rng = new Random();
static string RandomString()=> $"A{rng.Next(0,1000)}";
static void Creature() => Creature(RandomString())
static void Creature(string argument) {}
}
You can do the below with [optional] keyword.
by default optionalParam value will be Null if you do not pass anything else it will hold the passing value.
I hope it will clear about optional parameter.
Reference: https://www.geeksforgeeks.org/different-ways-to-make-method-parameter-optional-in-c-sharp/
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 trying to call a method (BeginCommand) from another class. The method is using an interface(IAudioRecorder) and has a parameter (recorder). My class is RecorderViewModel.
public void BeginCommand(IAudioRecorder recorder)
{
beginRecordingCommand = new RelayCommand(BeginRecording, () => recorder.RecordingState == RecordingState.Stopped || recorder.RecordingState == RecordingState.Monitoring);
}
Over in my other class here is how I'm trying to call it:
var audiorecorder = new AudioRecorder();
var recordviewmodel = new RecorderViewModel(audiorecorder);
recordviewmodel.BeginCommand();
If I remove the interface and parameter (IAudioRecorder recorder) from the BeginCommand method it will work, but how do I call it with the interface/parameter. It's telling me there is no argument given that corresponds to the required parameter... I'm not sure what parameters to use.
It's hard to tell (with your description) what's going on here, however I suspect you may just be able to do the following:
recordviewmodel.BeginCommand(audiorecorder);
Assuming audiorecorder implements the IAudioRecorder Interface.
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 7 years ago.
Improve this question
Every time I'd put in static it would keep replacing it with ContextStaticAttribute.
I need 10 rep to post images, but here's the link: http://i.imgur.com/jBOOF3s.png)
I also do not want to have to press the right arrow to put in a local variable!
I just figured out how you did this by typing static inside a method.
Variables in a method cannot be static, only class level elements can.
Simply declare those variables inside the class, not a method.
Example:
namespace ConsoleApplication2
{
class Program
{
static string username; // Correct
private static void Main()
{
static // Incorrect
}
}
}
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 7 years ago.
Improve this question
I'm working on a talents/skills tree system for my game made with Unity and I've got a class for my 45 talents buttons that looks like this:
public class SaveTalentClass {
public int talId;
public int talCurRank;
public int talMaxRank;
public SaveTalentClass(int id, int cRank, int mRank)
{
talId = id;
talCurRank = cRank;
talMaxRank = mRank;
}
}
I created 10 "Talent Lists" so the player can save different talents and I stored these 10 Lists in another List for easier access. So I've created a 2D list like that:
public List<List<SaveTalentClass>> containerList = new List<List<SaveTalentClass>>();
And added the 10 "talent Lists" into it but now I'm stuck trying to access/write in this 2D List.
I've tried a test like:
containerList[0][0].Add (new SaveTalentClass(0,1,2));
but got an error: SaveTalentClass' does not contain a definition forAdd' and no extension method Add' of typeSaveTalentClass' could be found (are you missing a using directive or an assembly reference?)
I'm pretty sure there's an easy fix for that but I couldn't figure out how to do it !
Thanks for helping me :)
Giving two indexes, you're already inside the second list. Either, if you want to add a new element to one of the lists inside the outer list, use
containerList[0].Add (new SaveTalentClass(0,1,2));
Or if you want to modify an existing one:
containerList[0][0] = new SaveTalentClass(0,1,2);
Since you have the containerList, you can add an object of the type on it, in that case is a List of Lists. So, add a new list of SaveTalentClass and after you can access it by 0 index to add an object of SaveTalentClass object, for sample:
// add the first list in the containerList
containerList.Add(new List<SaveTalentClass>());
// add an item in the first list of containerList
containerList[0].Add(new SaveTalentClass(0,1,2));
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 get two errors and I have no clue where the error is. Can anyone please help me resolve this issue?
; expected
Error Implicitly-typed local variables must be
initialized ComputerPlayer.cs 103
Tried searching for ";", but I don't see nothing in the code that is missing.
public override bool CheckForHit(Cell guessedCell) // Line 101
{ // Line 102
var wasHit base.CheckForHit(guessedCell); // Line 103
} // Line 104
Both errors are related. Watch closely the line 103:
var wasHit base.CheckForHit(guessedCell);
What was your intention? I imagine you were assigning the result of CheckForHit to wasHit, but there is no = sign anywhere.
Also, as noted by Mathew, there is no return in this method, so it won't compile even with = added to it.
You may rewrite it as:
public override bool CheckForHit(Cell guessedCell)
{
var wasHit = base.CheckForHit(guessedCell);
// Additional code goes here.
return wasHit;
}
If there is no additional code, consider simply removing the method, since all it does is to call the base method it overrides.