Edit code based on a string during runtime - c#

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'));

Related

Assign a formula at runtime for accessing elements of List

The scenario is that I have say two different types of cases - case 1 and case 2. For case 1 and case 2 each I have a certain startIndex, endIndex and a formula for accessing the elements of a List.
Now for assigning values startIndex and endIndex I am preferring a normal switch case, however I am at loss for the formula for accessing elements. For case 1 it is say something like List[ a+i ] and for case 2 it is say List[a + (i-b)].
One way can be to have a for loop like this
for(int i=0;;i++)
{
if(case is 1)
then f=a+i
else if(case 2)
then f=a+(i-b)
}
I thought of using delegates. however, as per my knowledge they need to be made global. Actions do not return value. Func can be used but one expression/formula takes only one element (int) and the other takes 3. I need something in lines to this like that anonymous function can be assigned any of above mentioned formulae at runtime from the switch case (as the cases might and will increase in future).
Thank you.
I thought of using delegates. however, as per my knowledge they need
to be made global.
This is not true (actually, there are no truly global variables in C#, since each and every variable needs to be encapsulated inside an object). A public delegate type is indeed visible to all code after referencing the assembly containing this type's code, but a variable of such type can be private.
What I recommend in your situation is to have some sort of mapping from case numbers to delegates. A good idea is to use a Dictionary<TKey, TValue> if you have at most one delegate per case. This dictionary can be stored as a private variable inside the class where your method resides.
public class MyClass
{
private Dictionary<int, Delegate> _delegateMapping = new Dictionary<int, Delegate>;
}
There are a couple of ways you can add elements do the dictionary in the constructor: passing the already populated dictionary, passing an array of delegates, creating these delegates in the constructor itself. Either way, you'll end up with a dictionary of Delegate types, so you'll need to use a cast to be able to use them in your code properly.
for (int i = 1; i < _delegateMapping.Count; i++)
{
switch (i)
{
case 1:
var f = (Action<int>)_delegateMapping[1];
f(i);
break;
case 2:
var f = (Action<int, int>)_delegateMapping[2];
f(i, a);
break;
}
}
Of course I'm greatly improvising here.
It is important to note that if the type of delegate changes inside the dictionary, you will have to modify the cast accordingly inside the switch statement. Otherwise, if no implicit cast exists, you'll get a runtime exception.
Hi guys thank you so very much for your feedbacks. I finally found the solution with Func. This is what my code looks like. I had to manipulate the Func usage a little. I made almost all the vars which I have to use in the Func as global/local to the function where I write these Funcs. My apologies, if I was not able to explain my problem properly.
int i = -1;
Func<int,int> formula = null;
switch(f)
{
case 1:
{
formula = new Func<int,int>(index => { return i; });
}
break;
case 2:
{
formula = new Func<int, int>( index => { return s- (i * c); } );//here s and c are global variables.
}
break;
}
i = startIndex;
while(i < endIndex)
{
var Obj= List[formula.Invoke(i)];
//my code goes here
i++;
}
Let me know if my solution is correct w.r.t performance, logic, C# programming, etc.. :)
EDITED::
#usr and #Kapol I tried the way you suggested and tried to improvise the code like this.
private Dictionary<int, Func<int[], int>> indexFormulae;
private void assignDelegates()
{
indexFormulae = new Dictionary<int, Func<int[], int>>();
indexFormulae.Add(0, getFormula_1);
indexFormulae.Add(1, getFormula_2);
}
private void someFunction(int sp)
{
int i = 0;
Func<int[], int> formula = null;
indexFormulae.TryGetValue(formation,out formula);
i = startIndex;
while (i < endIndex)
{
int[] intValues = new int[] {i,sp,globalVar };
var Obj = List[formula.Invoke(intValues)];
//My code here
i++;
}
}
private int getFormula_1(params int[] intValues)
{
return intValues[0];
}
private int getIndex_Vertical(params int[] intValues)
{
return intValues[1] - (intValues[0] * intValues[2]);
}
So, that with this now I can use these two getFormula methods anywhere in this class rather than keeping them anonymous. and also I think I will stick to params because I might have N number of int params in future for other functions.

Searching ObservableCollection's elements in a string

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.

Increase readability in this try-catch?

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)
{
}
}

Add TextBox.Text to a list using a for loop

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
}

c# - concatenation and properties of TextBox

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.

Categories