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/
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 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.
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 8 years ago.
Improve this question
Below is method declaration has been used in one of our interfaces to read entity from Azure table storage.
QueryEntity method is not having any variable associated with Func<IQueryable<TElement> parameter.
How does it work?
bool QueryEntity(string tableName, string partitionKey, out List<TElement> retrievedEntity,
out string errorMessage,Expression<Func<TElement, bool>> filter = null,
Func<IQueryable<TElement>,
IOrderedQueryable<TElement>> orderBy = null);
It is the type given to the orderBy argument. It is split across two lines.. hence your confusion. Put it on one line and it becomes:
Func<IQueryable<TElement>, IOrderedQueryable<TElement>> orderBy = null
A Func<T1, T2> delegate. It also has a default value of null.. so if you don't supply it when calling the function.. it is inferred to be null.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When writing a method that takes a string and populates a poco based on it, is it better to have a static Parse(string s) method like Int32.Parse() or overload the constructor so it takes a string?
I prefer the constructor version, but including both is easy, since the constructor can just call Parse. This is the pattern followed by the Guid struct (and likely others as well.)
I should add that if you're not dealing with a struct, then the static method should probably be referring to the constructor (or even a separate method that both can call) since you can't assign to this in a class constructor.
EDIT: As TrueWill points out, if you do include Parse, you should include TryParse as well. Incidentally, Guid is once again instructive: the Parse method actually uses TryParse, and just throws an exception if TryParse returns false.
If the method might fail due to an invalid string, I'd lean towards Parse and include TryParse as per the TryParse pattern.
I would recommend using .Parse(string s) if its a simple object, if the object stores more then 1-2 values you should use the constructor, or in other words, dont parse if the return value will be an instance with memebers unaffected by the parse value.