put the value inside the array inside a class - c#

I have a following class:
public class test100
{
public string dateofbirth{ get; set; }
public string phonenumber{ get; set; }
public Name[] names { get; set; }
}
I want to assign value to names and then add that value in test100 class. I don't want to create the list. I just want to do this with an array. I want to put the values like this:
names[0] = {firstName="test1", MiddleName="test2", lastName="test3"}
and then add names[0] to test100 class.
How can I achieve this.

You can initialize the array while initializing the class itself
var data = new test100
{
dateofbirth = "22-03-1997",
phonenumber = "1-223344",
names = new Name[]
{
new Name {firstName="test1", MiddleName="test2", lastName="test3"}
}
}
Problem with this is that you haven't defined any size for your names array. So you can create instance of your class, define the size of that array and then insert the object to that array
var data = new test100();
data.names = new Name[10];
Name name = new Name {firstName="test1", MiddleName="test2", lastName="test3"};
data.names[0] = name;

Related

Class including list

when i am assigning the values in the list its saying object not set to instance of an object any help , i can retrieve from other service , when i am trying to assign the value in the list its creating problem
public class exam
{
public string Name { get; set; }
public string Age { get; set; }
public string RollNo { get; set; }
public List<Subject> subject{get; set;}
}
public class Subject
{
public string SubjectName{ get; set;}
public string SubjectPartNo{ get; set; }
}
exam ex = new exam()
ex.Name = "john"
ex.Age = 22
ex.RollNo = 13
ex.subject.SubjectName = "English"
ex.subject.SubjectPartNo = 1
subject is an object which is defined as a list of type Subject. Like every other object this needs to first be instantiated before it can be used. The fact that it is inside Exam (another object) doesn't change this.
So you can do this either during property assignment like this.
public List<Subject> subject { get; set; } = new List<Subject>();
or you can create a constructor and instantiate it there
public exam()
{
subject = new List<Subject>();
}
So now you have an empty list, that's the first part of the problem resolved. But you are still trying to assign values to it incorrectly.
subject is a list of Subject, so this means we can only add Subject objects to it.
First create a new Subject object and add values to it. (can also be done inline). Here we create two;
Subject sub1 = new Subject();
sub1.SubjectName = "English";
sub1.SubjectPartNo = "1";
Subject sub2 = new Subject();
sub2.SubjectName = "Maths";
sub2.SubjectPartNo = "2";
Then add these to your list;
exam ex = new exam();
ex.Name = "john"
ex.Age = "22";
ex.RollNo = "13";
ex.subject.Add(sub1);
ex.subject.Add(sub2);
Now you have an exam object with 2 Subjects
BTW: You have many properties, such as age, which are defined as string but you are trying to add integer values to them. They need to be correctly defined according to the right datatype. Don't just set everything to string because it is "easier" now, it will cause you problems later.

How initializing List that is in class in another C# class to fill the list

I have this class in C#:
public class Init
{
public string First_name { get; set; }.
public List<LeadLocations> Locations { get; set; }
}
public Init()
{
this.Locations = new List<LeadLocations>();
}
public class LeadLocations
{
public string City { get; set; }
public string State { get; set; }
public string County { get; set; }
}
I need to fill Locations with values that I have in another list like:
foreach (var item2 in AreaOfInterest.AreaOfInterests)
{
foreach (var item in transactionInit.Locations)
{
item.City = item2.City;
item.County = item2.County;
}
}
but it never goes to second foreach as it is empty. How I can initialize it to new object, anything I try is not working.
If you are trying to add location to a list of locations, you need to create a new location object (based on the AreaOfInterests objects) and then add that to the list of locations.
// Initialize your Locations if its not already initialized.
// If its not initialized, you will get Object Reference Not Set to Instance of an Object.
transactionInit.Locations = new List<LeadLocations>();
foreach (var item2 in AreaOfInterest.AreaOfInterests)
{
// For Each item2, create a new location then insert it in transactionInit.Locations.
var leadLocation = new LeadLocation() {City = item2.City, County = item2.County};
transactionInit.Locations.Add(leadLocation);
}
I believe I understood what you are intending to do. You could do the following.
transactionInit.Locations.AddRange(AreaOfInterest.AreaOfInterests
.Select(x=> new Location
{
City = x.City,
County=x.County
});
In the above code, you are iterating over the AreaOfInterest.AreaOfInterests to create instances of Location and using the List.AddRange(IEnumerable<T>) to add them to the Locations property of transactionInit.

an array list with n rows and four columns in c#

I need a clear example that shows me how to define a list that has n rows and 4 columns and how to use it. I need a list to save my data like the below image. as you see this could be a dictionary.
You need to create a class with all the above properties
public class Sample
{
public string vocabulary { get; set; }
public string meaning { get; set; }
public int number { get; set; }
public int group { get; set; }
}
and then you can create a List of type Sample,
List<Sample> yourList = new List<Sample>();
You can add items to the list as below
yourList.Add(new Sample { vocabulary = "massive", meaning = "very big", number = 5, group = 15 });
You can access them later like this, if you want the first element,
var result = yourList[0];
this is the easiest and best way of doing it. You need to create a new class and then create new instances of the class and then add it to the list and then use LINQ to get the data out
void Main()
{
var list = new List<myClass>()
list.Add(new myClass() {
Vocabluary = "Vocabluary ",
Meaning = "meaning",
Number = 1,
Group = 2})
}
public class myClass
{
public string Vocabluary { get; set; }
public string Meaning { get; set; }
public int Number { get; set; }
public int Group { get; set; }
}
yes... as Sajeetharan mentioned, with a custom class you can create an any dimensions List. but i don't think you need to think about dimension in C#... it is a bit more high level than that.
just simply create a class and put everything you need in it...
public class CustomClass{
public string d1;
public int d2;
public string d3;
public string d4;
...
//you can easily create a N dimension class
}
to access it and apply it
public void Main(){
List<CustomClass> list = new List<CustomClass>();
CustomClass cc = new CustomClass();
cc.d1 = "v1";
cc.d2 = 0; //v2
list.Add(cc);
//to access it
foreach(CustomClass tmpClass in list)
{
string d1Value = tmpClass.d1;
int d2Value = tmpClass.d2;
}
}

how to put the list data obtained from database to another variable in c# mvc using petapoco

I have obtained the list of data from database in the following way
List<MakerCheckerModel> mkckdata = new List<MakerCheckerModel>();
var dataContext = new PetaPoco.Database("MessageEntity");
mkckdata = dataContext.Query<MakerCheckerModel>(PetaPoco.Sql.Builder.Append("Select * from MakerChecker1")).ToList();
The data comes in mkckdata. My model is of the following way.
public class MakerCheckerModel
{
public int MakerCheckerId { get; set; }
public string OldJson { get; set; }
public string NewJson { get; set; }
public string ModelName { get; set; }
}
Now I want to put the value obtained in OldJson and NewJson of mkckdata in new List type of model variables so that I can manipulate it further.I want something like this.
List<MakerCheckerModel> oldDataList = new List<MakerCheckerModel>();
oldDataList.Add(mkckdata.OldJson));
But this is not allowed here. PLease help me how to do this.

