Call a list class from controllerHow do I check if a list is empty?Create ArrayList from arrayFinding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How to randomly select an item from a list?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I remove a particular element from an array in JavaScript?Why not inherit from List<T>?

How can an advanced civilization forget how to manufacture its technology?

Correlation of independent random processes

'rm' (delete) thousands of files selectively

I quit, and boss offered me 3 month "grace period" where I could still come back

Filtering fine silt/mud from water (not necessarily bacteria etc.)

Crowbar circuit causes unexpected behavior for op amp circuit

How do I determine whether a permit is required for a new gas line?

Was the Ford Model T black because of the speed black paint dries?

How do I take a fraction to a negative power?

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

Extract an attribute value from XML

What is this welding tool I found in my attic?

Is it possible for thermophilic viruses to infect humans?

QGIS Welcome page: What is 'pin to list' for?

Shortest distance around a pyramid

Repeating redundant information after dialogues, to avoid or not?

How to achieve this rough borders and stippled illustration look?

Dropping outliers based on "2.5 times the RMSE"

How do you tell if your Nintendo Switch is the primary console?

diff shows a file that does not exist

Credit union holding car note, refuses to provide details of how payments have been applied

Is Arc Length always irrational between two rational points?

Machine learning and operations research projects

Why do players in the past play much longer tournaments than today's top players?



Call a list class from controller


How do I check if a list is empty?Create ArrayList from arrayFinding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How to randomly select an item from a list?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I remove a particular element from an array in JavaScript?Why not inherit from List<T>?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















How can I call my class list from controller?



Here are my models:



public class AuthJsonResponses

public int Code get; set;
public string Jwt get; set;
//public string[] Message get; set;
public List<RootObject> Message get; set;


public class RootObject

public string msg get; set;
public string code get; set;



This is what I call in the controller:



List<RootObject> rootObj = new List<RootObject>();
rootObj[0].code = "success_04";
rootObj[0].msg = "Access granted";
JsonRes.Message = rootObj;


However there it seems there is a problem with the range which I don't know.
What's wrong with my code?



Thanks in advance.










share|improve this question



















  • 1





    What do you want to achieve? Do you want to initialize the list?

    – Kaushik
    Mar 26 at 4:39











  • use rootObj.Add(new RootObject("success_04","Access granted"));

    – Shailesh Chaudhary
    Mar 26 at 7:05

















0















How can I call my class list from controller?



Here are my models:



public class AuthJsonResponses

public int Code get; set;
public string Jwt get; set;
//public string[] Message get; set;
public List<RootObject> Message get; set;


public class RootObject

public string msg get; set;
public string code get; set;



This is what I call in the controller:



List<RootObject> rootObj = new List<RootObject>();
rootObj[0].code = "success_04";
rootObj[0].msg = "Access granted";
JsonRes.Message = rootObj;


However there it seems there is a problem with the range which I don't know.
What's wrong with my code?



Thanks in advance.










share|improve this question



















  • 1





    What do you want to achieve? Do you want to initialize the list?

    – Kaushik
    Mar 26 at 4:39











  • use rootObj.Add(new RootObject("success_04","Access granted"));

    – Shailesh Chaudhary
    Mar 26 at 7:05













0












0








0








How can I call my class list from controller?



Here are my models:



public class AuthJsonResponses

public int Code get; set;
public string Jwt get; set;
//public string[] Message get; set;
public List<RootObject> Message get; set;


public class RootObject

public string msg get; set;
public string code get; set;



This is what I call in the controller:



List<RootObject> rootObj = new List<RootObject>();
rootObj[0].code = "success_04";
rootObj[0].msg = "Access granted";
JsonRes.Message = rootObj;


However there it seems there is a problem with the range which I don't know.
What's wrong with my code?



Thanks in advance.










share|improve this question
















How can I call my class list from controller?



Here are my models:



public class AuthJsonResponses

public int Code get; set;
public string Jwt get; set;
//public string[] Message get; set;
public List<RootObject> Message get; set;


public class RootObject

public string msg get; set;
public string code get; set;



This is what I call in the controller:



List<RootObject> rootObj = new List<RootObject>();
rootObj[0].code = "success_04";
rootObj[0].msg = "Access granted";
JsonRes.Message = rootObj;


However there it seems there is a problem with the range which I don't know.
What's wrong with my code?



Thanks in advance.







c# arrays list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 5:04









Michał Turczyn

19.1k13 gold badges23 silver badges41 bronze badges




19.1k13 gold badges23 silver badges41 bronze badges










asked Mar 26 at 4:35







