Is their a way in c# to instantiate a variable into a method call without using a switch statement.
It sounds like you want to take a string and use that string to call a method on an object, this can be done with reflection without the need for a switch statement.
string methodName = "ToString";
var method = typeof(TypeYourMethodExistsOn).GetMethod(methodName);
method.Invoke(objectInstance, null);
I'm not too clear, either. If you don't want to use reflection (heavy sometimes), for dynamically calling methods using a variable, you could use something like a collection containing delegates as values and call them.
I use an extremely like dictionary object to dynamically call a known method based on string inputs.
psuedo code:
delegate void Del(int i, double j);
class MathClass
{
static void Main()
{
MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"
Del d = m.MultiplyNumbers;
Hashtable ht = new Hashtable();
ht.Add("mult", d);
// Invoke the delegate object.
System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{
((del) ht("mult"))(i, 2);
}
}
// Declare the associated method.
void MultiplyNumbers(int m, double n)
{
System.Console.Write(m * n + " ");
}
}
Related
I am new to dart and am having issues storing data in a class. I know how to create instances but I then want to store each instance into a map that I can easily access. Here is my code below...
class myCar{
var myMapOfCars = new Map(); // I wanted this to be a class level variable that I can continuously add or remove from.
String carName; // Instance
myCar({this.carName});
addInfoToMap() {
// Some logic to increment the index or create the index the
myMapOfCars[theKey] = this.carName; // This should be the class variable.
}
}
Every time I call "addInfoToMap", the "myMapOfCars" instance is reinitialized and empty again. I wanted to add/append into the map so I can have as many cars in there as I want. I am open to other solutions as well, I come from Swift and I know you can do it in Swift. It just makes everything really clean.
Thanks for your help!!
The documentation "Class variables and methods" would be appropriate for this.
Static variables
Static variables (class variables) are useful for class-wide state and
constants:
class Queue {
static const initialCapacity = 16;
// ···
}
void main() {
assert(Queue.initialCapacity == 16);
}
Static variables aren’t initialized until they’re used.
Static methods
Static methods (class methods) don’t operate on an instance, and thus
don’t have access to this. They do, however, have access to static
variables. As the following example shows, you invoke static methods
directly on a class:
import 'dart:math';
class Point {
double x, y;
Point(this.x, this.y);
static double distanceBetween(Point a, Point b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
}
void main() {
var a = Point(2, 2);
var b = Point(4, 4);
var distance = Point.distanceBetween(a, b);
assert(2.8 < distance && distance < 2.9);
print(distance);
}
You can use static methods as compile-time constants. For example, you
can pass a static method as a parameter to a constant constructor.
As an additional reference, you can also visit this blog.
I'm writing the Java SE 8 app on Eclipse IDE. The issue that I came across is following.
private Object[][] adjustIndexTaskValueAdded(int size){
Integer adjustingIndex = 0;
Object[][] tasksDisplay = new Object[size][taskValues[0].length];
for (int i = 0; i < size; i++) {
tasksDisplay[i][0] = taskValues[i][0];//phase colour
tasksDisplay[i][1] = identifyNextRowIDTaskTable(adjustingIndex, i);// the index
}
return tasksDisplay;
}
So, I've got adjustingIndex Integer wrapper class which I pass to the identifyNextRowIDTaskTable() method. So that the local var can store the value which gets modified at the child method.
private String identifyNextRowIDTaskTable(Integer adjustingIndex, int currentRowID){
if(UtilityOperations.isPhaseRow(phaseColourCurrent)){//it's a phase row
adjustingIndex++;
return "";
}
else{//it's a task row
int displayID = tableID - adjustingIndex;
adjustingIndex = 0;
return String.valueOf(displayID);
}
}
The above methods displays the method which modifies the Integer wrapper class which I pass to.
Now when I run the app, the new value is not reflected at the invoker method. It appears that value changes/adjusts at the child method, but the parent method does not see the changes. In the end, the outcome becomes erroneous.
The displayed source-code is simplified...
So, what the problem is?
I pass reference type var, and it is not a recursive operation.
I could use object's state to store the value instead, of-course. Yet, I want to understand the current pitfall.
Best regards
Consider
adjustingIndex++;
This is unboxing the value from the Integer to get an int and incrementing that value, this is equivalent to:
int tmp = adjustingIndex.intValue();
tmp++;
adjustingIndex = Integer.valueOf(tmp);
This resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method - that is a separate reference.
Again consider:
adjustingIndex = 0;
Again this resets the parameter adjustingIndex to be a reference to a new Integer, it does not change the value of the adjustingIndex variable in the calling method.
One alternative would be to use AtomicInteger
AtomicInteger adjustingIndex = new AtomicInteger(0);
increment with
adjustingIndex.incrementAndGet();
set back to zero with
adjustingIndex.set(0);
AtomicInteger has methods to change the value of the integer it contains, in contrast Integer is immutable and its value can't be changed.
I wrote a OnMethodBoundaryAspect attribute for Logging the methods exceptions.
I am in trouble with Complex method parameter.
The method signature is:
TestClass m_tf = new TestClass();
m_tf.DoWorkInternal(1, new Prova1() { ProvaP1=10, ProvaP2=11 });
I be able to trace the first parameter of type int, so i can get the parameter name and value.
But how can i get the values of the properties of the second parameters that is a complex object ?
Thanks in advance.
Giuseppe.
RESOLVED.
Found solution.
The aspect method is like this, and write the target method parameters in json format:
public override void OnException(MethodExecutionArgs args)
{
base.OnException(args);
Dictionary<string, object> m_args = new Dictionary<string, object>();
for (int i = 0; i < args.Arguments.Count(); i++)
{
string name = args.Method.GetParameters()[i].Name;
object obj = args.Arguments.GetArgument(i);
m_args.Add(name, obj);
}
var output = JsonConvert.SerializeObject(m_args);
:
:
}
While you can turn the parameter/argument array into a dictionary and JSON serialize it, another way would be just find it directly using the type.
Following code should do the trick for you.
public override void OnException(MethodExecutionArgs args)
{
Prova1 prova1 = null;
var parameters = args.Method.GetParameters();
var arguments = args.Arguments
for (int i = 0; i < arguments.Count(); i++)
{
var param = parameters[i];
var arg = arguments[i];
if (param.ParameterType == typeof(Prova1))
{
prova1 = arg;
}
}
var output = prova1;
// Do your magic
base.OnException(args);
}
Also, keep in mind of two things.
System.Linq functions should be able to clean out that function a lot.
You are looping through the argument list every time there is an exception. However, the indices of your parameters never change. Thus, you should be able to calculate the index of the complex method parameter once (either in CompileTimeInitialize() or RuntimeInitialize()) and re-use that index.
class test
{
char *str;
public:
test(char *p_str) /**default constructor**/
{
cout<<"Default\n";
int l = strlen(p_str) + 1;
str = (l ? new char[l] : 0);
memcpy(str,p_str,l);
}
test(const test& ob) /**copy**/
{
cout<<"Copy\n";
int l = strlen(ob.str) + 1;
str = (l ? new char[l] : 0);
memcpy(str,ob.str,l);
}
test(test&& ob) /**move constructor **/
{
cout<<"Move Constructor\n";
str = ob.str;
ob.str = nullptr;
}
void my_swap(test &ob1 , test &ob2) /**swap function**/
{
swap(ob1.str , ob2.str);
}
test& operator=(test ob) /**copy is called because of pass by value **/
{
cout<<"Copy Assignment operator\n";
my_swap(*this,ob);
return *this;
/**copy is destroyed as object passed by value is destroyed just after the function returns;**/
}
~test()
{
cout<<"Destructor\n";
delete[] str;
}
void print_str()
{
cout<<str<<"\n";
}
};
The above class contains simple implementation of rule of 5 in c++.Now when a vector of this class is created as following.
int main()
{
vector<test> vec1;
vec1.push_back(test("Hi!There"));
return 0;
}
I get the following output
Default
Move Constructor
Destructor
Destructor
And when one more object is pushed into the vector like this
int main()
{
vector<test> vec1;
vec1.push_back(test("Hi!There"));
vec1.push_back(test("Hello! There"));
return 0;
}
The output is
Default
Move Constructor
Destructor
Default
Move Constructor
Copy
Destructor
Destructor
Destructor
Destructor
Now my question is why is the copy constructor called in the second case and not the first.
Thanku
Since you have 5 destructor calls instead of 4 I assume that the vector had to reallocate it's internal storage when you tried to push the second item. This means that the vector had to move the first pushed item to the newly allocated (larger) memory area. However when a vector reallocates it storage and has to transfer items from the old memory block the the new one, it may happen that the item is transferred with copy instead of move. Reason: How to enforce move semantics when a vector grows?
In your case the correct solution would be using vec1.reserve(2) (before pushing items) to avoid the unnecessary reallocation even if the reallocation happens with move. Frequent reallocation is often the target of memory usage optimization.
Ok so I -roughly- want this code:
test1.m:
Foo *foo = [[Foo alloc] init];
foo.x = 1.0f;
[staticClass bar:*foo.x];
staticClass.m:
-(void)bar:(float *)argVar
{
*argVar += 1.0f;
}
So I'm pointing the argVar to a property of the Foo class. Obivously the current code doesn't work.
What's the proper syntax for/way to do this?
I think that the proper way to do it is this:
float tmp = foo.x;
[staticClass bar:&temp];
foo.x = tmp;
and StaticClass.m should look like this:
+(void) bar:(float *) argvar // < not plus instead of minus, denotes static method
{
*argVar = 1.0f;
}
x is a property of Foo, not a variable. A property is just a short-hand for a pair of get/set methods. It has no address, as such, and so cannot be passed around as you are trying to do.
The simplest work-around is to go through a local variable:
float d = foo.x;
[staticClass bar:&d];
foo.x = d;
Also note that you use &, not *, to take the address of a variable.
Options:
change Foo such that it has a float * property
change +[staticClass bar:] such that it takes and returns a float, updating your client code accordingly
use ivar_getOffset to find the location of the instance variable backing x in your Foo instance