System.String[] argv conversion to string - c#

Good Morning,
I have a problem with my WPF application, I need to use this method:
public static void LaunchTraining(System.String[] argv)
{
svm_train t = new svm_train();
t.run(argv);
}
The problem is that I do not have a stream from the Console (the input is a filename), but I need to declare the name of the file for this method, example:
string filename = "training.txt";
How can I convert my string into System.String[] argv?

There is no feasible way to do what you want.
The best solution would be to create an overload:
public static void LaunchTraining(String argv) {
// Handle String instead of String[]
}
Alternatively you could make an array:
LaunchTraining(new[]{"training.txt");

Maybe you just need the first item of the argv:
if(argv.Any()) t.run(argv[0]);

You can use this static method like below
LaunchTraining(new []{filename});

Related

Text to Speech C#

So I'm just a uni student who's enjoying programming (1st year) and I've decided to try out some new features.
I came across the speech synthesizer and its reallt interesting, but i have one problem.
Say if I said
Console.WriteLine("Hello");
Is there a way that I could get the program to say that without having to add
Console.WriteLine("Hello");
s.Speak("Hello");
?
Like I'm wondering if I am able to have my program speak whatever I type without adding extra code
Cheers
There is no way you could do that. But what you can try is creating a static method that takes a string value and prints "Hello" on to the console and use the TTS engine, like
static void PrintAndSpeak(string message)
{
Console.WriteLine(message);
s.Speak(message);
}
And then use the method inside the entry point:
PrintAndSpeak("Hello");
You could create a method that would take in your string. That method would print and read the message.
Example (pseudocode)
public void writeAndTalk(string message){
Console.WriteLine(message);
s.Speak(message);
}
And you then use the method wherever you like.
You could create a type to do this for you. Something like:
public class TextAndSpeech
{
private readonly WhateverSIs s;
public TextAndSpeech(WhateverSIs s)
{
this.s = s;
}
public Spurt(string message)
{
Console.WriteLine(message);
s.Speak(message);
}
}
I have no idea what type s is that you're using, so replace WhateverSIs with the actual type.
Using it would look something like:
var spurter = new TextAndSpeech(new WhateverSIs());
spurter.Spurt("Hello");
spurter.Spurt("Another thing.");

Use a function or method with variables in them in another class? C#

Im trying to use a method or function from another class, This is how my code looks from the class i want to use the functions from.
public class Crypting
{
internal static void EncryptFile(string inputFile, string outputFile)
{
// My code is in here
}
So I try to use this function in my main form like this.
private void primeButton14_Click(object sender, EventArgs e)
{
Crypting.EncryptFile();
}
It gets red marked and says "No overload for method 'EncryptFile' takes 0 arguments"
When I remove string inputFile, string outputFile
It does not get red marked however I need those 2 arguments for that function to work.
How do I use this function in my main form? Thanks in advance. :)
The message is pretty clear: you have to provide arguments:
EncryptFile(string inputFile, string outputFile) // see the two arguments, inputFile and outputFile?
Call it e.g. like this:
Crypting.EncryptFile(#"c:\myinputfile.txt", #"c:\myoutputfile.txt");
your function EncryptFile needs two parameters to work on, as you specified i.e. input file and output file location. you need to pass them while calling the method. like Crypting.EncryptFile("somestring", "somestring");
To add what #roryap said here: https://stackoverflow.com/a/34004763/327079 - while defining your method, you can also define default values for the parameters on the method:
public class Crypting
{
internal static void EncryptFile(string inputFile = #"c:\input.txt", string outputFile = #"c:\output.txt")
{
Console.WriteLine("inputFile: {0} outputFile: {1}", inputFile, outputFile);
}
}
Then if you use it, you can get following results:
Crypting.EncryptFile(); -> inputFile: c:\input.txt outputFile: c:\output.txt
Crypting.EncryptFile(#"c:\otherInputFile.txt"); -> inputFile: c:\otherInputFile.txt outputFile: c:\output.txt
Crypting.EncryptFile(#"c:\otherInputFile.txt", #"c:\otherOutputFile.txt"); -> inputFile: c:\otherInputFile.txt outputFile: c:\otherOutputFile.txt
It's hard to tell whether it will work for your actual case, but that's what language offers you.

it is possible to get stacktrace of methods calls inside call method?

I want to add more info to the logger at the call method level, and i need to know if exist possibility to get StackTrace of methods calls inside call method.
UPDATE: The purpose of this is to draw the flow of all methods called until the certain step inside call method.
EXAMPLE:
public class Type1
{
internal string method2_T1() {
return new Type2().method1_T2();
}
}
public class Type2
{
public string method1_T2()
{
return "Type2.method1_T2";
}
}
static void Main(string[] args)
{
string t = new Type1().method2_T1();
LogNow();
....
}
and the result to obtain, when I call LogNow(), are:
StackTrace of method2_T1()
...
Thanks
It's pretty easy:
var stackTrace = new StackTrace(true);
var traceToLog = stackTrace.ToString();
The true argument says to include the file info.
Todd Sprang's answer is good as the actual answer, but be aware that the stack trace will change in unpredictable ways when you move to a RELEASE build, or use async/await. Don't rely programatically on the answers because you may come unstuck when you put the code into production.
If you want to know the direct caller of a particular function, in a way Microsoft recommend, there's the useful trick using the [CallerMemberName], [CallerFilePath], and [CallerLineNumber] attributes. Mark up optional parameters like so;
public void LogWithCallerInfo(
string message,
[CallerMemberName] string memberName = "Caller",
[CallerFilePath] string sourceFilePath = "File",
[CallerLineNumber] int sourceLineNumber = 0)
{
WriteProgressMessage(..., memberName, sourceFilePath, sourceLineNumber);
}
and call like this;
LogWithCallerInfo("my message");
The three optional parameters will be replaced with the appropriate call info.

Rename existing method returning void with input arguments

Maybe this is silly, but I'm trying to shorten the calling of the method StreamWriter.WriteLine becasue I have to call it many times throughout my code. So, instead of calling myFile.WriteLine() I would like to write just myFile.WL().
Is this possible?
After searching for a solution I wrote this:
private static void WL(this StreamWriter myFile, params string myString)
{
return (void)myFile.WriteLine(myString);
}
but I get the error
Cannot convert type 'void' to 'void'
Any ideas?
There is no need for the extension method, but just for the sake of completeness...
Remove the return statement from your method, as it doesn't have to return anything.
private static void WL(this StreamWriter myFile, params string myString)
{
myFile.WriteLine(myString);
}
BUT, Reconsider what you are trying to do. There is no point in shortening WriteLine to WL, it doesn't serve any purpose, it will make the code less readable, also you will not be able to cover up all the overloads.
Your Extension method should only be
private static void WL(this StreamWriter myFile, params string myString)
{
myFile.WriteLine(myString);
}
Reasons:
WriteLinemethod of StreamWriter does not return anything i.e. void. It only and I quote Writes a string followed by a line terminator to the text string or stream. So you should remove return keyword from the method. Read here.
BTW, Extension method should probably be public if you want to use it outside of the class you define it in.

Create string in MessageBox or WriteLine

I have a question, I don't really need it for the application at the moment but I was just curious.
Is there a way to create a string and fill it between the parentheses of WriteLine or Messagebox.Show ?
The code should then look something like this I think:
MessageBox.Show(String s = string.Format("Hello World"));
That is not the correct code, my only question is: Is something like that possible?
You can declare a string inside a call like that. However you can assign it.
string s = string.Empty;
MessageBox.Show(s = string.Format("Hello World"));
If you could declare strings inside a functioncall it wouldnt be visible elsewhere. So it woulndt really make any sense having that functionality in the language.
An alternative to Evelie's answer that lets you write it all in one line could be to define a helper method returning a string:
public static string ShowMsg(string msg) {
MessageBox.Show(msg);
return msg;
}
And your code would become:
string s = ShowMsg("Hello World");
or
string s = ShowMsg(string.Format("Now is {0}.", DateTime.Now));
And you could also perform the formatting inside your helper method:
public static string ShowMsg(string format, params object[] args) {
string mgs = string.Format(format, args);
MessageBox.Show(msg);
return msg;
}
And use it as:
string s = ShowMsg("Now is {0}.", DateTime.Now);

Categories