Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a dataTable called myResult which has tw columns Process and ParentProcess and I am trying to put this into an Ienumerable type so I can serialise it with Json.net.
Original post is here:
Now I am up to this point, I have this interface:
namespace myFirstCProgramIn2013
{
public interface Interface1 : IEnumerable
{
string Process { get; set; }
string ParentProcess { get; set; }
}
}
I have implemented it in this class:
public class myArray : Interface1
{
public string Process
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string ParentProcess
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public System.Collections.IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
I have then tried to use this class to assign myResult to it using the code below:
List<Interface1> recordList = new List<Interface1>();
if (myResult.Rows.Count > 0)
{
foreach (DataRow row in myResult.Rows)
{
var myArray = new myArray();
myArray.Process = Convert.ToString(row["Process"]);
myArray.ParentProcess = Convert.ToString(row["ParentProcess"]);
}
}
I get this error:
An exception of type 'System.NotImplementedException' occurred in
App_Web_hi4zxnmq.dll but was not handled in user code
which is thrown at the set clause of the Process Property of myArray Class.
Where am I going wrong with this?
Thanks.
you are throwing an NotInprementedException on the setter of the property. if you want automatic properties replace
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
with
get; set;
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 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 5 years ago.
Improve this question
How can I prevent the host application from crashing, due to any uncaught exceptions in the instances of a dynamically loaded external DLL?
Right now I have something like the following setup, please note that this is not the exact implementation:
internal class ExtensionManager
{
private static Type mExtension;
public static GetExtension() { return mExtension; }
ExtensionManager()
{
FileInfo[] fis = null;
fis = directory.GetFiles("*.dll");
foreach (FileInfo file in fis)
{
Assembly assembly = Assembly.LoadFrom(file.FullName);
if(some condition)
{
mExtension = Assembly.GetTypes().FirstOrDefault(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BaseExtension)));
break;
}
}
}
}
internal class Room
{
public Room()
{
StartExtensions();
}
private BaseExtension CreateInstance(Type classType)
{
BaseExtension instance = null;
try
{
instance = (BaseExtension)Activator.CreateInstance(classType, new object[] { this });
}
catch (Exception error)
{
LogException("[Room][CreateInstance()]", error);
}
return instance;
}
private void StartExtensions()
{
if (!mExtensionInitialised)
{
Type classType = ExtensionManager.GetExtension();
if (classType == null) return;
if (classType.BaseType != typeof(BaseExtension)) return;
Task<BaseExtension> task = Task<BaseExtension>.Factory.StartNew(() => CreateInstance(classType), TaskCreationOptions.LongRunning);
try
{
task.Wait();
}
catch (Exception error)
{
//
}
}
}
}
class Program
{
static List<Room> mRooms = new List<Room>();
static void Main(string[] args)
{
for(int i = 0; i < 100; i++)
{
Room room = new Room();
mRooms.Add(room);
}
}
}
So I need to figure out if there is a uncaught exception, at run-time in the instance created by BaseExtension CreateInstance(Type classType).
P.S .NET Plugin framework is not an option, at the moment for me.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What I'm wondering is what would be the best performance for determining what child class was actually passed into the function.
Here are the 3 different ways I am going between for what would be the best performance:
(ACHData, CCData, and DC Data all Inherit from BaseData)
public BaseProcessor GetProcessor(BaseData paymentInfo)
{
if (paymentInfo.GetType() == typeof(ACHData))
{
return GetACHProcessor((ACHData)paymentInfo);
}
else if (paymentInfo.GetType() == typeof(CCData))
{
return GetCCProcessor((CCData)paymentInfo);
}
else if (paymentInfo.GetType() == typeof(DCData))
{
return GetDCProcessor((DCData)paymentInfo);
}
else
{
throw new Exception(ExceptionMessage);
}
}
public BaseProcessor GetProcessor(BaseData paymentInfo)
{
if (paymentInfo is ACHData)
{
return GetACHProcessor((ACHData)paymentInfo);
}
else if (paymentInfo is CCData)
{
return GetCCProcessor((CCData)paymentInfo);
}
else if (paymentInfo is DCData)
{
return GetDCProcessor((DCData)paymentInfo);
}
else
{
throw new Exception(ExceptionMessage);
}
}
public BaseProcessor GetProcessor(BaseData paymentInfo)
{
var achInfo = paymentInfo as ACHData;
if (achInfo != null)
{
return GetACHProcessor(achInfo);
}
var ccInfo = paymentInfo as CCData;
if (ccInfo != null)
{
return GetCCProcessor(ccInfo);
}
var dcInfo = paymentInfo as DCData;
if (dcInfo != null)
{
return GetDCProcessor(dcInfo);
}
throw new Exception(ExceptionMessage);
}
I would re-think your entire design. Have your classes implement an interface that forces them to choose what to provide as a processor:
public interface IKnowWhatProcessorIWant {
IProcessor CreateProcessor();
}
public class ACHData : BaseData, IKnowWhatProcessorIWant {
public IProcessor CreateProcessor() {
return new GetACHProcessor(this);
}
}
Then your GetProcessor code becomes:
public BaseProcessor GetProcessor(IKnowWhatProcessorIWant obj) {
return obj.CreateProcessor();
}
...or, you could omit that method entirely then.
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
}
}