Question

I have two methods in my application, both return similar objects from different web services.

The first one compiles ok, but the second one which I have just added won't compile, giving me an error message 'method must return a value'

here is my code:

public SforceService() {
        this.Url = global::email2case_winForm.Properties.Settings.Default.email2case_sforce_SforceService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }

    public SforceServiceSandbox()
    {
        // 
        // 
        this.Url = global::email2case_winForm.Properties.Settings.Default.SForceService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true))
        {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else
        {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }

What am I missing please, is there a special declaration that I should add?

Was it helpful?

Solution 2

public SforceService()
{
}

This syntax is for a constructor which is called when creating the class. The name must be the same as the class name.

If you want a method with no return type use void

public void SforceServiceSandbox()
{
}

It looks like you want one class to handle 2 scenerios. In which case you have two options,

Two classes

public class SforceService
{
    public SforceService() {
        this.Url = global::email2case_winForm.Properties.Settings.Default.email2case_sforce_SforceService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }
}

public class SforceServiceSandbox
{
    public SforceServiceSandbox()
    {
        this.Url = global::email2case_winForm.Properties.Settings.Default.SForceService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true))
        {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else
        {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }
}

Or one class where you pass in a url

public class SforceService
{
    private SforceService() { }

    public SforceService(TypeOfUrl url)
    {
        this.Url = url;
        if ((this.IsLocalFileSystemWebService(this.Url) == true))
        {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else
        {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }
}

OTHER TIPS

Both "methods" appear to be constructors. Constructors must have the same name as the enclosing type. And if you have multiple constructors, they must have different parameter lists ("overloads").

Here, only one of the two methods you posted can be a constructor, and it must have the same name as the type. The other method, with a different name, is a regular method, and must have a return type. If the method doesn't return a value, its return type is void.

public class MyType()
{
    public MyType() { }              // constructor 1
    public MyType(int x) { }         // constructor 2
    public void Foo() { }            // regular method
    public int Bar() { return 42; }  // regular method 2
}

You need to add the void modifier to your method if it does not return a value. The method without the modifier is the constructor and is correct. So add void to whichever one isn't your classname.

If you don't want to return a value from a method, declare it void

public void Something() { }

constructors are in the form of:

public ClassName() { }

and must have the same name as the class it is declared in.

To return a value from a method, declare the type eg:

public string GetAString() { return ""; }

In C# a method that does not return a value should be declared "void", e.g.

public void Foo()
{

}

You miss the keyword void in your methods in order to execute the the code inside the {} if you want to return a value just preceed the type of the object you want to return before the name of the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top