• 0

Code not working


Question

So there are 2 classes ,and i'm using windows application form, the for has 2 buttons and code for them are as follows :-

 

Class MyBaseClass

 class MyBaseClass
    {
        public MyBaseClass(string baseClassNeedsThis)
        {
            MessageBox.Show("thishe base class:" + baseClassNeedsThis);
        }
    }

Class MySubClass

 class MySubclass : MyBaseClass
    {
        public MySubclass (string baseClassNeedsThis, int anotherValue)
            :base(baseClassNeedsThis)
        {
            MessageBox.Show("Thishe subclass: " + baseClassNeedsThis + "and" + anotherValue);
        }
    }

And Form1.cs

 

public partial class Form1 : Form
    {
        MyBaseClass myBaseClass;
        MySubclass mySubClass;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myBaseClass = new MyBaseClass.MyBaseclass(string baseClassNeedsThis);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            mySubClass = new MySubclass.MySubclass(string baseClassNeedsThis , int anotherValue);
        }
    }

The output expected is up on clicking button1 MyBaseClass  messsge should pop up and on clicking button2 both MyBaseClass  and MySubClass messages should be displayed, i'm getting a lot of errors 

What are the corrections to be made in Form1.cs ?

Link to comment
https://www.neowin.net/forum/topic/1321238-code-not-working/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Try changing the code on your button click events to the code below. 

 

 

private void button1_Click(object sender, EventArgs e)
{
    myBaseClass = new MyBaseClass("Test");
}

private void button2_Click(object sender, EventArgs e)
{
 mySubClass = new MySubclass("Test2", 3);
}

 

  • 0
  On 03/02/2017 at 07:44, Ch33f said:

The output expected is up on clicking button1 MyBaseClass  messsge should pop up and on clicking button2 both MyBaseClass  and MySubClass messages should be displayed, i'm getting a lot of errors

Expand  

If you have compilation errors please post them it makes it much easier to spot the problem.

 

Here the following is not valid C#:

new MyBaseClass.MyBaseclass(string baseClassNeedsThis);

You're confusing method definition and invocation. When you invoke a method (or here, a constructor, same thing), you need to provide it with the parameters it needs, not try to declare new ones. What would you have expected it to print anyway?

 

So as Zag L. mentions, pass a real string, like a string literal (i.e. "Test") or a variable or field of type string.

 

To give an example, you're probably familiar with the "Hello World" program:

Console.WriteLine("Hello World");

This is an invocation of the method Console.WriteLine, which definition looks something like

public static void WriteLine(string line)
{
  ...
}

Here the parameter line will be "Hello World" as this is value passed in. Trying to invoke the method like this is meaningless:

Console.WriteLine(string line); // not valid C#

However this is:

string line = "Hello World!";
Console.WriteLine(line);

Hopefully that clears things up.

  • 0
  On 03/02/2017 at 14:55, Zag L. said:

Try changing the code on your button click events to the code below. 

 

 

private void button1_Click(object sender, EventArgs e)
{
    myBaseClass = new MyBaseClass("Test");
}

private void button2_Click(object sender, EventArgs e)
{
 mySubClass = new MySubclass("Test2", 3);
}

 

Expand  

Oh cool now i get it, thank you for the help

  • 0
  On 03/02/2017 at 07:44, Ch33f said:

So there are 2 classes ,and i'm using windows application form, the for has 2 buttons and code for them are as follows :-

 

Class MyBaseClass

 class MyBaseClass
    {
        public MyBaseClass(string baseClassNeedsThis)
        {
            MessageBox.Show("thishe base class:" + baseClassNeedsThis);
        }
    }

Class MySubClass

 class MySubclass : MyBaseClass
    {
        public MySubclass (string baseClassNeedsThis, int anotherValue)
            :base(baseClassNeedsThis)
        {
            MessageBox.Show("Thishe subclass: " + baseClassNeedsThis + "and" + anotherValue);
        }
    }

And Form1.cs

 

public partial class Form1 : Form
    {
        MyBaseClass myBaseClass;
        MySubclass mySubClass;
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myBaseClass = new MyBaseClass.MyBaseclass(string baseClassNeedsThis);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            mySubClass = new MySubclass.MySubclass(string baseClassNeedsThis , int anotherValue);
        }
    }

The output expected is up on clicking button1 MyBaseClass  messsge should pop up and on clicking button2 both MyBaseClass  and MySubClass messages should be displayed, i'm getting a lot of errors 

What are the corrections to be made in Form1.cs ?

Expand  

 

you are combining things which should be done on separate lines:

Declare first, then use.

 

        private void button1_Click(object sender, EventArgs e)
        {
		string baseClassNeedsThis = "Test";
		myBaseClass = new MyBaseClass.MyBaseclass(baseClassNeedsThis);
        }

        private void button2_Click(object sender, EventArgs e)
        {
		string baseClassNeedsThis = "Test";
		int anotherValue = 25;
		myBaseClass = new MySubclass.MySubclass(baseClassNeedsThis, anotherValue);
        }

 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.