TypeInitializationException was unhandled #2 [closed] - c#

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.

Related

C# | Receiving CS0236 Error: A field initializer cannot reference the non-static field, method, or property 'getInfo.BSN_Navigator'

I am trying to read an event log from my local computer using the EventLogReader and EventRecord classes. Using C#.
I keep getting the error
CS0236 Error: A field initializer cannot reference the non-static field, method, or property 'getInfo.BSN_Navigator'
Unsure what I am doing wrong.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics.Eventing.Reader;
/// </var bank>
/// </var bank>
namespace EventLogInfoReader
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
String inputString = Console.ReadLine();
}
}
}
public class getInfo
{
public static PathType FilePath { get; private set; }
EventLogReader BSN_Navigator = new EventLogReader("c:\\Users\\banvilb\\Documents\\Event Log\\FalconBackup_Sep192016T124905\\BSN_Navigator.evt", FilePath);
EventRecord bsnRecord = BSN_Navigator.ReadEvent();
public void getLogName()
{
string x = bsnRecord.LogName;
Console.WriteLine(x);
}
public void getId()
{
int x = bsnRecord.Id;
Console.WriteLine(x);
}
}
EventRecord bsnRecord = BSN_Navigator.ReadEvent(); needs to go in the constructor.
Fields are limited in how they can be initialized on the declaration. In your case, you are trying to call a method, but you can't call methods outside of a method. This means you need to initialize it in the constructor.
public class getInfo
{
EventLogReader BSN_Navigator = new EventLogReader("BSN_Navigator.evt", FilePath);
EventRecord bsnRecord;
public getInfo()
{
bsnRecord = BSN_Navigator.ReadEvent();
}
}

No result in Console [closed]

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");

How to define constructor outside the class in c#

I am new to c# and just switched from c++ to c#.
I was doing something like this in c++:
Class A
{
public : A(char *argv);//declaration of constructor
}
then in main i was doing like this:
int main(int argc, char **argv)
{
A Obj(argv[1]);
}
then definition of constructor i do like this :
A::A(char * argv)
{
//Here i use this command line argument argv which contains a file.
}
I tried to write equivalent code in c# which is as follows:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shekhar_final
{
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
Huffman(char *args);
}
public Huffman(char *args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args);//object creation
}
}
}// Line:34
The couple of errors i got are ://I have indicated the line corresponding to the errors in my code
shekhar_c#.cs(16,25): error CS1525: Unexpected symbol `Huffman', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,33): error CS1530: Keyword `new' is not allowed on namespace elements
shekhar_c#.cs(18,36): error CS1525: Unexpected symbol `BinaryReader', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
shekhar_c#.cs(18,79): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
shekhar_c#.cs(34,1): error CS8025: Parsing error
Compilation failed: 4 error(s), 1 warnings
Could you please help me in writing c# equivalent of this c++ (removing these errors). Extra guidance are also welcome because i am beginner to c#.
Unlike C++ where you have a choice of combining the declaration and the definition of a member function in the header, or placing the declaration in the header and the implementation in the cpp file, in C# there is no such choice: if a function has a body (i.e. it is not abstract), the body needs to be part of the declaration:
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
Huffman(string args) {
using (var stream = new BinaryReader(System.IO.File.OpenRead(args)))
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
In C#, declarations and implementations go together:
namespace shekhar_final
{
class Huffman
{
public int DataSize {get; set;}
public int Length {get; set;}
public int I {get;set;}
public int IsThere {get;set;}
public int TotalNodes {get;set;}
private string code;
public Huffman(string[] args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman objSym = new Huffman(args);//object creation
}
}
}// Line:34
You don't define methods ahead of time in C# - they're defined within the class itself. Try this instead:
class Huffman
{
public int data_size,length,i,is_there, total_nodes;
string code;
public Huffman(char *args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args); //Here is the error
}
}
The main phlosophy between C# and C++ are different. In C++ you have a header file and an implementation file. In C#, everthing needs to be within a class. So, you declare the constructor to the class and put the implementation within it.
class funny {
public funny() {
... add your constructor stuff here
}
... other stuff ...
}
C# requires constructors to be defined within the class:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shekhar_final
{
public class Huffman{
public int data_size,length,i,is_there, total_nodes;
string code;
public Huffman(string[] args) //called from MyClass Line:16
{
using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) //Line : 18
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
byte processingValue = stream.ReadByte();
}
}
}
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym =new Huffman(args);//object creation
}
}
}// Line:34
In c# you do not separate declaration and definition. There is no such concept as declaration in c# since all the types exist together in an assembly. If you wish to use multiple files in c3 for classes you can use the concept of partial classes.

