I have a list that is used as a DataContext in a GridView.
This list is created with the following code:
private void initializeStarredHub()
{
List<StarredData> starredList = new List<StarredData>();
starredList.Add(new StarredData("ms-appx:///Images/Absence.png", "Sample Data 1"));
starredList.Add(new StarredData("ms-appx:///Images/Absence.png", "Sample Data 2"));
StarredHub.DataContext = starredList;
}
Where StarredData is
public class StarredData
{
public static string StarredImage { get; set; }
public static string StarredTitle { get; set; }
public StarredData() { }
public StarredData(string itemImageSet, string itemNameSet)
{
StarredImage = itemImageSet;
StarredTitle = itemNameSet;
}
}
The end result of the above is both starredList[0] and starredList[1] have "Sample Data 2" as the StarredTitle, meaning all previous values are overwritten by the latest set.
Why is this happening and how do I fix it?
That's because you declared static members in StarredData class, just remove the static keywords:
public class StarredData
{
public string StarredImage { get; set; }
public string StarredTitle { get; set; }
public StarredData() { }
public StarredData(string itemImageSet, string itemNameSet)
{
StarredImage = itemImageSet;
StarredTitle = itemNameSet;
}
}
You can fix it by removing the static keyword from your member definition.
Only one copy of a static member exists, regardless of how many instances of the class are created.
Check here: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
Related
I am currently struggling to accept a list of objects from FormData in ASP.NET Core.
The project looks like this:
I have a class called Stavka (English: Item).
public class Stavka
{
public string naziv { get; set; }
public double cenaPoJedinici { get; set; }
public string jedinicaMere { get; set; }
public int kolicina { get; set; }
public Stavka(string naziv, double cenaPoJedinici, string jedinicaMere, int kolicina)
{
this.naziv = naziv;
this.cenaPoJedinici = cenaPoJedinici;
this.jedinicaMere = jedinicaMere;
this.kolicina = kolicina;
}
public Stavka()
{
}
}
I have a class called Faktura (English: Bill) which has a variable called Stavke (English: Items) that is a list containing the Stavka objects.
public class Faktura
{
public int Id { get; set; }
public string pibStart { get; set; }
public string pibEnd { get; set; }
public DateTime datumGen { get; set; }
public DateTime datumRok { get; set; }
public List<Stavka> stavke { get; set;}
public double cena { get; set; }
public string tip { get; set; }
public Faktura(int id, string pibStart, string pibEnd, DateTime datumGen, DateTime datumRok, List<Stavka> stavke, string tip)
{
Id = id;
this.pibStart = pibStart;
this.pibEnd = pibEnd;
this.datumGen = datumGen;
this.datumRok = datumRok;
this.stavke = stavke;
this.tip = tip;
double sumCena = 0;
foreach(Stavka s in stavke)
{
sumCena += s.kolicina * s.cenaPoJedinici;
}
this.cena = sumCena;
}
public Faktura()
{
}
I want to create a new Faktura object and add it to a list within my Controller. I tried to do this with the following code:
[HttpPost("dodajFakturu")]
public IActionResult dodajFakturu([FromForm]string pibStart, [FromForm]string pibEnd,[FromForm]DateTime datumStart, [FromForm]DateTime datumEnd,[FromForm]List<Stavka> stavkeLis, [FromForm]string tip)
{
int id = lst.OrderByDescending(p => p.Id).First().Id + 1;
Faktura f = new Faktura(id, pibStart,pibEnd, datumStart,datumEnd,stavkeLis,tip);
lst.Add(f);
return Ok(SveFakture());
}
And yet, when i post the request (in Swagger/Postman), the variable stavkeLis (which accepts the JSON array) is always empty:
This is certainly because i fundamentally misunderstood the way in which NET Core accepts these variables.
Is there some other way to send a list of objects through form data?
this way you have is currect, but if its not maybe because simple code problem but way that you right the code can be better or you can say develop your code as Below:
// StavkaBody => I Mean All Body In One Json
public async Task<IActionResult> MethodName([FromForm] string
StavkaBody)
{
YourObjectType object = new YourObjectType();
// this will be Populate All Json To Single object And
// You dont Need To Add some Constructors For Done this
JsonConvert.PopulateObject(StavkaBody, objec);
// Example Usage
Console.WriteLine(object.Name);
}
in Here I`ve Used The Newtonsoft.Json For this And Its Make Your Model So Much Simpler.
I Hope Its Helps
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
I have a class with this:
public class myDataType
{
public class GetInvoice
{
public string InvoiceID { get; set; }
public string InvoiceNumber { get; set; }
public decimal InvoiceAmount { get; set; }
public List<InvoiceRow> Rows { get; set; }
}
public class InvoiceRow
{
public decimal RowQty { get; set; }
public string RowCode { get; set; }
public string RowDescription { get; set; }
}
}
And when I want to add data has th
using static test.myDataType;
...
private void LoadData()
{
GetInvoice Invoice = new GetInvoice();
Invoice.InvoiceID = "0a8625e5-62f6-4ad7-a8bf-ab04b1158392";
Invoice.InvoiceNumber = "Inv-001";
Invoice.InvoiceAmount = 100;
Invoice.Rows.Add(new InvoiceRow { RowQty= 1, RowCode = "C100", RowDescription = "Item C100"});
}
When try to add the row:
Invoice.Rows.Add(new InvoiceRow { RowQty= 1, RowCode = "C100",
RowDescription = "Item C100"});
Show me this error "System.NullReferenceException: 'Object reference not set to an instance of an object'"
I think i have a sintax o wrong way to do it
Can someone help?
Thanks in advance
It's not a syntax error, you just haven't initialised the list.
With
public List<InvoiceRow> Rows { get; private set; }
you've declared a place to hold the list, but haven't created the list itself.
(If an analogy helps, imagine you've drawn a line on the wall of your house where you're going to put up a bookshelf, but you haven't actually screwed the shelf to the wall yet - that's the situation your code is in).
If you want the list to always be available you can either initialise it automatically through the property declaration, or in the constructor of the class. Alternatively of course you could leave the calling code to initialise it.
This version just makes it part of the property declaration:
public List<InvoiceRow> Rows { get; private set; } = new List<InvoiceRow>();
You need first to initialize list Rows before you add element to it.
For example in GetInvoice class you can add:
public List<InvoiceRow> Rows { get; set; } = new List<InvoiceRow>();
List is reference type in C# so it needs to be initialized before being used.
If you want to do that in LoadData() method you can do in this way:
private void LoadData()
{
GetInvoice Invoice = new GetInvoice();
Invoice.InvoiceID = "0a8625e5-62f6-4ad7-a8bf-ab04b1158392";
Invoice.InvoiceNumber = "Inv-001";
Invoice.InvoiceAmount = 100;
Invoice.Rows = new List<InvoiceRow>();
Invoice.Rows.Add(new InvoiceRow { RowQty = 1, RowCode = "C100", RowDescription = "Item C100" });
}
I have a list of elements that contain another list.
public class SiloNode
{
public string Key { get; private set; }
public string Url { get; private set; }
public List<NodeQuery> Queries { get; private set; }
}
public class NodeQuery
{
public string Query { get; private set; }
public int Seq { get; private set; }
}
I need to produce a new list containing all queries, with the corresponding parent url.
An example would be:
https://www.bbc.co.uk, How old is the BBC?
https://www.bbc.co.uk, Where can I find a guide to BBC channels?
https://www.bbc.co.uk, How is the BBC funded?
https://www.channel4.com, More queries about channel 4 ...
The output should be in the form of class LinkMeta.
public class LinkMeta
{
public LinkMeta(string url, string text)
{
Url = url;
Text = text;
}
public string Text { get; private set; }
public string Url { get; private set; }
}
The complete list is contained in: root.Children.
The query below gives me all Query elements but I can't get back to Url.
var filtered = root.Children.SelectMany(x => x.Queries.Select(y => y.Query));
You can achieve it with the below linq.
var filtered = (from snode in root
from qry in snode.Queries
select new LinkMeta(snode.Url, qry.Query)).ToList();
It will return List of LinkMeta. Hope it helps.
Lambda expressions have the same scope as the nested block. This means that the nested lambda expression still has access to the variable 'x'. So, you should be able to do the following:
var filtered = root.SelectMany(x => x.Children.Queries.Select(y => new LinkMeta(x.Url, y.Query));
public class WorkingList
{
public string Name { get; set; }
public bool Working { get; set; }
}
Class WorkingList is Contains String
i want use like
public List<WorkingList> Work = new List<WorkingList>();
bool ContainsCheck=false;
foreach(WorkingList list in Work)
{
if(list.Name.Equals("FindName")) { ContainsCheck = true; break }
}
I Want Know Easy get List contains String Method!
using linq ? or anymethod / and
public class TopClass
{
public List<WorkingList> Work = new List<WorkingList>();
public string TopName { get; set; }
}
i want know easy way Topclass.Work.Name contains string result
bool ContainsCheck = Work.Any(list => list.Name.Equals("FindName"));
You can use this:
ContainsCheck =work.any(i=>i.Name=="FindName");
I am using ASP.NET 3.5 with C#2008.
I have a table with data like below :
Id Code Message Details
-- ---- ------ ------------
1 111 xxxxx xxxxxxxxxxx
2 222 yyyyy yyyyyyyyyyy
3 333 zzzzz zzzzzzzz
and so on..
This is static data and I want to use it across application.
I have created a class with properties as below :
public class CodeDetails
{
public int Id { get; set; }
public int Code { get; set; }
public string message { get; set; }
public string details { get; set; }
}
And created another class codeDetailsList to whom I want to make as singletone as below :
public class codeDetailsList
{
private List<CodeDetailst> lstCodeDetailst;
private CodeDetailst()
{
lstCodeDetails = new List<CodeDetails>();
}
}
Now, what I want to do is, I want to add the items of above given tabular data into this singleton class's list and want to access it throughout application.
My question is how to add the items and access them?
Similar to Varun's answer, I just wanted to do a full implementation.
public class CodeDetailsList
{
private static readonly CodeDetailsList _instance = new CodeDetailsList();
public static CodeDetailsList Instance
{
get { return _instance; }
}
public ReadOnlyCollection<CodeDetails> lstCodeDetailst { get; private set; }
private codeDetailsList()
{
var masterList = new List<CodeDetails>();
masterList.Add(new CodeDetails(1, 111, "xxxxx", "xxxxxxxxxxx"));
masterList.Add(new CodeDetails(2, 222, "yyyyy", "yyyyyyyyyyy"));
//... And so on ...
//mark the list as read only so no one can add/remove/replace items in the list
lstCodeDetailst= masterList.AsReadOnly();
}
}
public class CodeDetails
{
public CodeDetails(id, code, message, details)
{
Id = id;
Code = code;
Message = message;
Details = details;
}
//Mark the setters private so no one can change the values once set.
public int Id { get; private set; }
public int Code { get; private set; }
public string Message { get; private set; }
public string Details { get; private set; }
}
The constructor for CodeDetailsList will be called once when you first try to access Instance (If you had other static members in the class the constructor would run on the first time any static member was called).
Because lstCodeDetaillst is a ReadOnlyCollection callers will not be able to add, remove, or replace objects in the list. Also because now CodeDetails has private setters all of the items in it are effectively "read only" too.
public class CodeDetailsList
{
public static readonly CodeDetailsList Instance = new CodeDetailsList();
public List<CodeDetails> ListCodeDetails { get; private set; }
private CodeDetailsList()
{
ListCodeDetails = new List<CodeDetails>
{
new CodeDetails { Id = 1, Code = 1, Details = "xxxxx", Message = "xxxxx"},
new CodeDetails { Id = 2, Code = 2, Details = "yyyyy", Message = "yyyy"} // ...
};
}
}
You should initialize the data in the constructor of codeDetailsList. The constructor should remain private to insure you do not create a new instance. Access the data using the Instance field on CodeDetailsList.
Did you intentionally omit the getInstance() method from your Singleton class? anyway...
The add might look something like:
CodeDetails codeDetails = new CodeDetails();
codeDetails.setId("id1");
codeDetails.setCode("code1");
codeDetails.setMessage("message1");
codeDetails.setDetails("details1");
(CodeDetailsList.getInstanceMethod()).add(codeDetails);
To access:
CodeDetails codeDetails = (CodeDetails)(CodeDetailsList.getInstaceMethod()).get(0);
You can put it in a loop if you have number of records
Hope this helps