0

I defined a struct like this

    public enum LineType
    {
        Red,//angle<th,L>th
        Green,//L<th
        Blue,//angle>th,L>th
        none
    }

    public struct LineData
    {
        public double angle;
        public double length;
        public LineType lineType;
        public int number;
    }

In another class called DataTableTools, I defined a function

class DataTableTools
{
    public enum LineType
    {
        Red,//angle<th,L>th
        Green,//L<th
        Blue,//angle>th,L>th
        none
    }

    public struct LineData
    {
        public double angle;
        public double length;
        public LineType lineType;
        public int number;
    }

    public bool InsertData(LineData lineData)
    {
        ...
        return true;
    }
}

However, when I called function in main dialog:

DataTableTools dtTools=new DataTableTools();
bool b=dtTools.InsertData(lineData);

An error occurred: Argument type '...MainDialog.LineData' is not assignable to parameter type 'DataTableTools.LineData'. I really confused about it. Why LineData type was different in different class?

Thanks for help.

Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
Albert Mo
  • 3
  • 2
  • Can you provide full sample code? Currently it is not clear if you declare `LineType` and `LineData` twice, it is not clear what the value of variable `lineData` is. – dotnetom May 24 '16 at 03:45
  • Seems you defined two `LineData` class/struct, one is from your above code, another is from `MainDialog`. – Mango Wong May 24 '16 at 03:48
  • Try making class DataTableTools public. It doesn't help having the properties and methods public when the class is not public. – jdweng May 24 '16 at 03:51

2 Answers2

0

Your LineData struct inside DataTableTools class is call Nested Type. It's often use to implicit the idea this nested struct/class is only use for this class, So the class DataTableTools will recognize the parameter struct LineData in InsertData() as DataTableTools.LineData. That is clearly different from MainDialog.LineData that you defined in your MainDialog class.

For your case, you should use

DataTableTools dtTools=new DataTableTools();
DataTableTools.LineData  lineData = new DataTableTools.LineData ();
//Do something with lineData 
bool b=dtTools.InsertData(lineData);

Second solution could be c-sharp-convert-struct-to-another-struct , from struct MainDialog.LineData to DataTableTools.LineData

Community
  • 1
  • 1
Pham X. Bach
  • 5,284
  • 4
  • 28
  • 42
  • Based on your experience, what is the best way if I have to define same struct in different classes? The method you provided seems a bit of complex. Thanks. – Albert Mo May 25 '16 at 13:58
  • If you find the 2nd solution complex, then you could make a constructor for **DataTableTools.LineData**, then call `DataTableTools.LineData newLineData = new DataTableTools.LineData (lineData.angle, lineData.length, lineData.lineType, lineData.number);`, with **lineData** is instance of that struct from class **MainDialog** – Pham X. Bach May 25 '16 at 15:25
0

Is it possible you have accidentally re-declared your enum and struct. Did you mean to define your class something more like this:

class DataTableTools
{
    public LineType LineType { get; set; }
    public LineData LineData { get; set; }

    public bool InsertData(LineData lineData)
    {
        this.LineData.angle = lineData.angle
        ...
        return true;
    }
}

That would allow you to do something like this:

DataTableTools dtTools=new DataTableTools();
bool b=dtTools.InsertData(lineData);
Tim Ashton
  • 436
  • 6
  • 18