Can a property have a method? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can a property GridTerminalSystem have a method named GetBlockWithName?
class Program
{
static void Main(string[] args)
{
var InteriorLight = GridTerminalSystem.GetBlockWithName("Interior Light") as IMyInteriorLight;
InteriorLight.ApplyAction("OnOff_On");
}
}

var InteriorLight = GridTerminalSystem.GetBlockWithName("Interior Light") as IMyInteriorLight;
Here you are calling the static method GetBlockWithName of class GridTerminalSystem and passing in a string argument "Interior Light". Then, the result is being casted to IMyInteriorLight, and assigned to InteriorLight variable.
InteriorLight.ApplyAction("OnOff_On");
Here you are calling the method ApplyAction on the IMyInteriorLight instance that was previously assigned to InteriorLight. You are passing in a string "OnOff_On" to the method. Apparently, the method does not return anything (void), as there is no assignment.

Related

Does this public get set for a private variable make sense? [closed]

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 1 year ago.
Improve this question
I am working on a codebase a student with questionable knowledge made, here I am just confused if there is any reasonable intend there or if its just unnecessary... I personally would just use the ShowNewButton field and trash the _showNewBtn, please provide an opinion.
private _showNewBtn;
public bool ShowNewButton
{
get => _showNewBtn;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
_showNewBtn = value;
}
}
I see no reason (in the sample you provided) for the _showNewBtn.
You now have 2 markers which contain the visibility state of the button. At some point, this will cause problems if you aren't carefull.
Either remove the _showNewBtn completely:
public bool ShowNewButton
{
get => bbNew.Visibility == BarItemVisibility.Always;
set
{
bbNew.Visibility = value == false ? BarItemVisibility.Never : BarItemVisibility.Always;
}
}
Or justify the existance of _showNewBtn which makes my remark null and void.

How to convert Span<byte> to ASCII string? [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 3 years ago.
Improve this question
I can convert byte array to ASCII string in C# by Encoding.ASCII.Getstring() method.
But I don't know how to convert Span to String.
Added I want to use Span<byte>.ToArray().
Encoding.GetString does not accept Span<byte>.
But you can create a Extension Method:
public static class EncodingExtensions
{
public static string GetString(this Encoding encoding, Span<byte> source)
{
//naive way using ToArray, but possible to improve when needed
return encoding.GetString(source.ToArray());
}
}
Then you are able to call:
var foo = new Span<byte>();
var bar = Encoding.ASCII.GetString(foo);

C# switch case Array contains string [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 4 years ago.
Improve this question
Is it possible to look up if a string in contained in an Array, with a switch case?
string text = "blalbac";
string arr[] = {"a","b","c"};
switch (text)
{
case arr.Contains(filename):
//do..
break;
}
I’m not sure what you’re trying to do. Do you want something like
foreach(string item in arr)
{
if(text.Contains(item))
{
...
}
}

C# Convert string to name of bool variable [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 7 years ago.
Improve this question
I trying to make something like that:
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
Anyway, I need to convert string and int to bool name. How can I do this?
Use an array instead:
bool[] abc;
// ...
if (abc[counter] == true) {
{
// some code.
}

C# Multi Spintax - Spin Text [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 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

Categories

Resources