Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am trying everything I can but the object is not getting deserialized, I tried almost all the possible solutions to the problem but nothing is working, if someone can help that would be great.
please see below is the code snippet for the code
it always returns a null value to me.
using System;
using System.Text.Json;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
var a = "{\"enrollmentType\":\"draft\",\"emrName\":\"accuro\",\"emrVersion\":\"v1\",\"workflowType\":\"coordinator\"}";
var x = "{\"enrollmentType\":\"draft\",\"emrName\":\"accuro\",\"emrVersion\":\"v1\",\"workflowType\":\"coordinator\"}";
string json = #"{""id"":10,""name"":""Name"",""revisionDate"":1390293827000}";
var ed = JsonSerializer.Deserialize<EnrollmentExtension>(x);
//.Replace("'", "\"")
if (!string.IsNullOrWhiteSpace(ed.emrName))
{ }
}
}
public class EnrollmentExtension
{
#region MyRegion
private string _emrName;
private string _emrVersion;
private string _workflowType;
private string _enrollmentType; public bool IsDraft()
{
return (string.Compare(_enrollmentType, "draft", true) == 0);
}
#endregion
public string enrollmentType
{
get { return _enrollmentType; }
private set { _enrollmentType = value; }
}
public string workflowType
{
get { return _workflowType; }
private set { _workflowType = value; }
}
public string emrVersion
{
get { return _emrVersion; }
private set { _emrVersion = value; }
}
public string emrName
{
get { return _emrName; }
private set { _emrName = value; }
}
public void SetWorkflowType(string workFlowType)
{
_workflowType = workFlowType;
}
}
public class Test
{
public EnrollmentExtension myprop { get; set; }
}
}
you have a bug in your classes, all your setters are private, but should be public. all properties should be like this
public string enrollmentType
{
get { return _enrollmentType; }
set { _enrollmentType = value; }
}
or you can keep the private setters but create a constructor
public EnrollmentExtension(string enrollmentType, ... and so on, string workflowType)
{
_enrollmentType=enrollmentType;
_workflowType=workflowType;
... and so on
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to use the Delete_Click funktion as laid out in https://stackoverflow.com/a/46898543/12955083
private void Delete_Click(object sender, EventArgs e){
lararLista.Items.Clear(); }
however I get error CS0122 'Collection.Items' is inaccessible due to its protection level
The collection Larare is defined in binding list as lararLista = new BindingList<Larare>();
and the code for Larare is
class Larare : Personal, ILarare
{
#region Variabler och ctor
private string namn;
private int personalID;
private long personNummer;
private long personNnummer;
private string email;
private int telNr;
public Larare(string Namn, int PersonalID, long PersonNummer, string Email, int TelNr)
{
this.Namn = namn;
this.PersonalID = personalID;
this.PersonNummer = personNnummer;
this.Email = email;
this.TelNr = telNr;
}
and the getter and setter are defined in a class that it inherits from
class Personal : IPersonal
{
private string namn;
private int personalID;
private long personNnummer;
private string email;
private int telNr;
private string taBort;
public string Namn
{
get { return namn; }
set { namn = value; }
}
public int PersonalID
{
get { return personalID; }
set { personalID = value; }
}
public long PersonNummer
{
get { return personNnummer; }
set { personNnummer = value; }
}
public string Email
{
get { return email; }
set { email = value; }
}
public int TelNr
{
get { return telNr; }
set { telNr = value; }
}
public string TaBort
{
get { return taBort; }
set { taBort = value; }
}
where is the problem?
edit:
void lararLista_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Larare(personalNamnText.Text, int.Parse(personalPersonalIDText.Text), long.Parse(personalPersonnummerText.Text),
personalEmailText.Text, int.Parse(personalTelNrText.Text));
}
or lararLista.Add(new Larare("Rasmus", "123", "198911224130", "Rasmus#HS.se", "0704554488"));
is the code for adding new objects to the collection
The Items property in a BindingList is not public, so you can't access it directly. Try:
lararLista.ClearItems();
We are using HttpSessionStateBase to store messages in a set up similar to this working example:
public class HttpSessionMessageDisplayFetch : IMessageDisplayFetch
{
protected HttpSessionStateBase _session;
private IList<ICoreMessage> messages
{
get
{
if (_session[EchoCoreConstants.MESSAGE_KEY] == null)
_session[EchoCoreConstants.MESSAGE_KEY] = new List<ICoreMessage>();
return _session[EchoCoreConstants.MESSAGE_KEY] as IList<ICoreMessage>;
}
}
public HttpSessionMessageDisplayFetch()
{
if (HttpContext.Current != null)
_session = new HttpSessionStateWrapper(HttpContext.Current.Session);
}
public void AddMessage(ICoreMessage message)
{
if (message != null)
messages.Add(message);
}
public IEnumerable<IResultPresentation> FlushMessagesAsPresentations(IResultFormatter formatter)
{
var mToReturn = messages.Select(m => m.GetPresentation(formatter)).ToList();
messages.Clear();
return mToReturn;
}
}
When we pass in a QualityExplicitlySetMessage (which inherits from ICoreMessage, see below) it is saved correctly to messages.
This is how the object looks after being inserted into the messages list, at the end of AddMessage(ICoreMessage message) above.
But when we come to access it after changing controllers the inherited member's properties are null, which causes a variety of null reference exceptions.
This is how the object now looks after we call FlushMessagesAsPresentations. I've commented out var mToReturn... as this tries to access one of these null ref properties.
I'd like to ask the following:
Why is the HttpSessionStateBase failing to capture these values taken
by the inherited type?
Is this an issue in saving to the HttpSession or in retrieving?
Is this anything to do with, as I suspect, inheritance?
Or is the fact I'm potentially calling a new controller that dependency injects the HttpSessionMessageDisplayFetch causing an issue?
I'm a first-time poster so please let me know if I'm making any kind of faux pas - Super keen to learn! Any input is very welcome.
Some potentially useful code snippets:
QualityExplicitlySetMessage
public class QualityExplicitlySetMessage : QualityChangeMessage
{
public QualityExplicitlySetMessage(IQPossession before, IQPossession after, IQEffect qEffect)
: base(before, after, qEffect)
{
IsSetToExactly = true;
}
}
QualityChangeMessage - Working example
public abstract class QualityChangeMessage : CoreMessage, IQualityChangeMessage
{
protected PossessionChange Change;
public PossessionChange GetPossessionChange()
{
return Change;
}
protected QualityChangeMessage(IQPossession before, IQPossession after, IQEffect qEffect)
{
Change = new PossessionChange(before, after, qEffect);
StoreQualityInfo(qEffect.AssociatedQuality);
}
public override IResultPresentation GetPresentation(IResultFormatter formatter)
{
return formatter.GetQualityResult(this);
}
#region IQualityChangeMessage implementation
public int LevelBefore
{
get { return Change.Before.Level; }
}
//... And so on with values dependent on the Change property.
}
CoreMessage - Working example
public abstract class CoreMessage : ICoreMessage
{
public string MessageType
{
get { return GetType().ToString(); }
}
public string ImageTooltip
{
get { return _imagetooltip; }
set { _imagetooltip = value; }
}
public string Image
{
get { return _image; }
set { _image = value; }
}
public int? RelevantQualityId { get; set; }
protected void StoreQualityInfo(Quality q)
{
PyramidNumberIncreaseLimit = q.PyramidNumberIncreaseLimit;
RelevantQualityId = q.Id;
RelevantQualityName = q.Name;
ImageTooltip = "<strong>" + q.Name + "</strong><br/>" + q.Description + "<br>" +
q.EnhancementsDescription;
Image = q.Image;
}
public virtual IResultPresentation GetPresentation(IResultFormatter formatter)
{
return formatter.GetResult(this);
}
}
UserController - Working example.
public partial class UserController : Controller
{
private readonly IMessageDisplayFetch _messageDisplayFetch;
public UserController(IMessageDisplayFetch messageDisplayFetch)
{
_messageDisplayFetch = messageDisplayFetch;
}
public virtual ActionResult MessagesForStoryletWindow()
{
var activeChar = _us.CurrentCharacter();
IEnumerable<IResultPresentation> messages;
messages = _messageDisplayFetch.FlushMessagesAsPresentations(_storyFormatter);
var vd = new MessagesViewData(messages)
{
Character = new CharacterViewData(activeChar),
};
return View(Views.Messages, vd);
}
}
This question already has answers here:
What is the { get; set; } syntax in C#?
(20 answers)
Closed 7 years ago.
I have an strange comportement with my singleton class.
public class HttpCommunicator{
public const int TYPEJSON = 1;
private static HttpCommunicator;
private bool TypeIsInit = false;
public static HttpCommunicator Instance {
get{
if( instance == null ){
instance = new HttpCommunication();
}
return instance;
}
}
private HttpCommunicator(){}
public int RequestType {get {return RequestType;} set{ this.RequestType = value; TypeIsInit = true;}
}
And later in another class i call this
HttpComminicator.Instance.RequestType = HttpCommunicator.TYPEJSON;
My app get stuck/freeze and my debugger don't show me any error. But if I change the get;set; method for this attribut to:
public int GetRequestType(){
return RequestType;
}
public void SetRequestType(int value){
RequestType = value;
TypeIsInit = true;
}
everything works like a charm.
Anybody can explain me why I get this?
Check out your property:
public int RequestType
{
get { return RequestType; }
set { this.RequestType = value; TypeIsInit = true; }
}
You have a bunch of problems here.
What happens when you get that property?
RequestType.get is going to execute, which in turn is going to return RequestType;. To return RequestType you must read RequestType, which will trigger RequestType.get and the loop will go on and on and on and on.
My point is that you're trying to return a property by returning said property, which will probably end up causing a StackOverflowException.
The same can be said about your set accessor.
To fix this, have private fields behind the scenes:
private int _requestType;
public int RequestType
{
get { return _requestType; }
set { _requestType = value; TypeIsInit = true; }
}
EDIT: Question Reconstructed.
OK, I have revisited my get and set methods, but I am still very unclear on how it all works.
What I want to achieve is the Model is populated by the Controller, from the values that it takes form the form. This is then sent to the Db_Facade, which compares the uName and uPwd, and if they are equal returns the ACCESS, which will be set for the entire scope of the program.
I don't know if the get and set declarations are done correctly, or if they can be bunched together (If this is possible it would be great because I will be using this for much larger collections of data), and I'm pretty sure I'm implementing them wrong as well.
If you can help, my knowledge of Accessors is incredibly limited.
Here is my Compare Login method in my Controller:
public static void Compare_Login(User_Login_View Login_View)
{
User_Model getACCESS = new User_Model(); // Creates a new oject of User_Model
getACCESS.Name = Login_View.txtUsername.Text; //Populates the Model from the Login View
getACCESS.Pwd = Login_View.txtPassword.Text;
if (getACCESS.ACCESSLEVEL > 0)
{
Login_View.Close();
}
else
{
Login_View.lblError.Visible = true;
}
Login_View.Menu.SetMenuView();
}
Here is my Model:
public class User_Model
{
public string Name
{
get
{
return Db_Facade.uName;
}
set
{
Db_Facade.uName = value;
}
}
public string Pwd
{
get
{
return Db_Facade.uPwd;
}
set
{
Db_Facade.uPwd = value;
}
}
public int ACCESSLEVEL
{
get
{
return Db_Facade.ACCESS;
}
set
{
Db_Facade.ACCESS = value;
}
}
}
Here is the dummy database comparison:
class Db_Facade
{
public static string uName;
public static string uPwd;
public static string cPwd;
public static int ACCESS;
public static void getLoginACCESS()
{
uName = "paul";
uPwd = "pwd";
ACCESS = 1;
/* I get a "getACCESS does not exist" error here
if (uName == getACCESS.Name && uPwd == getACCESS.Pwd)
{
getACCESS.ACCESSLEVEL = ACCESS;
}
else
{
getACCESS.ACCESSLEVEL = 0;
}
*/
}
}
I don't know if it's needed, but here is my View
public partial class User_Login_View : Form
{
public Menu_View Menu { get; set; }
public User_Login_View()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
User_Controller.Compare_Login(this);
}
}
2 Questions / Hints
1.) Where do you call your getLoginACCESS() ?
2.) Why do you think Db_Facade is able to access getACCESSfrom your class User_Controller?
a solution would be to modyfie your getLoginACCESS() to getLoginACCESS(User_Model getACCESS) and than call it in your Compare_Login(User_Login_View Login_View) befor your if like Db_Facade.etLoginACCESS(getACCESS);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have a custom collection and I want to overload the indexer. My problem is that I get a compile time error even though the signatures are different.
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
And when I simply try to implement this:
MessageBox.Show(myDatafile.Header[3].Description);
These are the errors:
ERROR The best overloaded method match for 'XXX.Header.this[XXX.LineNumber]' has some invalid arguments
ERROR Argument '1': cannot convert from 'int' to 'XXX.LineNumber'
I'm starting to think that you can't overload an indexer multiple times? Or what am I doing wrong where this doesn't work. I KNOW WORKAROUNDS such as XXX.Header[(LineNumber)myIntegerValue], but I'd really like it to be VERY novice friendly. Thank you.
EDIT LineNumber is an enum
LONG EDIT Here is my custom collection class (sorry if too long I didn't know what would be necessary)
namespace XXX
{
public class HeaderLines : CollectionBase
{
public void Add(HeaderLine newHeaderLine)
{
List.Add((HeaderLine)newHeaderLine);
}
public void Remove(HeaderLine newHeaderLine)
{
List.Remove((HeaderLine)newHeaderLine);
}
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
}
namespace XXX
{
public class HeaderLine
{
public string Description { get; set; }
public override string ToString()
{
return Description;
}
}
}
EDIT: I feel so sorry to waste everyones time. I found my problem and it was a stupid mistake. I'm just beginning to teach myself through reading. What happened was the class that held the collection didn't correctly implement the indexers. Sorry.
You most definitely can overload indexers (and plenty of system types do so). Here's an example which compiles without problem:
using System;
class Foo
{
public string this[string text]
{
get { return text; }
}
public int this[int number]
{
get { return number; }
}
}
class FooHolder
{
public Foo Foo { get; set; }
}
class Test
{
static void Main()
{
var holder = new FooHolder { Foo = new Foo() };
int x = holder.Foo[10];
string y = holder.Foo["hello"];
}
}
See whether you can come up with a similar short but complete program which fails, and we can work out why. At the moment there are too many unknowns to easily diagnose the problem.
Here is an example that again shows multiple indexers, but also shows it doing the enum conversion like you are doing and it works, you can copy and paste this into LinqPad to run it.
We need more info about your code.
public void Main()
{
DaClass thing = new DaClass();
Console.WriteLine(thing[1]);
Console.WriteLine(thing[Magic.Two]);
}
public class DaClass
{
private List<string> stuff = new List<string>{"Item1", "Item2", "Item3", "Item4"};
public string this[Magic m]
{
get{return stuff[(int)m];}
set{stuff[(int)m] = value;}
}
public string this[int i]
{
get{return stuff[i];}
set{stuff[i] = value;}
}
}
//Results
//Item2
//Item3