简体   繁体   中英

How can i make a public sub in c#

I'm trying to make a web service email, but get the error Must have return type. And I don't know how to handle this because I'am new in C#.

Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;

    namespace Email_Service
    {
        public partial class Service1 : ServiceBase
        {   
            public string name;

            public Service1()
            {
                InitializeComponent();
            }

            protected override void OnStart(string[] args)
            {
            }

            protected override void OnStop()
            {

            }

            public Notification(){
               name = "Denmark";
                return name;//I'm just trying this if its work but its not working....
            }
        }
    }

Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.

Source - Microsoft Developer Network

In your case, you want to return a string back, so your method should be defined as :

public string Notification(){...}

If a method is not returning anything, the return type should be void . Regardless, every method needs to have a return type.

If you want a C# method to be like a VB "subroutine", simply mark the return type "void".

Otherwise, specify what type the function should return. For example:

public string Notification() { ...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM