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 11 months ago.
Improve this question
I'm fairly new to c# and I'm trying to create a custom object, but they always seem to be empty.
public class Item
{
public string itemName {get;set;}
public int itemAmount {get;set;}
public Item (string name, int amount)
{
name = itemName;
amount = itemAmount;
}
}
public class Backpack : MonoBehaviour
{
void Start()
{
Item gold = new Item ("gold", 5)
}
}
When I try to get gold parameters I get null, 0. Should it work like that? I wanted to use it to quickly add items to a list, and right now I would have to change all of them manually with something like
gold.itemName = "gold"; gold.itemAmount = 5.
Can I do it in another way?
Your assignment in the constructor is the wrong way around, instead it should be:
public Item (string name, int amount)
{
itemName = name;
itemAmount = amount;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So when i read a tutorial i met that piece of code, and i didn't understand why does " T id" and why author after then write in class "id = _id"?
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T _id)
{
id = _id;
}
}
He is just naming the variables differently for ease of reading / understanding. What he does is, declaring a property of type T (generic type) with name id and accepting a value for that in the constructor using the variable _id also of type T. and assigning it to the id proprty. this can also be writter as below but for clarity of reading the above approach may be good.
class Account<T>
{
public T id { get; set; }
public int Sum { get; set; }
public Account(T id)
{
this.id = id;
}
}
So the Constructor is theroeticly (practicly) a Method, it takes Parameters.
So you can new T(5).
The Class ITSELF has a Member which is "id", a variable which holds information AS LONG as the Object of this Class is not disposed.
Parameters of a Method are only Viable inside the Method, so _id is only Aviable in the Constructor.
If u want to Create a new Car with ID 5. The Object has to know that its ID 5.
So you Programmer, Create a Car with ID 5
T lambo = new T(5)
Now the Constructor Method has the _id = 5.
In the Constructor while constructing u Mark on the Car that its which it has been given
id = _id
now id = 5
and _id is gone after the constructor
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm very new to C# and please excuse me if i'm asking you stupid question.
I don't know how to add node to serialized class.
Here is my code:
namespace DuloGames.UI
{
[Serializable]
public class UISpellInfo
{
public int ID;
public string Name;
public Sprite Icon;
public string Description;
public float Range;
public float Cooldown;
public float CastTime;
public float PowerCost;
public float test;
[BitMask(typeof(UISpellInfo_Flags))]
public UISpellInfo_Flags Flags;
}
}
Here is how i try add new node to the serialized class above from another class:
using DuloGames.UI;
public class Character : MonoBehaviour
{
private void AddToNode()
{
UISpellInfo serializedNode = new UISpellInfo();
serializedNode.Add(new UISpellInfo()
{
ID = 1,
Name = "test",
Description = "description"
});
}
}
This is how i try to add new node to the serialized class but it seems i'm not doing it correctly. Can you please help me out ?
I'm not sure what your are trying to do but you UISpellInfo class is just a POCO that has no Add method right?
What are you trying to do.. I think you need to have a List of UISpellInfo object before you can add something to it?
List<UISpellInfo> nodes = new List<UISpellInfo>();
nodes.Add(new UISpellInfo{ID = 1, Name = "test", Description = "description"});
This should work i think.
But then again what will you do with serializedNode, i don't see you are doing anything with it?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Schedule.cs
public static class SchArray
{
public static string[] clientName = new string[20];
public static DateTime[] startDate = new DateTime[20];
public static DateTime[] endDate = new DateTime[20];
public static string[] allocatedDriver = new string[20];
public static string[] depot = new string[20];
public static int count = 3;
}
public void schedule()
{
SchArray.clientName[0] = "eric cartman";
SchArray.clientName[1] = "peter griffin";
SchArray.clientName[2] = "homer simpson";
SchArray.startDate[0] = Convert.ToDateTime("2016,3,2");
SchArray.startDate[1] = Convert.ToDateTime("2016,3,4");
SchArray.startDate[2] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[0] = Convert.ToDateTime("2016,3,3");
SchArray.endDate[1] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[2] = Convert.ToDateTime("2016,3,6");
SchArray.allocatedDriver[0] = "owen";
SchArray.allocatedDriver[1] = "daniel";
SchArray.allocatedDriver[2] = "owen";
SchArray.depot[0] = "depot1";
SchArray.depot[1] = "depot2";
SchArray.depot[2] = "depot3";
}
Work_Schedule.cs
public void schedule()
{
Console.Clear();
Console.WriteLine(" Create Work Schedule ");
Console.WriteLine(Schedule.SchArray.clientName[0]);
Console.ReadKey();
}
Console.WriteLine(Schedule.SchArray.clientName[0]);
^^^^^ this line should display the name the eric cartman, i've debugged it and it says there are no objects in the array, they're are all null.
You need to create an object of Schedule since the array elements ware initialized inside the constructor:
Schedule scheduleObject = new Schedule();
Console.WriteLine(SchArray.clientName[0]);
For this particular scenario, i would like to suggest a different approach of creating a static list/array of objects. Let me modify the class as like the following:
public class SchArray
{
public string clientName;
public DateTime startDate;
public DateTime endDate;
public string allocatedDriver;
public string depot;
public int count = 3;
}
And i have a List<SchArray> defined as static;
public static List<SchArray> StaticSchArray = new List<SchArray>();
Then i can populate the list as like the following:
StaticSchArray.Add(new SchArray() {clientName="eric cartman",
startDate=Convert.ToDateTime("2016,3,2"),
endDate= Convert.ToDateTime("2016,3,2"),
depot="depot1",allocatedDriver ="owen" });
Similarly other elements can also be added to the array. this will be a better option for this scenario.
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 7 years ago.
Improve this question
I knew constructors from Java and have now a C# project. Syntax in both languages is very similar, so I thought this shouldn't be a problem:
class ShapeItems
{
public String masterName = "";
public String stencilName = "";
public Double coordY = 0.0;
public Double coordX = 0.0;
public String shapeText = "";
public void ShapeItems(String mN, String sN, Double X, Double Y, String sT)
{
this.masterName = mN;
this.stencilName = sN;
this.coordX = X;
this.coordY = Y;
this.shapeText = sT;
}
}
But as I wrote the constructor, I received the error:
Member Names cannot be the same as their enclosing type
I've seen some others with this problems here, but the answers won't fix my problem.
Maybe someone here has a hint for me to solve this issue?
You don't have a constructor but a method: void. Remove the word void and it should work.
So
public ShapeItems(params) { }
instead of
public void ShapeItems(params) { }
Remove "void" from constructor signature:
public ShapeItems(...) { }
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 8 years ago.
Improve this question
Why is my extension method GetVendorForXMLElement() erupting in an NRE when I pass it XElements with data?
The same code (except for the custom class used, which here is "Vendor") works with other classes/data, but not here. I get an NRE in the GetVendorForXMLElement() method.
private void buttonVendors_Click(object sender, EventArgs e)
{
IEnumerable<Vendor> vendors = GetCollectionOfVendors();
}
private IEnumerable<Vendor> GetCollectionOfVendors()
{
ArrayList arrList = FetchDataFromServer("http://localhost:21608/api/vendor/getall/dbill/ppus/42");
String contents = "<Vendors>";
foreach (String s in arrList)
{
contents += s;
}
contents += "</Vendors>";
String unwantedPreamble = "<ArrayOfVendor xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/CStore.DomainModels.HHS\">";
contents = contents.Replace(unwantedPreamble, String.Empty);
contents = contents.Replace("</ArrayOfVendor>", String.Empty);
MessageBox.Show(contents);
XDocument xmlDoc = XDocument.Parse(contents);
// The result (NRE) is the same with any one of these three "styles" of calling Select()
//IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select(GetVendorForXMLElement).ToList();
//IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select(x => GetVendorForXMLElement(x));
IEnumerable<Vendor> vendors = xmlDoc.Descendants("Vendor").Select<XElement, Vendor>(GetVendorForXMLElement);
return vendors;
}
private static Vendor GetVendorForXMLElement(XElement vendor)
{
return new Vendor
{
CompanyName = vendor.Element("CompanyName").Value,
VendorID = vendor.Element("VendorID").Value
};
}
public class Vendor
{
public String CompanyName { get; set; }
public String VendorID { get; set; }
public String siteNum { get; set; }
}
There is data; this is what I see with the MessageBox.Show() call prior to the XDocument.Parse() call:
Regardless of which of the three types of calls to "Select(GetVendorForXMLElement)" I use, the NRE occurs. Is it the angle brackets in the CompanyName element ("[blank]")? Or...???
Your element has a VendorId element, not a VendorID element (notice the casing), Element("VendorID") therefore returns null, and calling Value on that throws an NRE.