i am trying to search an observablecollection's items in a string.
I try as in the below code.
for (int j = 0; j < DBControl.Instance.SelectLanguagesFromDatabase().Count; j++)
{
if ( dominatorIntern.ProgrammingLanguages.Contains(ProgrammingLanguagesList[j] )
ProgrammingLanguagesList[j].IsSelected = true;
}
Here ProgrammingLanguagesList is an observablecollection, then i got an error saying
Error 8 Argument 1: cannot convert from
'InternRegistration.ViewModel.SelectionItem<string>' to 'string'
here, dominatorIntern.ProgrammingLanguages is a string storing languages like c,java,c++ and ProgrammingLanguagesList is an observablecollection.With an example
if ProgrammingLanguagesList ={c,java,c#} and dominatorIntern.ProgrammingLanguages=c,java i want to ProgrammingLanguagesList[0].IsSelected = true; and ProgrammingLanguagesList[1].IsSelected = true; how can i do that?
Based on your code... it looks like ProgrammingLanguagesList is an array of InternRegistration.ViewModel.SelectionItem<string>?
You need something like this...
// replace ??? on the next line with your SelectionItem<string>'s property that
// holds the language name.
var vmLanguageNameProp = ProgrammingLanguagesList[j].???
if ( dominatorIntern.ProgrammingLanguages.Contains(vmLanguageNameProp )
{
ProgrammingLanguagesList[j].IsSelected = true;
}
Also note that it looks like you are making a DB call for every loop iteration to get the count... you definitely don't want to do that if that is indeed what is going on there.
Related
I have a program in which the user types a string which the code needs to type out using the InputSimulator NuGet package.
Is there a way for me to type the code and fill in the blanks with strings and have it run? here is what it mean:
Inputsimulator sim = new Inputsimulator();
sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.VK_A);
(VK_A Would be replaced with "VK_" + MyLetter)
Is there any way to change or make code to run during runtime?
I appreciate any help, thanks.
You can just use the TextEntry method instead:
sim.Keyboard.TextEntry("Say hello!");
I don't know about WindowsInput.Native.VirtualKeyCode.VK_A, but it obviously look like an enum, so they must have incremental integer values, So:
int a = (int)WindowsInput.Native.VirtualKeyCode.VK_A;
would give you the value of VK_A, and VK_B would be a+1, and so on...
int a = (int)WindowsInput.Native.VirtualKeyCode.VK_A;
for(int i=0; i<26; i++)
sim.Keyboard.KeyPress((WindowsInput.Native.VirtualKeyCode)(a + i));
You may use a method like this:
WindowsInput.Native.VirtualKeyCode KeyCode(char chr)
{
int a = (int)WindowsInput.Native.VirtualKeyCode.VK_A;
if( a >= 'A' and a<='Z')
return (WindowsInput.Native.VirtualKeyCode)(chr - a);
//else do other cases
}
And then you can use it like this:
sim.Keyboard.KeyPress(KeyCode('B'));
Is it possible to concatenate inside a C# loop? Below is my sample code:
for (int i = 0; i <= metricCount; i++)
{
if (m.metrictNumber == i)
{
aggrgt.Add(new PlainBrgDataSummaryChartAggrgt
{
scoreWk6 = scoresPerDuration.scoresPerDuration.scoreWk6.metricScore1,
scoreWk5 = scoresPerDuration.scoresPerDuration.scoreWk5.metricScore1,
scoreWk4 = scoresPerDuration.scoresPerDuration.scoreWk4.metricScore1,
scoreWk3 = scoresPerDuration.scoresPerDuration.scoreWk3.metricScore1,
scoreWk2 = scoresPerDuration.scoresPerDuration.scoreWk2.metricScore1,
scoreWk1 = scoresPerDuration.scoresPerDuration.scoreWk1.metricScore1
});
}
}
What I want is to concatenate metricScore1.
Here's what I tried:
scoreWk6 = scoresPerDuration.scoresPerDuration.scoreWk6.metricScore + i,
Is that possible?
You cannot. If you see numbers in variable names, it's probably wrong. Numbering your variables means you should have used a container type. Either an array or a list or anything else that supports going through it with a loop index or foreach.
So the answer is no. No, you cannot. You should change the types holding your data.
I was wondering if there is a more aesthetic/easier to read way to write the following:
for (int i = 0; i < 100; i++)
{
// If m.GetString(i) throws an exception, continue.
// Otherwise, do stuff.
try
{
string s = m.GetString(i);
continue;
}
catch (InvalidCastException)
{
}
// do stuff with the message that you know is not a string.
}
Here's what m looks like:
msg[0] = 10
msg[1] = "string"
msg[2] = 2.224574743
// Etc.
// Assume it's different every time.
So therefore when I do m.GetString(0) in this example, it throws an exception, as msg[0] is a uint and not a string. This is what I use to get the type, since m does not contain a GetType and I cannot edit m.
m is an instance of class Message in a library that I cannot edit.
However, despite this working just fine, it feels inefficient (and certainly not reader-friendly) to intentionally create exceptions, even if it's in a try-catch, in order to get the type.
Is there a better way or am I stuck with this?
Edit: Alright, I researched the Message class some more (which I should have done to begin with, my apologies). It is an IEnumerable<object>
Now that I know that m is an IEnumerable<object>, I think this is probably your best bet:
foreach (string s in m.OfType<string>())
{
// Process s, which can't be null.
}
Nice and simple and it appears to handle all the logic that you want, i.e. it will process only the items in the sequence that are strings, and it will ignore all objects of other types.
However as Servy points out, this will not handle nulls in the list because null does not have any type at all.
[My previous answer before I knew the type of m]
I think you can take one of three approaches to this:
(1) Add a bool TryGetString(int index, out string) method to whatever type m is in your example and then do
if (m.TryGetString(i, out s))
// Process s (need to check for null!)
(2) Add a bool IsString(int index) method and call that before calling GetString().
if (m.IsString(i))
{
s = m.GetString(i);
// Process s (need to check for null!)
(3) Alternatively, you could expose the item via something like GetObject(int index) and then do something like Iiya suggested:
string s = m.GetObject(i) as string;
if (s != null)
// Process s
I think (1) or (3) would be best, although there might be a much better solution that we could suggest if we had more information about m.
If you want to process only strings in a non-strongly typed sequence of data, use next code:
for (int i = 0; i < 100; i++)
{
string s = m[i] as string;
if(s != null)
{
}
}
I am trying to take the values in the textboxes, named sequentially from 0-9, and add that to a List using a for loop. I am having problems with the syntax or something.
here is what I have now.
for (int i = 0; i <= amt.Count(); i++)
{
amt[i] = int.Parse(amtBox[i].Text);
}
The error is that amtBox doesnt exist in the current context.
My problem is within the loop where i have amtBox[i].Text. I have tried this several ways and VS always throws an error. I have tried "amtBox" + i and that compiles but then causes an error when I try to do something with it and says "data is of wrong type".
I am new to C# and come from PHP so maybe that is why I think this approach will work. PHP doesnt care about data types where C# really does. I have done this exact thing in PHP many times without any issue.
Any suggestions on another way to do this are appreciated as I am probably coming at this all wrong.
Thanks
One solution would be to declare an array and assign amtBox'es to the individual indexes in the array and then you can iterate on that array.
var amtBoxes = new TextBox[] { amtBox0, amtBox1, .... };
for (int i = 0; i <= amt.Count(); i++)
{
amt[i] = int.Parse(amtBoxes[i].Text);
}
If you end up needing to iterate on your TextBox controls in other places I would consider making the array an instance member of your object.
I suppose that your textbox are named "amtBox" + a number.
(The Name property is "amtBox1" as an example)
In this case you could use
Control[] t = Controls.Find("amtBox" + i, false);
for a code like this
for (int i = 0; i <= amt.Count(); i++)
{
Control[] t = Controls.Find("amtBox" + i, false);
if(t != null && t.Length > 0)
{
amt[i] = int.Parse(t[0].Text);
}
}
My understanding is that you have text boxes named amtBox1, amtBox2, etc., and what you are trying to do is sequence through them. As you point out, this is very easy in PHP. It is possible to do what you're suggesting using reflection, but that is expensive and, in any event, there's probably a better way to do what you're looking for.
You could put all of your amount boxes into an array, and then what you have would work:
var amtBoxes = new[] {
amtBox1,
amtBox2,
amtBox3
}
I'm trying to use something like this:
for (int i = 0; i < 40; i++)
{
string x + i = TextBox + i.Text;
}
Is there any solution for this? I want to use i as an index as if I had an array of TextBox. How can I achieve a textbox's Text property like this?
I want to create for example a string named x1,x2,x3,x4... and value of x1 = TextBox1.Text.
But I could not :(
I have TextBox1,2,3...40 and I just want to pass their text values to new string or string list ex. like x1 = TextBox1.Text, x2 = TextBox2.Text; ..... :(
You're trying to get the Text property from a collection of TextBox - which, by the way, you haven't shown us how you're getting. It's possible that you don't even have an array of TextBox.
If you do, you'd use code like this (note that SomeCodeToGetTextBoxArray() needs to be defined by you):
TextBox[] myTextBoxes = SomeCodeToGetTextBoxArray();
var arrayLength = myTextBoxes.Length;
String[] x = new String[arrayLength];
for (int i = 0; i < arrayLength; i++)
{
x[i] = myTextBoxes[i].Text;
}
What you're looking to do is have a reference to a local variable, field, or property by it's string name that you generate at runtime. The only way to do what you want is to use reflection.
There are different ways to do this depending on if the variable is locally scoped, a field member, or a property member.
Reflection - LocalVariableInfo - FieldInfo - PropertyInfo
Note that while that would be the answer to your question, it is probably better to use an array (or List<T>) and use indexes. Though, it is really impossible to know without knowing what you are trying to do fully (not just in your little snippet). But, it's more likely that an array (or List<T>) is the more correct solution.