简体   繁体   English

我如何在C#中创建公共子

[英]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#. 而且我不知道该如何处理,因为我是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 来源-Microsoft开发人员网络

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 . 如果方法不返回任何东西,则返回类型应该为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". 如果希望C#方法像VB的“子例程”一样,只需将返回类型标记为“ void”。

Otherwise, specify what type the function should return. 否则,请指定函数应返回的类型 For example: 例如:

public string Notification() { ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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