user4734580














  • 1





    What do you want to achieve? Do you want to initialize the list?

    – Kaushik
    Mar 26 at 4:39











  • use rootObj.Add(new RootObject("success_04","Access granted"));

    – Shailesh Chaudhary
    Mar 26 at 7:05












  • 1





    What do you want to achieve? Do you want to initialize the list?

    – Kaushik
    Mar 26 at 4:39











  • use rootObj.Add(new RootObject("success_04","Access granted"));

    – Shailesh Chaudhary
    Mar 26 at 7:05







1




1





What do you want to achieve? Do you want to initialize the list?

– Kaushik
Mar 26 at 4:39





What do you want to achieve? Do you want to initialize the list?

– Kaushik
Mar 26 at 4:39













use rootObj.Add(new RootObject("success_04","Access granted"));

– Shailesh Chaudhary
Mar 26 at 7:05





use rootObj.Add(new RootObject("success_04","Access granted"));

– Shailesh Chaudhary
Mar 26 at 7:05












5 Answers
5






active

oldest

votes


















0














You are handling the list like an array, which will not work, you need to make use of the Add method, for example



rootObj.Add(new RootObj
code = "success_04",
msg = "Access granted"
);


You can also make use of the AddRange method to add multiple objects, for example



rootObj.AddRange(new List<RootObj>new RootObj
code = "success_04",
msg = "Access granted"
, new RootObj
code = "success_05",
msg = "Access denied"
);





