So I've got a character called oper, and i'm parsing it's value from a string. In this if statement I'm trying to check if oper's value is " ". I was told single quotes are needed for a char but I'm still not finding success. Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectCalc
{
public partial class Form1 : Form
{
double val1;
double val2;
char oper;
public Form1()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
if (oper = char.Parse('')) ;
}
}
}
There are some issues with your code. First, you do an assignment operation and not equality (single = vs ==)
Secondly, if you have a string and want to check if one of his characters is space, you could do:
string myString = "This is a string";
foreach (var c in myString) {
if (c == ' ') // <=== Note the space between the single quotes
... do something
}
Related
I am trying to pass a string my function in Windows Form Application in order to invoke InteropAssembly.dll from a LabVIEW script. The script takes a string for opening image and do some process and then return 2 different images. But I am getting the error The type 'LVBaseRefnum' is defined in an assembly that is not referenced. You must add a reference to assembly 'NationalInstruments.LabVIEW.RefnumTypes'. I have checked the forum and so and found this . But it was posted quite long time ago and still no valid answer. Have you ever encounter this error any solution? Thanks in advance and my code as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InteropAssembly;
namespace testLabview
{
public partial class Form1 : Form
{
private object img1;
private object img2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1 = null;
this.pictureBox2 = null;
string text = this.textBox1.ToString();
LabVIEWExports.test(text, out Image img1, out Image img2);
this.pictureBox1.Image = img1;
this.pictureBox2.Image = img2;
}
}
}
I wrote a small WF program which caculates some condensators and the ohmic law. I now want to tidy up a little bit. I ran across a issue where I use 2 doubles which both got assigned the value 0. I can remove the value from the voltage double. But not the current one. And I can't figure out why. Is there anything I am missing? Error Message is CS0165 Use of unassigned local variable 'current' and occurs in the line where the CalcResistance Method gets called
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Kondensator_Ohmsches_Gesetz_Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void calcResistance_Click(object sender, EventArgs e)
{
double voltage;
double current;
bool ok = double.TryParse(textBox1.Text, out voltage) && double.TryParse(textBox2.Text, out current);
if (ok)
{
textBox3.Text = Formulacollection.CalcResistance(voltage, current);
}
else
{
textBox3.Text = "Error Format not found";
}
textBox4.Text = Formulacollection.ConvertMicro(textBox3.Text);
textBox5.Text = Formulacollection.ConvertMilli(textBox3.Text);
textBox6.Text = Formulacollection.ConvertKilo(textBox3.Text);
textBox7.Text = Formulacollection.ConvertMega(textBox3.Text);
}
}
Formulacollection Class works like this:
using System;
using System.Collections.Generic;
using System.Text;
using static System.Math;
using System.Numerics;
using System.Globalization;
namespace Kondensator_Ohmsches_Gesetz_Calc
{
public static class Formulacollection
{
public static string CalcResistance(double voltage, double current)
{
var resistance = voltage / current;
return resistance.ToString();
}
}
The second part won't always be evaluated when you use the && operator. Try to use & instead of it, or set a default value to current when you declare it.
Firstly CS0165 is the error occurs when using the uninitialized variable.
But here the error occurs for the variable current which is initialized correctly. The brief about the error is here
So try the variable initialization globally in the class and then try to compile it.
Then there is a probability of the ok variable holding the value of two TryParse simultaneously, and also replace the && with &.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace namegenerator1._2
{
public partial class Form1 : Form
{
string[] FirstNames = { "jan", "jaap", "sjuul", "koen" };
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btn_generate_Click(object sender, EventArgs e)
{
int indexFirstName = rand.Next(FirstNames.Length);
this.txtbox_FirstName.Text = FirstNames[indexFirstName];
}
}
}
This basically makes a little box that generates random names from the array. I need to make it so that before the user gets to that he needs to put in a bunch of names himself. Which I don't know how to do.
You either check the array length to see if there are enough games, or use list as suggested.
If the array.Length is not enough, either use Input box or TextBox to ask for user input.
The input will go in the array/list with list.Add or by rearranging the array
The IDE is Visual Studio 2010.
I have two text file called total-cost.txt and amount.txt
The file inside look like below:
total-cost.txt
4500000
amount.txt
600
The first text file (total-cost.txt) represents the total cost which will display at textbox(textbox name is totalcost).
A second file (amount.txt) represents the increment value for every second.
I'm trying to display the total of cost from total-cost.txt and auto increase the value in every second that set in the amount.txt
For example:
4500000 after 1 second become 4500600 after 2 second 4501200 and so on.
If I change the amount.txt value from 600 to 700 it become
4500000 after 1 second become 4500700 after 2 second 4501400 and so on.
The value will keep it refresh and display latest total cost only.
The issue is that I already display the total-cost value in textbox but I do not know how to increment a value that set by amount.txt
The coding I have done is in below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
namespace new_countdown
{
public partial class Form1 : Form
{
private string TotalCost;
private int TotalFont;
public Form1()
{
InitializeComponent();
}
private void ReadTotalCostFile()
{
try
{
StreamReader sr = File.OpenText("total-cost.txt");
TotalCost = sr.ReadToEnd();
sr.Close();
}
catch { }
}
private void UpdateDisplay()
{
if (totalcost.Text != TotalCost)
{
totalcost.Text = TotalCost;
}
if (totalcost.Font.Size != TotalFont && TotalFont != 0)
{
this.totalcost.Font = new System.Drawing.Font("Microsoft Sans Serif",(float)TotalFont,System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,((byte)(0)));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateDisplay();
ReadTotalCostFile();
}
}
}
Somehow, I just have done the display total cost in the textbox.
I have no ideas to do for auto-increment.
Have anyone share the idea or solution. I have much appreciated it.
using System;
using System.IO;
private void IncrementInt32ValueInFile(string filePath)
{
var currentFileText = File.ReadAllText(filePath);
if (int.TryParse(currentFileText, out int integerValue))
{
File.WriteAllText(filePath, Convert.ToString(++integerValue));
}
throw new Exception($"Incorrect file content. Path: {filePath}"); // If value in file can't be parsed as integer
}
I am writing my own speech recognition program in C# with Microsoft's engine and the way I have the program to recognise commands is to read what is already in a text file. The problem with this is, I have to say the command exactly as it is written. For example, if the command is "what is tomorrows date", I cannot say "what's tomorrows date". I have thought of way to get around it and that is to use the Contains method. Here is my code below,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
namespace TestDECA
{
public partial class Form1 : Form
{
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
SpeechSynthesizer DECA = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"D:\Luke's Documents\Speech Commands\TestCommands.txt")))));
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recongizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
void _recongizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
if (speech.Contains("open" && "fire fox"))
{
System.Diagnostics.Process.Start(#"D:\Program Files (x86)\Mozilla Firefox\firefox.exe");
}
}
}
}
As you can see, I want to check if speech contains the words "open" and "fire fox". However, Visual Studio gives me an error saying that the && operator cannot be applied to strings. Is there a way of checking the text to see if contains those words or not? Any help at all will be appreciated.
The String.Contains() method takes a single string. "open" && "fire fox" does not evaluate to a string. If you want to check if a string contains two different values, do this:
if (speech.Contains("open") && speech.Contains("fire fox"))
{
...
}
You could create an extension method to help make this easier:
public static bool ContainsAll(this string str, params string[] values)
{
foreach (var value in values)
{
if (!str.Contains(value)) return false;
}
return true;
}
And then use it like this:
if (speech.ContainsAll("open", "fire fox"))
{
...
}