Question Pass variables between classes

phibobaggins

New member
Joined
Jun 2, 2021
Messages
1
Programming Experience
Beginner
Can someone please explain how you pass variables, in this case string or short from one class to another within the same name space?

i have a Draw_Line class and a Get_Line class. I want to get some data in the Get_Line class and pass it my Draw_Line class so i can draw an object with certain properties. I have no doubt this i user error but appreciate the help.
C#:
namespace Drawing
{
    public class Draw_Line
    {
        public void DrawLine()
        {
            Document dc = Application.DocumentManager.MdiActiveDocument;
            Database db = dc.Database;
            Editor ed = dc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    // get the variables from ProPID_Get_Lines class
                    string layName = getLineLayer(LineLayer);
                    short layColor = getLineColor(LineColor);
                    string lineType = getLineLayer(LineLayer);
                    string lineFile = getLineFile(LineFile);
                }
                catch (System.Exception ex)
                {
                    dc.Editor.WriteMessage("Error encountered...: " + ex.Message);
                    trans.Abort();
                }
            }
        }
    }
}

C#:
namespace Drawing
{
    public class Get_Line
    {
        public string getLineLayer()
        {
            string LineLayer = "Process Major";
            return LineLayer;
        }

        public string getLineType()
        {
            string LineType = "Phantom";
            return LineType;
        }
        
        public string getLineFile()
        {
            string LineFile = "acad.lin";
            return LineFile;
        }

        public short getLineColor()
        {
            short LineColor = 4;
            return LineColor;
        }
    }
}
 
Last edited by a moderator:
All your methods in Get_Line are instance methods, so if Draw_Line is supposed to call those method then it needs an instance of the Get_Line class to call them on. This is no different any other type. If you want to show a form, do you just call Show and a form magically appears? Of course not. You have to create an instance of the appropriate form type and then you call Show on that. In this case, you need to create an instance of the Get_Line class and call those methods on it.

The alternative is to declare the methods static and then call them on the type rather than an instance of the type, much like MessageBox.Show. That means that you can't have multiple instances of the type containing different data and exhibiting different behaviour, but there's no indication in the code you posted that you need that. If all members of a class are static, you should declare the class static too.
 
Back
Top Bottom