Question:
What does "Class, struct, or interface method must have a return type" mean?
Brandon
2012-08-20 18:23:35 UTC
This is the program.

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;

namespace Ch01HandsOn
{
public partial class PromotionsForm : Form
{

public Form1()
{
InitializeComponent();
}

private void exitButton_Click(object sender, EventArgs e)
{
//End the project.
this.Close();
}
private void clothingButton_Click(object sender, EventArgs e)
{
//Display current promotion.
promotionsLabel.Text = "Take an extra 30% off the clearance items.";
}
private void equipmentLabel_Click(object sender, EventArgs e)
{
//Display current promotion.
promotionsLabel.Text = "Yoga Mats--25% off.";
}
private void juiceBarButton_Click(object sender, EventArgs e)
{
//Display current promotion.
promotionsLabel.Text = "Try a free serving of our new Wheatberry Shake.";
}
private void membershipButton_Click(obect sender, EventArgs e)
{
//Display current promotion.
promotionsLabel.Text = "First month personal trainer included.";
}
private void personalTrainingButton.Click(object sender, EventArgs e)
{
//Display current promotion.
promotionsLabel.Text = "3 free sessions with membership renewal.";
}
}
}


I keep geeting an error about public Form1() and it says: Class, struct, or interface method must have a return type

HELP!
Four answers:
picture6542002
2012-08-20 18:52:52 UTC
you may need understand c sharp dot net function defined.

C sharp defined function syntax is:

()

{



}

description:

The first part, public, is the visibility, and is optional. If you don't define any, then the function will be private.

Next is the type to return. It could be any valid type in C sharp, or as we have done it here, void. A void means that this function returns absolutely nothing,your Form1() function has no return type,here is your code reporting error.

name is function name.your function name is Form1.

parameters and function code,your are right.
Silent
2012-08-20 18:41:49 UTC
Your Form1() method doesn't have a return type. That's because it's a constructor.



A constructor needs to have the same name as the class it's in. Your class is called PromotionForm but your constructor is called Form1.



If you don't understand what that means, you need to go back and read up on how classes and methods work before you go any further.
Frank
2012-08-20 18:25:13 UTC
It needs a return type. Any of these will fix it:

public void Form1() // return nothing

public int Form1() // return an int

public String Form1() // return a String object
?
2012-08-20 23:24:19 UTC
It looks to me that you have jumped directly into Java.



Anyhow, in any programming language we will be writing modules which are also called as functions, macros, sub-routines or sub-programs. All of them will be having some predefined responsibility. That is, they takes some arguments of some type and returns some thing



For example:



double x=Math.sqrt(2);

sqrt is a method which takes one number (int or float or double) and returns its root. Thus, x value becomes 1.414


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...