So I am adding/creating objects within Console class via an array:
string[] console_available = { "Yes", "Yes", "Yes", "Yes", "Yes" };
for (int i = 0; i < console_available .Length; i++)
{
Classes.Console console = new Classes.Console(console_available[i]);
}
In the class itself I have int console_id which I want to increment but for some reason it only increments it once with the below constructor:
public int ConsoleID { get; set; } = 0;
public string Available { get; set; }
public Console(string available)
{
ConsoleID++;
this.Available = available;
}
So in essence its all "1 Yes" where I need it to be "1 Yes", "2 Yes", "3 Yes".
I don't really want to go down the route of having multiple lines of code to create the objects, e.g.:
Classes.Console console = new Classes.Console("Yes");
Classes.Console console2 = new Classes.Console("Yes");
Classes.Console console3 = new Classes.Console("Yes");
You'll need a static variable that you increment and store in a member variable:
private static int _ID = 1;
public int ConsoleID { get; set; };
public string Available { get; set; }
public Console(string available)
{
ConsoleID = _ID++;
this.Available = available;
}
Related
I'm looping through some logic for a program I'm making that reads text through a .txt file and every time I get to the place where the algorithm adds a Class Object I created it works but then the next time it hits it the previous object gets its data changed to the object currently being added and so on.
Here is a Snippet of code for preface this is inside While loop and nested in 3 if statements.
Question: Why is it overwriting all the other entries?
My logic is 100% working I ran tests on it for over 10 hours with many breakpoints also please go easy on me I'm semi proficient at C#
if (Att == a1)
{
Student s1 = new Student();
s1.Eid = Eid;
s1.Name = Name;
s1.Attempt1 = att1;
AllStudents.Add(s1);
//AllStudents.Add(new Student(Eid,Name, att1));
Eid = line;
Att = "";
qnum = 1;
counter = 1;
}
Here is my Student class
public class Student
{
public string Eid { get; set; }
public string Name { get; set; }
public string[] Attempt1 { get; set; }
public string[] Attempt2 { get; set; }
public string[] Attempt3 { get; set; }
public string[] Att1filler = { "n/a", "n/a", "n/a", "n/a", "n/a", "n/a" };
public string[] Att2filler = {"n/a","n/a","n/a","n/a","n/a","n/a"};
public string[] Att3filler = {"n/a","n/a","n/a","n/a","n/a","n/a"};
public int FinalGrade { get; set; }
public int Grade1 { get; set; }
public int Grade2 { get; set; }
public int Grade3 { get; set; }
public int Grade4 { get; set; }
public Student()
{
FinalGrade = 0;
Attempt1 = Att1filler;
Attempt2 = Att2filler;
Attempt3 = Att3filler;
}
public Student(string Eagid, string name, string[] Att1)
{
Eid = Eagid;
Name = name;
Attempt1 = Att1;
Attempt2 = Att2filler;
Attempt3 = Att3filler;
FinalGrade = 0;
}
public Student(string Eagid, string name, string[] Att1, string[] Att2)
{
Eid = Eagid;
Name = name;
Attempt1 = Att1;
Attempt2 = Att2;
Attempt3 = Att3filler;
FinalGrade = 0;
}
public Student(string Eagid, string name, string[] Att1, string[] Att2, string[] Att3)
{
Eid = Eagid;
Name = name;
Attempt1 = Att1;
Attempt2 = Att2;
Attempt3 = Att3;
FinalGrade = 0;
}
}
And finally this is how I declared my List
public List<Student> AllStudents = new List<Student>();
also the AllStudents.add(new Student(Eid,Name, att1)); is from another solution i found that still did not work for me.
I figured it out. Learned my lesson of passing by references vs passing by value. make sure if your algorithm is looping that anything that is initialized by new and is used inside the loop is re-initialized inside the loop so you don't just pass the same reference for each object.(sorry if this answer isn't 100% I'm running on 2 hours of sleep trying to get this project done!)
I have the following object:
namespace BluetoothExample
{
public class Assay
{
public double Band_1 //Vis450
{
get;
set;
}
public double Band_2 //Vis500
{
get;
set;
}
public double Band_3 //Vis550
{
get;
set;
}
public double Band_4 //Vis570
{
get;
set;
}
}
}
I want to populate the 4 bands in my object. Which I currently do in the following way:
public Populate()
{
int _i = 0;
double[] nirData = new double[4];
MyDevice.Characteristic.ValueUpdated += (sender, e) =>
{
nirData[_i] = BitConverter.ToDouble(e.Characteristic.Value, 0);
_i++;
};
Assay assay = new Assay();
assay.Band_1 = nirData[0];
assay.Band_2 = nirData[1];
assay.Band_3 = nirData[2];
assay.Band_4 = nirData[3];
}
I was wondering if it was possible to do the entire thing inside the MyDevice.Characteristic.ValueUpdate method instead? My thought is that it should be possible to increment and populate the properties of my object like so:
string name = "assay.Band_" + _i;
name = BitConverter.ToDouble(e.Characteristic.Value, 0);
This is obviously wrong, but it sort of demonstrates my idea.
Just make your band an Array, or List:
public class Assay
{
public double[] Band
{
get;
set;
}
}
And then you can simply assign it like:
public Populate()
{
int _i = 0;
double[] nirData = new double[4];
MyDevice.Characteristic.ValueUpdated += (sender, e) =>
{
nirData[_i] = BitConverter.ToDouble(e.Characteristic.Value, 0);
_i++;
};
Assay assay = new Assay();
assay.Band = nirData;
}
I have a class "MsrProgram" that will serialize. However, if the parameter "Number" in "MsrProgram" is different, i need different parameters in my XML File. What is the easyest way to do somthing like this?
Here is my code:
public class MsrProgram
{
[XmlAttribute]
public string OwnerTypeFullName { get; set; }
[XmlAttribute]
public int Number { get; set; }
[XmlAttribute]
public int MsrRange { get; set; }
[XmlAttribute]
public int TurnoverMeasure { get; set; }
}
public class main
{
var toolList = (from pos in Configuration.List
select new Position
{
ToolNumber = (int)pos.tlno,
Tool =
{
ToolId = pos.tlno.ToString(),
Step =
{
Position = "1",
MsrProgram =
{
OwnerTypeFullName = "",
Number = 1,
MsrRange = "1", //When Number is 1
TurnoverMeasure = "1", //When Number is 2
}
}
}
}
}
Your code does not show everything so I can not give complete code, but this should get you going:
var toolList = (from pos in Configuration.List
select new Position
{
ToolNumber = (int)pos.tlno,
Tool = new Tool
{
ToolId = pos.tlno.ToString(),
Step = new Step
{
Position = "1",
MsrProgram = new MsrProgram
{
OwnerTypeFullName = "",
Number = GetNumber(), // <- fill in what really should be used
MsrRange = GetNumber() == 1 ? 1 : 0,
TurnoverMeasure = GetNumber() == 2 ? 1 : 0
}
}
}
}
);
I also added several new ... statements which you missed or forgot.
I need to have my second nested for loop send the array values to class friends.
I do not know how I would go about this?
Unless I missed something in the class?
namespace List
{
class Program
{
public const int ARRAYSIZE = 5;
static void Main()
{
string[] relSend = { "Enter name", "enter phone number", "enter 2 didigt month dob", "enter 2 digit day dob", "enter 2 digit dob year" };
string[] In = new string[5];
string[] answer = new string[10];
for (int x = 0; x <= 8; x++)
{
for (int i = 0; i < relSend.Length; i++)
{
WriteLine("{0}", relSend[i]);
In[i] = Console.ReadLine();
}
for (int i = 0; i < In.Length; i++)
{
}
}
}
}
}
public class Friends
{
public string Name { get; set; }
public int Phone { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
I guess you mean you want to create an object out of the information collected, that is no problem:
List<Friend> friends = new List<Friend>();
for (int x = 0; x <= 8; x++)
{
for (int i = 0; i < relSend.Length; i++)
{
WriteLine("{0}", relSend[i]);
In[i] = Console.ReadLine();
}
friends.Add( new Friend() { Name = In[0]
, Phone = int.Parse(In[1])
, Month = int.Parse(In[2])
, Day = int.Parse(In[3])
, Year = int.Parse(In[4])
}
);
}
Make sure to validate the input before creating the object! Also, I would suggest to use string for a phone number since you would lose the 0 that is the usual prefix. Month, Day and Year might be combined in a single DateTime.
Tell me if you need any explanations:
namespace List
{
class Program
{
//Create a dictionary to find out each question is realated to which property.
private static Dictionary<string, string> questions = new Dictionary<string, string>();
static void Main()
{
questions.Add("Enter name", "Name");
questions.Add("enter phone number", "Phone");
questions.Add("enter 2 didigt month dob", "Month");
questions.Add("enter 2 digit day dob", "Day");
questions.Add("enter 2 digit dob year", "Year");
//Create list of friends
List<Friends> friendsList = new List<Friends>();
for (int x = 0; x <= 8; x++)
{
Friends f = new Friends();
foreach (string q in questions.Keys)
{
Console.WriteLine("{0}", q);
//Find property using Sytem.Reflection
System.Reflection.PropertyInfo property = f.GetType().GetProperty(questions[q]);
//Set value of found property
property.SetValue(f, Convert.ChangeType(Console.ReadLine(), property.PropertyType), null);
}
//Add a friend to list
friendsList.Add(f);
}
}
}
}
public class Friends
{
public string Name { get; set; }
public int Phone { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
I'm trying to do it like bellow, but for next operations i would like to output an array that consist of 3-5 structs.
I would like to do it like : string[] my_array = {struct1,struct2,struct3}; but don't know how to do it correct.
public struct student
{
public string Name { get; set; }
public string Last_Name { get; set; }
public DateTime Birthday { get; set; }
public string Address { get; set; }
public string City { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
}
class H
{
static void Main(string[] args)
{
student student_info = new student();
student_info.Name = "Mike";
student_info.Last_Name = "Johnson";
student_info.Birthday = new DateTime(1983, 12, 03);
student_info.Address = "Baker str. 84/4a";
student_info.City = "New LM";
student_info.Zip = 90541;
student_info.Country = "Paris";
string[] my_array = { student_info.Name, student_info.Last_Name, student_info.Birthday.ToString(), student_info.Address, student_info.City, student_info.Zip.ToString(), student_info.Country };
for (int counter = 0; counter < my_array.Length; counter++)
{
Console.WriteLine(my_array[counter]);
}
}
}
I'm not completely sure I understand what you're doing. But here's my best guess.
If the objects will all be of the same struct, you can just use that.
student[] args = new [] { struct1, struct2, struct3 };
If they aren't the same type, the greatest common denominator between three structs like this will be object. So,
object[] args = new object[] { struct1, struct2, struct3 };
If you'd like, on the other hand, to combine the three structs into a single string array as you've shown us, that's a little different, and I can show you if you confirm that that is actually what you were looking for.
You can create an array of the struct.
student[] my_array = new student[] {struct1,struct2,struct3};