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.

3
  • 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. Commented May 24, 2016 at 3:45
  • Seems you defined two LineData class/struct, one is from your above code, another is from MainDialog. Commented May 24, 2016 at 3:48
  • Try making class DataTableTools public. It doesn't help having the properties and methods public when the class is not public. Commented May 24, 2016 at 3:51

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.