Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
Why is the result of this empty and nothing shows up in the console?
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
pills sthnew= new pills("name", 25);
System.Console.WriteLine();
Console.ReadLine();
}
class pills
{
private int cena;
private string nazwa;
public int Price{
get { return price; }
set { price= value; }
}
public string Name
{
get { return name; }
set { name= value; }
}
public pills() { }
public pills(string Pname)
{
Pname = name;
}
public pills(string Pname, int Pprice)
{
Pname = name;
Pprice = price;
}
}
}
}
I am trying to write a class "Pills" which manages and keeps information about a current pill. The Class should have private fields: Name of Pill, Price of pill.
Because you are writing nothing in the console -
System.Console.WriteLine();
You need to write something to the console to see something on the console:
System.Console.WriteLine("something");
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 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
}
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
I'm trying to create an alternative to the code found on this website (https://www.exercisescsharp.com/oop/first-class-method-tostring) where a dynamic list is used rather than an array. However, my code has failed to execute the ToString method and keeps giving me the NullReferenceException.
Below is my code:
public class MainClass
{
private static List<Person> person;
static void Main(string[] args)
{
string name = String.Empty;
while (name != null)
{
name = Console.ReadLine();
person.Add(new Person(name));
}
foreach (var individual in person)
{
Console.WriteLine(individual.ToString());
}
}
}
public class Person
{
private string name;
public Person (string _name)
{
name = _name;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string ToString()
{
return "Hello. My name is" + Name;
}
}
Your List instance is not initalized, you have to create it first.
private static List<Person> person = new List<Person>();
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 7 years ago.
Improve this question
I'm creating a DLL for a application that I make.
But I got a error when I added the DLL as refference to the console Application but do not know what it means this is the error:
An unhandled exception of type 'System.TypeInitializationException' occurred in ConsoleApplication1.exe
And this is my dll class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Steap
{
public class SteapAPI
{
public static String URL
{
get;
set;
}
public static XmlReader r = XmlReader.Create(URL + "?xml=1&l=english");
public int getSteamID64()
{
int ID = 0;
r.ReadToFollowing("steamID64");
ID = r.ReadContentAsInt();
return ID;
}
public string getSteamID()
{
string ID = String.Empty;
r.ReadToFollowing("steamID");
ID = r.ReadContentAsString();
return ID;
}
public int getVac()
{
int Vac = 0;
r.ReadToFollowing("vacBanned");
Vac = r.ReadContentAsInt();
return Vac;
}
public bool hasVac()
{
if (getVac() == 0)
{
return false;
}
else
{
return true;
}
}
// =================== [ Aliases
public string getName()
{
return getSteamID();
}
}
}
Console application code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Steap;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SteapAPI sapi = new SteapAPI(); // TypeInitializationException was unhandled error here
SteapAPI.URL = "http://steamcommunity.com/id/bluesephire";
Console.ReadKey();
}
}
}
What is wrong or what is missing
You have exception during initialization of static field of your class that leads to failure to load the class and hence the TypeInitializationException exception.
Particular line:
public static XmlReader r = XmlReader.Create(URL + "?xml=1&l=english");
URL is not initialized at the time method called (and even if it would have static value like URL=#"c:\file.txt" there is no guarantee that one field will be initialized first.
Note from that point any access to the SteapAPI class will throw the TypeInitializationException even if it is not touching fields directly involved into original exception.
In this case you shouldn't be using static fields. Static fields will cause huge problems if you ever create two SteapAPI objects, in that when you set one URL, it will overwrite the other one, and you'll never be able to re-initialize the XmlReader.
Here is how the API class should be rewritten to be a full instance class:
namespace Steap
{
public class SteapAPI
{
public String URL
{
get;
set;
}
public XmlReader r;
public SteapAPI(string url)
{
URL = url;
//NOTE: This is wrong! You can't create an XmlReader with a URL
//and expect it to fetch a web resource.
r = XmlReader.Create(URL + "?xml=1&l=english");
}
public int getSteamID64()
{
int ID = 0;
r.ReadToFollowing("steamID64");
ID = r.ReadContentAsInt();
return ID;
}
public string getSteamID()
{
string ID = String.Empty;
r.ReadToFollowing("steamID");
ID = r.ReadContentAsString();
return ID;
}
public int getVac()
{
int Vac = 0;
r.ReadToFollowing("vacBanned");
Vac = r.ReadContentAsInt();
return Vac;
}
public bool hasVac()
{
if (getVac() == 0)
{
return false;
}
else
{
return true;
}
}
// =================== [ Aliases
public string getName()
{
return getSteamID();
}
}
And then to use it in your program:
class Program
{
static void Main(string[] args)
{
SteapAPI sapi = new SteapAPI("http://steamcommunity.com/id/bluesephire");
Console.ReadKey();
}
}
Its a minor change but the benefits are huge, you should learn more about using constructors and the drawbacks of static fields/properties as it applies to multiple instances. Just remember, a static field/property of a non-static class is shared between all "instances" of the class, so setting one will set all "instances" of that class to the new value. This is especially important when doing I/O operations and file/resource reading/writing.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to serialize a class with protoBuf. My class has a field that is a struct. How can I serialize it?
[ProtoContract]
class P
{
[ProtoMember(1)]
private T t;
public P()
{ }
}
[ProtoContract]
public struct T
{
[ProtoMember(1)]
public int a;
[ProtoMember(2)]
public int b;
}
Just serialize/deserialize it - it should work fine. Because t is private, I added an accessor (that is only used by my code below - this accessor is completely unrelated to serialization):
public T T { get { return t; } set { t = value; } }
And then this works just fine:
static class Program
{
static void Main()
{
P p = new P { T = new T { a = 123, b = 456 } },
clone;
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, p);
ms.Position = 0;
clone = Serializer.Deserialize<P>(ms);
}
System.Console.WriteLine(clone.T.a); // 123
System.Console.WriteLine(clone.T.b); // 456
}
}
When using the FileHelpers library I am getting a NullReferenceException when trying to write a .csv file.
I have narrowed the problem down. Whenever I have a null decimal? it throws this exception. It works fine on reading, just not writing.
I have included a sample that shows the same problem as my app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args) {
rec record = new rec { id = 1, mydecimal = null };
List<rec> records = new List<rec> { record };
FileHelpers.FileHelperEngine<rec> engine = new FileHelpers.FileHelperEngine<rec>();
Console.WriteLine(engine.WriteString(records));
}
}
[FileHelpers.DelimitedRecord(",")]
public class rec
{
public int id;
public decimal? mydecimal;
}
}
You can use a custom converter.
public class NullableDecimalConverter : FileHelpers.ConverterBase
{
public override object StringToField(string from)
{
return from;
}
public override string FieldToString(object fieldValue)
{
if (fieldValue == null)
return String.Empty;
return fieldValue.ToString();
}
}
You need to modify your record class to add a [FieldConverter()] attribute to any decimal? field.
[FileHelpers.DelimitedRecord(",")]
public class rec
{
public int id;
[FileHelpers.FieldConverter(typeof(NullableDecimalConverter))]
public decimal? mydecimal;
}
Hate to answer my own question, but FileHelpers 2.9.9 fixes this problem. It used to be available on the official site (marked as beta), but can't find it now.
It is however available in NuGet under a package called FileHelpers-stable