0

I'm new in the c# world and I'm trying to pass an argument dynamically So I'm doing a small addin on ms Project, and I need to do some calculation when the data is a duration or a Cost, so I have many duration field Duration1,Duration2,... and the same for cost fields So what I'm doing is simple, I found all duration ID and I put them in my DurationList, after that I make a check, if I select only one column and the ID of this selected column is in my list, I take the name of this field and I try to pass it as an argument
This is a piece of my code, here I'm just working on the duration

    using MSProject = Microsoft.Office.Interop.MSProject;
private void Application_WindowSelectionChange(MSProject.Window Window, MSProject.Selection sel, object selType)
        {
    MSProject.Task task = null;
    List <int> DurationList = new List<int> { 188744967,...};
    int value= Int32.Parse(Application.ActiveSelection.FieldIDList[1]);
    Double Cost=0, CostTotal=0;
    if (DurationList.Contains(value)){
    string fieldname= Application.ActiveSelection.FieldName[1];
    for (int i = 1; i <= sel.Tasks.Count; i++)
                {
                    task = sel.Tasks[i];
                    Cost = Convert.ToDouble(task.fieldname); 
                    CostTotal += Cost;       
                }

    }
    }

but when I tried that I get an error message: "Task" doesn't contain a definition for name and no extension method 'name' accepting a first argument of type 'Task' could be found. So what my code is excpected to do, it gets the name of my field, it stores it in my fieldname string and after that I want to pass the content of this string as an arugment of my task.fieldname. If I used a suggested field which appear with the small wrench, like cost or duration, my code runs without issues but I need to get something more dynamic Any idea?

Thanks

freedumz
  • 13
  • 6
  • 4
    I am sorry but after reading your post my brain feels like spaghetti... It is very confusing what you are trying to do and what exactly your problem is. Your posted error message says that you are trying to access a member `name` of the class `Task`, but I don't see it in your posted code. Please post an entire example which can be copy pasted. And please try to describe your aim a little more coherent – Mong Zhu Feb 26 '18 at 13:35
  • Assuming that `Task` is a class, then you need to create a property/method called `Name`, and then call `Convert.ToDouble(task.Name);` – Danny Goodall Feb 26 '18 at 13:36
  • Try and include some more code. What type is the task variable? What are you trying to accomplish? Your question will be answered much faster. ;) –  Feb 26 '18 at 13:38
  • That's better, but we still need more information. What is the `sel` variable? Where is it declared, etc. Also, is this a compiler error, or a run-time exception? If it's a compiler error, what line number is the compiler warning you on? –  Feb 26 '18 at 13:49
  • [Task has a Name property](https://msdn.microsoft.com/en-us/VBA/Project-VBA/articles/task-name-property-project), is that what you meant instead of `fieldname`? – Crowcoder Feb 26 '18 at 13:49
  • Task has a lot of proprety like duration, name, cost,... and I'd like to be able to pass this proprety with the value which is in my fieldname string – freedumz Feb 26 '18 at 13:59
  • I still can't understand. I can tell you that if `Task` doesn't have a property named `fieldname` (which it doesn't), then you can't write `task.fieldname` and this goes for any `Type` other than `dynamic`. – Crowcoder Feb 26 '18 at 14:03
  • Example, task has a proprety named duration, so the idea is to store the name : duration in my string fieldname and after that, using : task.fieldname, would have the same effect than task.duration – freedumz Feb 26 '18 at 14:08
  • In that case you can refer to https://stackoverflow.com/questions/5508050/how-to-get-a-property-value-based-on-the-name?rq=1 – Crowcoder Feb 26 '18 at 14:13
  • I found this post before, but it's not what I need I will try once again, to edit my post – freedumz Feb 26 '18 at 14:19

2 Answers2

0

I am not sure what you are trying to do. Could you explain your intent differently?

Here are some things I noticed though:

You cannot declare and use the "+=" operator in the same line. Since "+=" is the short-hand for adding a value to the already initialized variable on the left.

i.e: x += 2; is equivelant to x = x + 2; therefor x must be initialized (have a current value) by the time you use the += operator on it. When you say double x += anyValue; that actually means x = x + anyValue and x does not yet have a value.

And as for string argument = "Duration"; are you substituting it for a string which would represent a valid double literal i.e: 1.66? I assume you are.

Without Seeing the Task class definition I cannot be sure, what is going on but I assume it is that argument is not an instance member of Task.

K.Roe
  • 33
  • 8
  • I posted this before the code was updated sorry if it doesn't make sense now. – K.Roe Feb 26 '18 at 13:51
  • This should probably be a comment. –  Feb 26 '18 at 13:54
  • He code was vastly different when I posted this. It looked nothing like this. – K.Roe Feb 26 '18 at 13:57
  • That doesn't matter. The point is this answer doesn't actually answer anything. It points out how the question should be improved, which is what comments are for. –  Feb 26 '18 at 14:00
  • It points out flaws in the code and attempts an answer as well as asking for an improved answer. I cannot comment. Based on previous code example the only error could've been that the user had not created the field in the class definition. The previous code was like 2 lines long. – K.Roe Feb 26 '18 at 14:07
  • Not having enough reputation to comment does not give you a special one-time pass to be able to make an answer that's really a comment. There's a reason for the reputation limitation for comments. –  Feb 26 '18 at 14:09
0

If I finally understand, this is how you would get the value of your Task property named "fieldname":

task = sel.Tasks[i];
var valueofFieldName = task.GetType().GetProperty(fieldname).GetValue(task, null);
Cost = Convert.ToDouble(valueofFieldName); 
CostTotal += Cost;  
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • Thanks, it's working but I've a new error message: Additional information: Object reference not set to an instance of an object. – freedumz Feb 26 '18 at 14:40
  • @freedumz there are opportunities for failure there. I have given you best-case scenario code. You should check for nulls and trap for values that are not convertible to Double. – Crowcoder Feb 26 '18 at 14:49
  • Try doing `task = sel?.Tasks[i];` –  Feb 26 '18 at 16:24