Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i'm writing an application in c#, framework 4.0, that tries to write in a PLC. I have a method of writing that receives two parameters:
the path of tag in which to write and the value to write in the tag. When i get the connection to PLC, I dynamically know the type of every tag, so i would suggest for specified tag the value type that the user should pass to the method. Example 1: if user call the method passing PATH A, i would that compiler suggests to the user the VALUE OF TYPE INT because INT is the type of the tag in PLC. Example 2: if user call the method passing PATH B, i would that compiler suggests to the user the VALUE OF TYPE STRINT because STRING is the type of the tag in PLC
. Can i do this? Thanks a lot
Posted
Updated 9-Jul-15 0:48am
v2
Comments
Richard MacCutchan 9-Jul-15 7:02am    
The compiler cannot do that, you need to write the code in your program to show the information according to what the user chooses to do. You can do this by reading the text the user enters, or by providing a drop down that they can make a selection from.

You could maybe make use of the
nameof(yourVariable)
structure in C#. Not sure if that will work with the code you have, but it's a starting point.
 
Share this answer
 
If the number of tag values (PATHs) is small, then expose a method for each of them to write the value. These would have the correct type of value parameter in the method signature. Don't expose the method that takes an arbitrary tag string.
E.g.:
C#
const string PathA="A";
const string PathB="B";
public bool WritePathA(int value)
{
  return WritePLC(PathA, value);
}
public bool WritePathB(string value)
{
  return WritePLC(PathB, value);
}
private bool WritePLC(string tag, int value)
{
  // send value somehow
  return true; // success?
}
private bool WritePLC(string tag, string value)
{
  // send value somehow
  return true; // success?
}

This also assumes that the association between paths and value types is known at compile time. You implied this by what you wanted, however you also stated
When i get the connection to PLC, I dynamically know the type of every tag
which implies to me that the type information is not known until you're connected to the PLC. In this case it is obviously impossible for the compiler to know what will not be known until run time!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900