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;
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
add a comment |
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
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
add a comment |
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
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
c# arrays list
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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"
);
add a comment |
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;
add a comment |
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;
add a comment |
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");
add a comment |
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);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
);
add a comment |
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"
);
add a comment |
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"
);
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"
);
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
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 26 at 4:45
Shahrzad JahanbazShahrzad Jahanbaz
1861 silver badge13 bronze badges
1861 silver badge13 bronze badges
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 26 at 4:43
Luis MezasLuis Mezas
134 bronze badges
134 bronze badges
add a comment |
add a comment |
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");
add a comment |
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");
add a comment |
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");
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");
answered Mar 26 at 4:44
MiggMigg
3392 silver badges10 bronze badges
3392 silver badges10 bronze badges
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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