how to design a class that where I can store and retrieve data using class properties/method
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to do something like below where I should be able to store and retrieve data though class properties or method,
//set the value
var someObject = {... };
var newMessage = new Message(JsonConvert.SerializeObject(someObject))
{
ContentType = "application/json"
};
//get the value
Message message;
var x = message.GetBody<string>()
I tried creating class like below, but have to supply body for method. How to design this class?
public class Message
{
public Message(object serializableObject);
public string SessionId { get; set; }
public string ContentType { get; set; }
public T GetBody<T>();
}
c#
add a comment |
I would like to do something like below where I should be able to store and retrieve data though class properties or method,
//set the value
var someObject = {... };
var newMessage = new Message(JsonConvert.SerializeObject(someObject))
{
ContentType = "application/json"
};
//get the value
Message message;
var x = message.GetBody<string>()
I tried creating class like below, but have to supply body for method. How to design this class?
public class Message
{
public Message(object serializableObject);
public string SessionId { get; set; }
public string ContentType { get; set; }
public T GetBody<T>();
}
c#
I have no idea what you expect e.g.x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.
– Damien_The_Unbeliever
Jan 4 at 10:48
Ok, ignore theget
part. Can we design a class where I'm setting the value
– user584018
Jan 4 at 10:54
It should be noted thatJsonConvert.SerializeObject(someObject)
returns astring
, so why does your constructorpublic Message(object serializableObject)
useobject
instead ofstring
?
– Peter B
Jan 4 at 11:01
add a comment |
I would like to do something like below where I should be able to store and retrieve data though class properties or method,
//set the value
var someObject = {... };
var newMessage = new Message(JsonConvert.SerializeObject(someObject))
{
ContentType = "application/json"
};
//get the value
Message message;
var x = message.GetBody<string>()
I tried creating class like below, but have to supply body for method. How to design this class?
public class Message
{
public Message(object serializableObject);
public string SessionId { get; set; }
public string ContentType { get; set; }
public T GetBody<T>();
}
c#
I would like to do something like below where I should be able to store and retrieve data though class properties or method,
//set the value
var someObject = {... };
var newMessage = new Message(JsonConvert.SerializeObject(someObject))
{
ContentType = "application/json"
};
//get the value
Message message;
var x = message.GetBody<string>()
I tried creating class like below, but have to supply body for method. How to design this class?
public class Message
{
public Message(object serializableObject);
public string SessionId { get; set; }
public string ContentType { get; set; }
public T GetBody<T>();
}
c#
c#
asked Jan 4 at 10:38
user584018user584018
1,89993150
1,89993150
I have no idea what you expect e.g.x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.
– Damien_The_Unbeliever
Jan 4 at 10:48
Ok, ignore theget
part. Can we design a class where I'm setting the value
– user584018
Jan 4 at 10:54
It should be noted thatJsonConvert.SerializeObject(someObject)
returns astring
, so why does your constructorpublic Message(object serializableObject)
useobject
instead ofstring
?
– Peter B
Jan 4 at 11:01
add a comment |
I have no idea what you expect e.g.x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.
– Damien_The_Unbeliever
Jan 4 at 10:48
Ok, ignore theget
part. Can we design a class where I'm setting the value
– user584018
Jan 4 at 10:54
It should be noted thatJsonConvert.SerializeObject(someObject)
returns astring
, so why does your constructorpublic Message(object serializableObject)
useobject
instead ofstring
?
– Peter B
Jan 4 at 11:01
I have no idea what you expect e.g.
x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.– Damien_The_Unbeliever
Jan 4 at 10:48
I have no idea what you expect e.g.
x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.– Damien_The_Unbeliever
Jan 4 at 10:48
Ok, ignore the
get
part. Can we design a class where I'm setting the value– user584018
Jan 4 at 10:54
Ok, ignore the
get
part. Can we design a class where I'm setting the value– user584018
Jan 4 at 10:54
It should be noted that
JsonConvert.SerializeObject(someObject)
returns a string
, so why does your constructor public Message(object serializableObject)
use object
instead of string
?– Peter B
Jan 4 at 11:01
It should be noted that
JsonConvert.SerializeObject(someObject)
returns a string
, so why does your constructor public Message(object serializableObject)
use object
instead of string
?– Peter B
Jan 4 at 11:01
add a comment |
1 Answer
1
active
oldest
votes
I think I understand your question, but you've missed out on the constructor
new Message(JsonConvert.SerializeObject(someObject)) //<-- string is passed
I think you have misinterpreted the JsonConvert.SerializeObject(someObject)
statement, it will return a string not an object. Whereas you have used the incorrect type as an argument for your constructor.
public Message(object serializableObject); // should be a string
To correct your class, you need to design it the following way...
public class Message
{
private readonly string _json;
public Message(string json)
{
_json = json;
}
public string GetBody() // not generic, but you get the idea now.
{
return json;
}
}
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
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%2f54037298%2fhow-to-design-a-class-that-where-i-can-store-and-retrieve-data-using-class-prope%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think I understand your question, but you've missed out on the constructor
new Message(JsonConvert.SerializeObject(someObject)) //<-- string is passed
I think you have misinterpreted the JsonConvert.SerializeObject(someObject)
statement, it will return a string not an object. Whereas you have used the incorrect type as an argument for your constructor.
public Message(object serializableObject); // should be a string
To correct your class, you need to design it the following way...
public class Message
{
private readonly string _json;
public Message(string json)
{
_json = json;
}
public string GetBody() // not generic, but you get the idea now.
{
return json;
}
}
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
add a comment |
I think I understand your question, but you've missed out on the constructor
new Message(JsonConvert.SerializeObject(someObject)) //<-- string is passed
I think you have misinterpreted the JsonConvert.SerializeObject(someObject)
statement, it will return a string not an object. Whereas you have used the incorrect type as an argument for your constructor.
public Message(object serializableObject); // should be a string
To correct your class, you need to design it the following way...
public class Message
{
private readonly string _json;
public Message(string json)
{
_json = json;
}
public string GetBody() // not generic, but you get the idea now.
{
return json;
}
}
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
add a comment |
I think I understand your question, but you've missed out on the constructor
new Message(JsonConvert.SerializeObject(someObject)) //<-- string is passed
I think you have misinterpreted the JsonConvert.SerializeObject(someObject)
statement, it will return a string not an object. Whereas you have used the incorrect type as an argument for your constructor.
public Message(object serializableObject); // should be a string
To correct your class, you need to design it the following way...
public class Message
{
private readonly string _json;
public Message(string json)
{
_json = json;
}
public string GetBody() // not generic, but you get the idea now.
{
return json;
}
}
I think I understand your question, but you've missed out on the constructor
new Message(JsonConvert.SerializeObject(someObject)) //<-- string is passed
I think you have misinterpreted the JsonConvert.SerializeObject(someObject)
statement, it will return a string not an object. Whereas you have used the incorrect type as an argument for your constructor.
public Message(object serializableObject); // should be a string
To correct your class, you need to design it the following way...
public class Message
{
private readonly string _json;
public Message(string json)
{
_json = json;
}
public string GetBody() // not generic, but you get the idea now.
{
return json;
}
}
answered Jan 4 at 10:55
SvekSvek
6,52732348
6,52732348
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
add a comment |
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
Yes, Thanks!!!!
– user584018
Jan 4 at 10:59
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%2f54037298%2fhow-to-design-a-class-that-where-i-can-store-and-retrieve-data-using-class-prope%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
I have no idea what you expect e.g.
x
to be equal to at the end of the first snippet. Maybe it's clear to you but bear in mind we have no context here to work out what you're doing.– Damien_The_Unbeliever
Jan 4 at 10:48
Ok, ignore the
get
part. Can we design a class where I'm setting the value– user584018
Jan 4 at 10:54
It should be noted that
JsonConvert.SerializeObject(someObject)
returns astring
, so why does your constructorpublic Message(object serializableObject)
useobject
instead ofstring
?– Peter B
Jan 4 at 11:01