Convert one set of objects to another set of objects of different class in single line. C#

I have 2 classes with different properties in each. Also I have a collection of one set of objects of the class A. Now I want to copy these to an array of objects of Class B.
The 2 classes are not inter related and also the fields are different in each. SO i have to explicitly map the fields i want to copy. Right now I am using a foreach to copy individual element. Is there a shorter way to accomplish this.
This is the class B
public class Event
{
public string EventOriginTime { get; set; }
public string EventReceivedTime { get; set; }
public int EventCode { get; set; }
public string CardNumber { get; set; }
public string ReaderName { get; set; }
}
First class A also will appear something like this but that is a 3rd party class.
Current solution I have is:
List<Event> listOfEvents = new List<Event>();
foreach (var eachEvent in eventsFromArgus)
{
listOfEvents.Add( new Event
{
ReaderName = eachEvent.DeviceName,
EventCode = eachEvent.EventCode,
EventReceivedTime = eachEvent.ReceiveTime.ToString(),
EventOriginTime = eachEvent.OriginTime.ToString(),
CardNumber = eachEvent.CredentialIdentifier
});
}
You could use LINQ:
List<event> listOfEvents =
(from eachEvent in eventsFromArgus
select new Event(
ReaderName = eachEvent.DeviceName,
EventCode = eachEvent.EventCode,
EventReceivedTime = eachEvent.ReceiveTime.ToString(),
EventOriginTime = eachEvent.OriginTime.ToString(),
CardNumber = eachEvent.CredentialIdentifier)).ToList();
But that's not terribly different from what you already have.
Or, you could look into something like AutoMapper.
Another approach is to pass the third party object directly into the Event class' constructor:
public class Event
{
private readonly ThirdPartyClass _eventFromArgus;
public Event(ThirdPartyClass eventFromArgus)
{
_eventFromArgus = eventFromArgus;
}
public string ReaderName { get { return _eventFromArgus.DeviceName; } }
// etc.
}
Then you can just do this:
var listOfEvents = eventsFromArgus.Select(eachEvent => new Event(eachEvent));
also with linq as Jim suggested but a bit different
var listOfEvents = eventsFromArgus.Select(eachEvent =>
new Event( ReaderName = eachEvent.DeviceName,
EventCode = eachEvent.EventCode,
EventReceivedTime = eachEvent.ReceiveTime.ToString(),
EventOriginTime = eachEvent.OriginTime.ToString(),
CardNumber = eachEvent.CredentialIdentifier)).ToList();
You could add a constructor to the Event class:
public Event(string EventOriginTime, string EventReceivedTime, int EventCode, string CardNumber, string ReaderName)
{
this.EventOriginTime = EventOriginTime;
this.EventReceivedTime = EventReceivedTime;
this.EventCode = EventCode;
this.CardNumber = CardNumber;
this.ReaderName = ReaderName;
}
Then at least you don't have to specify the field names when creating a new instance.
List<Event> listOfEvents = new List<Event>();
foreach (var eachEvent in eventsFromArgus)
{
listOfEvents.Add(new Event(eachEvent.OriginTime.ToString(), eachEvent.ReceiveTime.ToString(), eachEvent.EventCode, eachEvent.DeviceName)
}

Categories