Filehelpers NullReferenceException when trying to write a null decimal value

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

C# Instantiate Class from String given an Interface

I am trying to make an instance of a class based on a string that will be retrieved from the User Interface, and then I want to access the properties of the instance of the class.
Here is an overview of what I have so far -
namespace MamdaAdapter
{
public interface IExchange
{
string GetTransport();
}
}
namespace MamdaAdapter
{
public class Exchange
{
public class Arca : IExchange
{
private const string _Transport = "tportname";
public string GetTransport()
{
return _Transport;
}
}
public static IExchange DeriveExchange(string ExchangeName)
{
IExchange SelectedExchange = (IExchange)Activator.CreateInstance(Type.GetType(ExchangeName));
return SelectedExchange;
}
}
}
namespace MyUserInterface
{
public class MainForm
{
private void simpleButton1_Click(object sender, EventArgs e)
{
IExchange SelectedExchange = Exchange.DeriveExchange("Exchange.Arca");
Console.WriteLine(SelectedExchange.GetTransport());
}
}
}
UPDATE:
Right now, I'm getting an Exception that says the "Value cannot be null" which to me means that it is unable to create the instance of the class given the string provided -
The problem here is how you specify the name of your class:
First, specify the namespace. Second, since Arca is an inner class you must use '+' instead of '.'
(...) = Exchange.DeriveExchange("MamdaAdapter.Exchange+Arca");
Assuming you UI doesnt expose the full type name, you typically want a dictionary to associate the display name to the type:
Dictionary<string, Type> _associations = new Dictionary<string, Type>();
Then, you simply instantiate the new object:
if(_associations.ContainsKey(someString))
{
Type selectedType = _associations[someString];
return Activator.CreateInstance(selectedType) as IExchange;
}
throw new ApplicationException("No type defined for that string yo");
If the string is not known at compile time, you basically need to check for the existance of the type:
var type = Type.GetType(someString);
if(type != null)
{
// Do Stuff
}
I wrote a small c# console application to simulate your need, tested ok, hope it helps:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MamdaAdapter;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IExchange SelectedExchange = Exchange.DeriveExchange("MamdaAdapter.Arca");
Console.WriteLine(SelectedExchange.GetTransport());
}
}
}
namespace MamdaAdapter
{
public interface IExchange
{
string GetTransport();
}
}
namespace MamdaAdapter
{
public class Arca : IExchange
{
private const string _Transport = "tportname";
public string GetTransport()
{
return _Transport;
}
}
}
namespace MamdaAdapter
{
public class Exchange
{
public static IExchange DeriveExchange(string ExchangeName)
{
IExchange SelectedExchange = (IExchange)Assembly.GetAssembly(typeof(IExchange)).CreateInstance(ExchangeName, false, BindingFlags.CreateInstance, null, null, null, null);
return SelectedExchange;
}
}
}
If the Type you are looking for is not defined in the same assembly that is executing Type.GetType you must use the AssemblyQualifiedName (something like MyNamespace.MyClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089), even the FullName is not enough. Otherwise you could first get the assembly containing the class and then execute the GetType method of the Assembly class.

Categories