In my c# project , I want to convert variable type from TreeViewEventArgs to TreeViewCancelEventArgs , or in the contrary .
I wrote a code , but there is an error :
"Error 1 'object' does not contain a definition for 'Node' and no extension method 'Node' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"
This is a part of my function code :
public void func_make_sql_filters_string(TreeViewEventArgs e1 ,TreeViewCancelEventArgs e2)
{
object value_event_object ;
Type type_event;
if (e1 == null)
{
type_event = e2.GetType();
value_event_object = e2;
}
else
// if(e2 == null)
{
type_event = e1.GetType();
value_event_object = e1;
}
var e = Convert.ChangeType(value_event_object, type_event.GetType());
if (e.Node.Level == 1)
{
array_treeView_checking[e.Node.Index] = e.Node.Parent + " = N'" + e.Node.Text + "'";
}
}
You can't just change from type A to type B. You have to do some type of conversion, which in your case doesn't really make sense. After all, you are just after the Node
property of both, so why don't just get that?
public void func_make_sql_filters_string(TreeViewEventArgs e1 , TreeViewCancelEventArgs e2)
{
TreeNode node = e1?.Node ?? e2?.Node;
if (node == null)
{
throw new Exception("Both events are null or don't have a related node.");
}
if (node.Level == 1)
{
array_treeView_checking[node.Index] = $"{node.Parent} = N'{node.Text}'";
}
}