Hi everybody. I have 3 classes namely SchoolCls, PrincipalCls, TeachersCls in a Class Library called CLsLib.
School
namespace ClsLib.Ed
{
public class SchoolCls
{
public int subtract(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
}
PrincipalCls
namespace ClsLib.Ed
{
public class PrincipalCls
{
public int subtractNumPrincipal(int firstNum, int secNum, int thirdNum)
{
return firstNum - secNum * thirdNum;
}
}
}
TeachersCls
namespace ClsLib.Ed
{
public class TeachersCls
{
public int subtractNumTeachers(int firstNum, int secNum, double thirdNum)
{
return firstNum - secNum * thirdNum;
}
}
}
Right now, I would like to call SchoolCls from PrincipalCls in SchoolCls. How do I do it without inheriting it as PrincipalCls:SchoolCls? More importantly, where do I put the code?
I have been told to use the following code, but I do not know where I should place it in.
int result = new SchoolCls().subtract(1, 2);
I also have a Windows Application called ClsLibApp. The code is shown as below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClsLib;
using ClsLib.Ed;
namespace ClsLibApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExchange_Click(object sender, EventArgs e)
{
SchoolCls schExch = new SchoolCls();
string ansExch = schExch.subtract(33, 99).ToString();
MessageBox.Show("School: " + ansExch);
PrincipalCls principalCls = new PrincipalCls();
string principal = principalCls.subtractNumPrincipal(369, 460).ToString();
MessageBox.Show("Principal " + ansE12);
TeacherCls teacherCls = new TeacherCls();
string teacher = teachersCls.subtractNumTeachers(119, 131).ToString();
MessageBox.Show("Teachers: " + ansE14);
}
}
}
Please advise.

1 answers
What you're wanting to do - "I would like to call SchoolCls from PrincipalCls in SchoolCls" - makes no sense to me.
However, the line you've been asked to use:
is creating a new SchoolCls object and then calling its subtract method. You could certainly include this line in a method inside the PrincipalCls class and I'd suggest the following:
namespace ClsLib.Ed{
public class PrincipalCls
{
public int subtractNumPrincipal(int firstNum, int secNum, int thirdNum)
{
return firstNum - secNum * thirdNum;
}
public int subtractNumSchools(int firstNum, int secNum)
{
int result = new SchoolCls().subtract(firstNum, secNum);
return result;
}
}
}
You can then call this method with code such as the following:
answered 2 years ago by:
17279