• 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.
  • Posts

    • Ubuntu gets second-ever snapshot release for Questing Quokka by Paul Hill Canonical has announced the release of Ubuntu 25.10 Questing Quokka Snapshot 2, a monthly development build that gives testers and developers a base from which to work on software for the upcoming release. Snapshot 1 was released at the end of May and Snapshot 3 is scheduled for July 31. Notably, the release date of Snapshot 2 and 3 have moved since last month. The Snapshot 2 update is available for various Ubuntu spins, such as Kubuntu and Lubuntu. To download, head to Ubuntu CD Image and go to the link for the version you want, such as ubuntu/. Once you’ve picked, go to releases/ > 25.10/ > snapshot-2/ and download the appropriate image for your computer - most people will want ‘64-bit PC (AMD64) desktop image’. The announcement mentions that these snapshot builds are not production ready, so you should not be installing them on a machine you use to do your work and daily computing. Canonical said that these builds should be seen as “throwaway artifacts”, whatever that means. If you’re an Ubuntu developer, you should submit your changes in the Ubuntu archive by July 28 to see it in the third snapshot. If you make any changes, Canonical asks you to update the Release Notes with the updates that you have worked on, so everyone knows what changed. Speaking of release notes, Canonical has been updating them incrementally. So far, we know that GNOME 48 is being used alongside the Linux 6.14 kernel. The use of GNOME 48 means that Ubuntu 25.10 only supports Wayland sessions as X.org has finally been dropped. Wayland has been used for a while on Ubuntu, so most people shouldn't have any issues as a result of the switchover. If you want to try out Ubuntu 25.10 Snapshot 2, you can find the download links over on the Ubuntu website. Just remember, these are not intended to be used on production machines!
    • Garmin Dash Cam X310: 4K dash cam on sale at lowest price for $334.99 by Paul Hill Dash cam footage can be vital for drivers who get into accidents as it can be used to help with insurance claims if you’re involved in an accident, outside of that, it can be used for filming your trips or recording freak events such as asteroids burning up in the atmosphere. If you’re still without one, or looking to upgrade, take a moment to read about the Garmin Dash Cam X310 which has been reduced by 16% from $399.99 to $334.99. To briefly touch on the features before we get started, the X310 is a compact 4K Ultra HD touchscreen dash cam with a 140-degree field of view, ensuring it captures everything. There’s also a built-in Clarity Polarizer to reduce glare from the windshield and it features automatic recording. The main attraction of the Garmin Dash Cam X310 is undoubtedly the 4K Ultra HD video which will capture fine details, and the 140-degree field of view that ensures it doesn’t miss anything important. Coupled with this, the Clarity Polarizer helps to reduce glare from the windshield so that your video footage is better. It also includes HDR which improves the night vision. Aside from having a touchscreen, the X310 comes with voice control allowing you to save video and audio hands-free, meaning you can focus on driving. It supports multiple languages - English, German, French, Spanish, Italian, and Swedish. Other features include built-in GPS for recording the location, date, and time of incidents; parking guard and live view for security while parked (required a paid Vault subscription); and its compact design which allows it to sit “virtually unnoticed” on the windshield. Please note that the product description says some jurisdictions may restrict the use of dashcams, so check your local laws before buying. If you are looking for a high-resolution dash cam with voice control and advanced parking surveillance (paid subscription), the X310 could be for you. The main downside of this product is that it’s still at a premium price point, despite it being at its lowest price on Amazon. If you don’t mind this, it could definitely be a great choice for you. Garmin Dash Cam X310: $334.99 (Amazon US) / MSRP $399.99 This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • I have a Motorola B12, and it's been working pretty solidly for a year now. 
  • Recent Achievements

    • Week One Done
      Marites earned a badge
      Week One Done
    • One Year In
      runge100 earned a badge
      One Year In
    • One Month Later
      runge100 earned a badge
      One Month Later
    • One Month Later
      jfam earned a badge
      One Month Later
    • First Post
      TheRingmaster earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      563
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      169
    4. 4
      Michael Scrip
      125
    5. 5
      Xenon
      118
  • Tell a friend

    Love Neowin? Tell a friend!