share|improve this answer
































    1














    You can do one of the things below :



     List<RootObject> rootObj = new List<RootObject>();
    var newObj=new RootObject()

    code = "success_04",
    msg = "Access granted"
    ;
    rootObj.Add(newObj);
    List<RootObject> rootObj1 = new List<RootObject>()

    new RootObject()

    code = "success_04",
    msg = "Access granted"

    ;


    and then



    JsonRes.Message = rootObj;





    share|improve this answer






























      0














      As you are using List actually you can use methods inherited from IEnumerable class, as List is an implementation of IEnumerable.



      You can do this:



      List<RootObject> rootObj = new List<RootObject>();
      //delete this---- rootObj[0].code = "success_04";
      //delete this---- rootObj[0].msg = "Access granted";
      //use this
      rootObj.Add(new RootObject
      code = "success_04",
      msg = "Access Granted"
      )
      JsonRes.Message = rootObj;





      share|improve this answer






























        0














        Access your AuthJsonResponses instance directly. First, update that class to make Message have a private setter and instantiate it as a List<RootObject>



        public class AuthJsonResponses

        public int Code get; set;
        public string Jwt get; set;
        public List<RootObject> Messages get; private set; = new List<RootObject>();


        public class RootObject

        public string msg get; set;
        public string code get; set;



        In your code, directly add the object to the Message property. I suggest renaming it to Messages to indicate it's a collection



        jsonRes.Messages.Add(new RootObjectmsg ="Access granted", code="success_04");





        share|improve this answer






























          0














          The problem you are facing is that your are refernecing first element of a list right afeter initializing the list! Initialization itself just creates the object of List class, it does not create any elements!



          So, you have to create objects of RootObject class yourself and then add it to list:



          // create object
          var r = new RootObject();
          r.code = "success_04";
          r.msg = "Access granted";
          // add it to list
          rootObj.Add(r);





          share|improve this answer

























            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%2f55349921%2fcall-a-list-class-from-controller%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown
























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You are handling the list like an array, which will not work, you need to make use of the Add method, for example



            rootObj.Add(new RootObj
            code = "success_04",
            msg = "Access granted"
            );


            You can also make use of the AddRange method to add multiple objects, for example



            rootObj.AddRange(new List<RootObj>new RootObj
            code = "success_04",
            msg = "Access granted"
            , new RootObj
            code = "success_05",
            msg = "Access denied"
            );





            share|improve this answer





























              0














              You are handling the list like an array, which will not work, you need to make use of the Add method, for example



              rootObj.Add(new RootObj
              code = "success_04",
              msg = "Access granted"
              );


              You can also make use of the AddRange method to add multiple objects, for example



              rootObj.AddRange(new List<RootObj>new RootObj
              code = "success_04",
              msg = "Access granted"
              , new RootObj
              code = "success_05",
              msg = "Access denied"
              );





              share|improve this answer



























                0












                0








                0







                You are handling the list like an array, which will not work, you need to make use of the Add method, for example



                rootObj.Add(new RootObj
                code = "success_04",
                msg = "Access granted"
                );


                You can also make use of the AddRange method to add multiple objects, for example



                rootObj.AddRange(new List<RootObj>new RootObj
                code = "success_04",
                msg = "Access granted"
                , new RootObj
                code = "success_05",
                msg = "Access denied"
                );





                share|improve this answer















                You are handling the list like an array, which will not work, you need to make use of the Add method, for example



                rootObj.Add(new RootObj
                code = "success_04",
                msg = "Access granted"
                );


                You can also make use of the AddRange method to add multiple objects, for example



                rootObj.AddRange(new List<RootObj>new RootObj
                code = "success_04",
                msg = "Access granted"
                , new RootObj
                code = "success_05",
                msg = "Access denied"
                );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 26 at 4:45

























                answered Mar 26 at 4:39









                Andre LombaardAndre Lombaard

                4,75312 gold badges48 silver badges85 bronze badges




                4,75312 gold badges48 silver badges85 bronze badges























                    1














                    You can do one of the things below :



                     List<RootObject> rootObj = new List<RootObject>();
                    var newObj=new RootObject()

                    code = "success_04",
                    msg = "Access granted"
                    ;
                    rootObj.Add(newObj);
                    List<RootObject> rootObj1 = new List<RootObject>()

                    new RootObject()

                    code = "success_04",
                    msg = "Access granted"

                    ;


                    and then



                    JsonRes.Message = rootObj;





                    share|improve this answer



























                      1














                      You can do one of the things below :



                       List<RootObject> rootObj = new List<RootObject>();
                      var newObj=new RootObject()

                      code = "success_04",
                      msg = "Access granted"
                      ;
                      rootObj.Add(newObj);
                      List<RootObject> rootObj1 = new List<RootObject>()

                      new RootObject()

                      code = "success_04",
                      msg = "Access granted"

                      ;


                      and then



                      JsonRes.Message = rootObj;





                      share|improve this answer

























                        1












                        1








                        1







                        You can do one of the things below :



                         List<RootObject> rootObj = new List<RootObject>();
                        var newObj=new RootObject()

                        code = "success_04",
                        msg = "Access granted"
                        ;
                        rootObj.Add(newObj);
                        List<RootObject> rootObj1 = new List<RootObject>()

                        new RootObject()

                        code = "success_04",
                        msg = "Access granted"

                        ;


                        and then



                        JsonRes.Message = rootObj;





                        share|improve this answer













                        You can do one of the things below :



                         List<RootObject> rootObj = new List<RootObject>();
                        var newObj=new RootObject()

                        code = "success_04",
                        msg = "Access granted"
                        ;
                        rootObj.Add(newObj);
                        List<RootObject> rootObj1 = new List<RootObject>()

                        new RootObject()

                        code = "success_04",
                        msg = "Access granted"

                        ;


                        and then



                        JsonRes.Message = rootObj;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 26 at 4:45









                        Shahrzad JahanbazShahrzad Jahanbaz

                        1861 silver badge13 bronze badges




                        1861 silver badge13 bronze badges





















                            0














                            As you are using List actually you can use methods inherited from IEnumerable class, as List is an implementation of IEnumerable.



                            You can do this:



                            List<RootObject> rootObj = new List<RootObject>();
                            //delete this---- rootObj[0].code = "success_04";
                            //delete this---- rootObj[0].msg = "Access granted";
                            //use this
                            rootObj.Add(new RootObject
                            code = "success_04",
                            msg = "Access Granted"
                            )
                            JsonRes.Message = rootObj;





                            share|improve this answer



























                              0














                              As you are using List actually you can use methods inherited from IEnumerable class, as List is an implementation of IEnumerable.



                              You can do this:



                              List<RootObject> rootObj = new List<RootObject>();
                              //delete this---- rootObj[0].code = "success_04";
                              //delete this---- rootObj[0].msg = "Access granted";
                              //use this
                              rootObj.Add(new RootObject
                              code = "success_04",
                              msg = "Access Granted"
                              )
                              JsonRes.Message = rootObj;





                              share|improve this answer

























                                0












                                0








                                0







                                As you are using List actually you can use methods inherited from IEnumerable class, as List is an implementation of IEnumerable.



                                You can do this:



                                List<RootObject> rootObj = new List<RootObject>();
                                //delete this---- rootObj[0].code = "success_04";
                                //delete this---- rootObj[0].msg = "Access granted";
                                //use this
                                rootObj.Add(new RootObject
                                code = "success_04",
                                msg = "Access Granted"
                                )
                                JsonRes.Message = rootObj;





                                share|improve this answer













                                As you are using List actually you can use methods inherited from IEnumerable class, as List is an implementation of IEnumerable.



                                You can do this:



                                List<RootObject> rootObj = new List<RootObject>();
                                //delete this---- rootObj[0].code = "success_04";
                                //delete this---- rootObj[0].msg = "Access granted";
                                //use this
                                rootObj.Add(new RootObject
                                code = "success_04",
                                msg = "Access Granted"
                                )
                                JsonRes.Message = rootObj;






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 26 at 4:43









                                Luis MezasLuis Mezas

                                134 bronze badges




                                134 bronze badges





















                                    0














                                    Access your AuthJsonResponses instance directly. First, update that class to make Message have a private setter and instantiate it as a List<RootObject>



                                    public class AuthJsonResponses

                                    public int Code get; set;
                                    public string Jwt get; set;
                                    public List<RootObject> Messages get; private set; = new List<RootObject>();


                                    public class RootObject

                                    public string msg get; set;
                                    public string code get; set;



                                    In your code, directly add the object to the Message property. I suggest renaming it to Messages to indicate it's a collection



                                    jsonRes.Messages.Add(new RootObjectmsg ="Access granted", code="success_04");





                                    share|improve this answer



























                                      0














                                      Access your AuthJsonResponses instance directly. First, update that class to make Message have a private setter and instantiate it as a List<RootObject>



                                      public class AuthJsonResponses

                                      public int Code get; set;
                                      public string Jwt get; set;
                                      public List<RootObject> Messages get; private set; = new List<RootObject>();


                                      public class RootObject

                                      public string msg get; set;
                                      public string code get; set;



                                      In your code, directly add the object to the Message property. I suggest renaming it to Messages to indicate it's a collection



                                      jsonRes.Messages.Add(new RootObjectmsg ="Access granted", code="success_04");





                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        Access your AuthJsonResponses instance directly. First, update that class to make Message have a private setter and instantiate it as a List<RootObject>



                                        public class AuthJsonResponses

                                        public int Code get; set;
                                        public string Jwt get; set;
                                        public List<RootObject> Messages get; private set; = new List<RootObject>();


                                        public class RootObject

                                        public string msg get; set;
                                        public string code get; set;



                                        In your code, directly add the object to the Message property. I suggest renaming it to Messages to indicate it's a collection



                                        jsonRes.Messages.Add(new RootObjectmsg ="Access granted", code="success_04");





                                        share|improve this answer













                                        Access your AuthJsonResponses instance directly. First, update that class to make Message have a private setter and instantiate it as a List<RootObject>



                                        public class AuthJsonResponses

                                        public int Code get; set;
                                        public string Jwt get; set;
                                        public List<RootObject> Messages get; private set; = new List<RootObject>();


                                        public class RootObject

                                        public string msg get; set;
                                        public string code get; set;



                                        In your code, directly add the object to the Message property. I suggest renaming it to Messages to indicate it's a collection



                                        jsonRes.Messages.Add(new RootObjectmsg ="Access granted", code="success_04");






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 26 at 4:44









                                        MiggMigg

                                        3392 silver badges10 bronze badges




                                        3392 silver badges10 bronze badges





















                                            0














                                            The problem you are facing is that your are refernecing first element of a list right afeter initializing the list! Initialization itself just creates the object of List class, it does not create any elements!



                                            So, you have to create objects of RootObject class yourself and then add it to list:



                                            // create object
                                            var r = new RootObject();
                                            r.code = "success_04";
                                            r.msg = "Access granted";
                                            // add it to list
                                            rootObj.Add(r);





                                            share|improve this answer



























                                              0














                                              The problem you are facing is that your are refernecing first element of a list right afeter initializing the list! Initialization itself just creates the object of List class, it does not create any elements!



                                              So, you have to create objects of RootObject class yourself and then add it to list:



                                              // create object
                                              var r = new RootObject();
                                              r.code = "success_04";
                                              r.msg = "Access granted";
                                              // add it to list
                                              rootObj.Add(r);





                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                The problem you are facing is that your are refernecing first element of a list right afeter initializing the list! Initialization itself just creates the object of List class, it does not create any elements!



                                                So, you have to create objects of RootObject class yourself and then add it to list:



                                                // create object
                                                var r = new RootObject();
                                                r.code = "success_04";
                                                r.msg = "Access granted";
                                                // add it to list
                                                rootObj.Add(r);





                                                share|improve this answer













                                                The problem you are facing is that your are refernecing first element of a list right afeter initializing the list! Initialization itself just creates the object of List class, it does not create any elements!



                                                So, you have to create objects of RootObject class yourself and then add it to list:



                                                // create object
                                                var r = new RootObject();
                                                r.code = "success_04";
                                                r.msg = "Access granted";
                                                // add it to list
                                                rootObj.Add(r);






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 26 at 5:03









                                                Michał TurczynMichał Turczyn

                                                19.1k13 gold badges23 silver badges41 bronze badges




                                                19.1k13 gold badges23 silver badges41 bronze badges



























                                                    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%2f55349921%2fcall-a-list-class-from-controller%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

                                                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript