Is it possible to instantiate a class using initialization list and call a method, all in single command?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I want to instantiate an object using initialization list and call a method in the same command.



string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();


The class looks like this:



class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}

string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}


I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.



The class object is not needed later. Only the return argument of the method would be needed.










share|improve this question




















  • 1





    Yes, it is, but you might want a static method instead.

    – John
    Nov 16 '18 at 17:04


















-1















I want to instantiate an object using initialization list and call a method in the same command.



string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();


The class looks like this:



class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}

string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}


I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.



The class object is not needed later. Only the return argument of the method would be needed.










share|improve this question




















  • 1





    Yes, it is, but you might want a static method instead.

    – John
    Nov 16 '18 at 17:04














-1












-1








-1








I want to instantiate an object using initialization list and call a method in the same command.



string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();


The class looks like this:



class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}

string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}


I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.



The class object is not needed later. Only the return argument of the method would be needed.










share|improve this question
















I want to instantiate an object using initialization list and call a method in the same command.



string importantData = SearchOptions() {id = "10", className = "Fluffy"}.justAFunction();


The class looks like this:



class SearchOptions : PageBase
{
public SearchOptions()
{
id = string.Empty;
className = string.Empty;
text = string.Empty;
partialText = string.Empty;
XPath = string.Empty;
cssModifier = string.Empty;
}

string void justAFunction()
{
Console.WriteLine(id);
Console.WriteLine(className);
return "ImportantReturn";
}
}


I want to keep the flexibility of defining the necessary fields using initialization list and leave the others empty.



The class object is not needed later. Only the return argument of the method would be needed.







c# object constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 17:45









Rufus L

19.4k31732




19.4k31732










asked Nov 16 '18 at 17:01









hasnain rehmanhasnain rehman

31




31








  • 1





    Yes, it is, but you might want a static method instead.

    – John
    Nov 16 '18 at 17:04














  • 1





    Yes, it is, but you might want a static method instead.

    – John
    Nov 16 '18 at 17:04








1




1





Yes, it is, but you might want a static method instead.

– John
Nov 16 '18 at 17:04





Yes, it is, but you might want a static method instead.

– John
Nov 16 '18 at 17:04












2 Answers
2






active

oldest

votes


















1














You almost have it. You just need to add the new keyword to create a new object before you can call a method on it:



string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();


Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.





Note that there are some other issues with your code that need to be fixed in order to make this work:





  1. justAFunction() should be publicly accessible (in your example, the access modifier is not specified, and the default is private, so it would not work).

  2. You cannot specify two return types (you have it declared as: string void justAFunction. You should remove the void since you're returning a string).


And a few other suggestions:




  1. This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)

  2. The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are public).

  3. Public properties and methods should be PascalCase (not camelCase).


With these suggestions in mind, your classes would look something like:



class PageBase
{
public string Id { get; set; }
public string ClassName { get; set; }
public string Text { get; set; }
public string PartialText { get; set; }
public string XPath { get; set; }
public string CssModifier { get; set; }
}

class SearchOptions : PageBase
{
public SearchOptions()
{
Id = string.Empty;
ClassName = string.Empty;
Text = string.Empty;
PartialText = string.Empty;
XPath = string.Empty;
CssModifier = string.Empty;
}

public string JustAFunction()
{
Console.WriteLine(Id);
Console.WriteLine(ClassName);
return "ImportantReturn";
}
}


And then the call to get the important data looks like:



string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();





share|improve this answer

































    0














    Replace "string void justAFunction()" => "public string justAFunction()".






    share|improve this answer
























    • While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

      – Rufus L
      Nov 16 '18 at 17:44














    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342342%2fis-it-possible-to-instantiate-a-class-using-initialization-list-and-call-a-metho%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You almost have it. You just need to add the new keyword to create a new object before you can call a method on it:



    string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();


    Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.





    Note that there are some other issues with your code that need to be fixed in order to make this work:





    1. justAFunction() should be publicly accessible (in your example, the access modifier is not specified, and the default is private, so it would not work).

    2. You cannot specify two return types (you have it declared as: string void justAFunction. You should remove the void since you're returning a string).


    And a few other suggestions:




    1. This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)

    2. The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are public).

    3. Public properties and methods should be PascalCase (not camelCase).


    With these suggestions in mind, your classes would look something like:



    class PageBase
    {
    public string Id { get; set; }
    public string ClassName { get; set; }
    public string Text { get; set; }
    public string PartialText { get; set; }
    public string XPath { get; set; }
    public string CssModifier { get; set; }
    }

    class SearchOptions : PageBase
    {
    public SearchOptions()
    {
    Id = string.Empty;
    ClassName = string.Empty;
    Text = string.Empty;
    PartialText = string.Empty;
    XPath = string.Empty;
    CssModifier = string.Empty;
    }

    public string JustAFunction()
    {
    Console.WriteLine(Id);
    Console.WriteLine(ClassName);
    return "ImportantReturn";
    }
    }


    And then the call to get the important data looks like:



    string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();





    share|improve this answer






























      1














      You almost have it. You just need to add the new keyword to create a new object before you can call a method on it:



      string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();


      Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.





      Note that there are some other issues with your code that need to be fixed in order to make this work:





      1. justAFunction() should be publicly accessible (in your example, the access modifier is not specified, and the default is private, so it would not work).

      2. You cannot specify two return types (you have it declared as: string void justAFunction. You should remove the void since you're returning a string).


      And a few other suggestions:




      1. This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)

      2. The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are public).

      3. Public properties and methods should be PascalCase (not camelCase).


      With these suggestions in mind, your classes would look something like:



      class PageBase
      {
      public string Id { get; set; }
      public string ClassName { get; set; }
      public string Text { get; set; }
      public string PartialText { get; set; }
      public string XPath { get; set; }
      public string CssModifier { get; set; }
      }

      class SearchOptions : PageBase
      {
      public SearchOptions()
      {
      Id = string.Empty;
      ClassName = string.Empty;
      Text = string.Empty;
      PartialText = string.Empty;
      XPath = string.Empty;
      CssModifier = string.Empty;
      }

      public string JustAFunction()
      {
      Console.WriteLine(Id);
      Console.WriteLine(ClassName);
      return "ImportantReturn";
      }
      }


      And then the call to get the important data looks like:



      string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();





      share|improve this answer




























        1












        1








        1







        You almost have it. You just need to add the new keyword to create a new object before you can call a method on it:



        string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();


        Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.





        Note that there are some other issues with your code that need to be fixed in order to make this work:





        1. justAFunction() should be publicly accessible (in your example, the access modifier is not specified, and the default is private, so it would not work).

        2. You cannot specify two return types (you have it declared as: string void justAFunction. You should remove the void since you're returning a string).


        And a few other suggestions:




        1. This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)

        2. The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are public).

        3. Public properties and methods should be PascalCase (not camelCase).


        With these suggestions in mind, your classes would look something like:



        class PageBase
        {
        public string Id { get; set; }
        public string ClassName { get; set; }
        public string Text { get; set; }
        public string PartialText { get; set; }
        public string XPath { get; set; }
        public string CssModifier { get; set; }
        }

        class SearchOptions : PageBase
        {
        public SearchOptions()
        {
        Id = string.Empty;
        ClassName = string.Empty;
        Text = string.Empty;
        PartialText = string.Empty;
        XPath = string.Empty;
        CssModifier = string.Empty;
        }

        public string JustAFunction()
        {
        Console.WriteLine(Id);
        Console.WriteLine(ClassName);
        return "ImportantReturn";
        }
        }


        And then the call to get the important data looks like:



        string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();





        share|improve this answer















        You almost have it. You just need to add the new keyword to create a new object before you can call a method on it:



        string importantData = new SearchOptions {id = "10", className = "Fluffy"}.justAFunction();


        Since you don't set any reference to the newly created object, it goes out of scope as soon as this line executes and will be cleaned up by the garbage collection engine.





        Note that there are some other issues with your code that need to be fixed in order to make this work:





        1. justAFunction() should be publicly accessible (in your example, the access modifier is not specified, and the default is private, so it would not work).

        2. You cannot specify two return types (you have it declared as: string void justAFunction. You should remove the void since you're returning a string).


        And a few other suggestions:




        1. This assumes that the base class contains the properties you're setting (or they exist in this class and you just left them out for brevity)

        2. The properties you set when instantiating the object must be publicly accessible (or at least accessible from wherever you're creating it - usually they are public).

        3. Public properties and methods should be PascalCase (not camelCase).


        With these suggestions in mind, your classes would look something like:



        class PageBase
        {
        public string Id { get; set; }
        public string ClassName { get; set; }
        public string Text { get; set; }
        public string PartialText { get; set; }
        public string XPath { get; set; }
        public string CssModifier { get; set; }
        }

        class SearchOptions : PageBase
        {
        public SearchOptions()
        {
        Id = string.Empty;
        ClassName = string.Empty;
        Text = string.Empty;
        PartialText = string.Empty;
        XPath = string.Empty;
        CssModifier = string.Empty;
        }

        public string JustAFunction()
        {
        Console.WriteLine(Id);
        Console.WriteLine(ClassName);
        return "ImportantReturn";
        }
        }


        And then the call to get the important data looks like:



        string importantData = new SearchOptions {Id = "10", ClassName = "Fluffy"}.JustAFunction();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 17:52

























        answered Nov 16 '18 at 17:26









        Rufus LRufus L

        19.4k31732




        19.4k31732

























            0














            Replace "string void justAFunction()" => "public string justAFunction()".






            share|improve this answer
























            • While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

              – Rufus L
              Nov 16 '18 at 17:44


















            0














            Replace "string void justAFunction()" => "public string justAFunction()".






            share|improve this answer
























            • While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

              – Rufus L
              Nov 16 '18 at 17:44
















            0












            0








            0







            Replace "string void justAFunction()" => "public string justAFunction()".






            share|improve this answer













            Replace "string void justAFunction()" => "public string justAFunction()".







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 17:08









            EvgeniiEvgenii

            91




            91













            • While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

              – Rufus L
              Nov 16 '18 at 17:44





















            • While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

              – Rufus L
              Nov 16 '18 at 17:44



















            While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

            – Rufus L
            Nov 16 '18 at 17:44







            While this is good advice and fixes a compile error, it doesn't answer the question and therefore should be left as a comment rather than an answer.

            – Rufus L
            Nov 16 '18 at 17:44




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342342%2fis-it-possible-to-instantiate-a-class-using-initialization-list-and-call-a-metho%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma