There is a method to read data from file
public static void ReadData(out StudentMarks[] Students, out int amount)
{
amount = 0;
Students = new StudentMarks[Max];
using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv"))
{
reader.ReadLine(); reader.ReadLine();
string line = null;
int[] marks;
marks = new int[Max];
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(',');
string surname = values[0];
string name = values[1];
string group = values[2];
int amountOfMarks = int.Parse(values[3]);
int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4;
while (amountOfMarks >= i)
{
if (yMin <= yMax)
{
marks[i] = int.Parse(values[yMin]);
yMin++;
}
i++;
}
StudentMarks MarksObj = new StudentMarks(surname, name, group, amountOfMarks, marks);
Students[amount++] = MarksObj;
}
}
}
There is a class:
class StudentMarks
{
public const int Max = 50;
public string Surname { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int AmountOfMarks { get; set; }
public int[] Marks { get; set; }
public StudentMarks(string surname, string name, string group, int amountOfMarks, int[] marks)
{
Surname = surname;
Name = name;
Group = group;
AmountOfMarks = amountOfMarks;
Marks = marks;
}
}
The thing is, I cannot read "Marks", because I dont know how to initialize array in Class. My method should be working fine, I just can't put marks[i] into Students[i].Marks[y]
I don't understand why you want this.
but do you want like it?
class StudentMarks
{
public const int Max = 50;
public string Surname { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public int[] Marks { get; private set; }
public StudentMarks(string surname, string name, string group, int amountOfMarks)
{
Surname = surname;
Name = name;
Group = group;
Marks = new int[amountOfMarks];
}
}
public static void ReadData(out StudentMarks[] Students, out int amount)
{
amount = 0;
Students = new StudentMarks[Max];
using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P3\\P3.2\\StudentsMarks.csv"))
{
reader.ReadLine(); reader.ReadLine();
string line = null;
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(',');
string surname = values[0];
string name = values[1];
string group = values[2];
int i = 0; int yMax = 3 + amountOfMarks;int yMin = 4;
Students[amount] = new StudentMarks(surname, name, group, amountOfMark);
while (amountOfMarks >= i)
{
if (yMin <= yMax)
{
Students[amount].Marks[i] = int.Parse(values[yMin]);
yMin++;
}
i++;
}
amount++;
}
}
}
Related
I have an MVC that takes values from a form, sends them to a dll, to have logic performed on them & assigned to a model. The model is then returned to the view. In localhost it is successful and I get the following result:
However when I publish the webapp to an azure instance, it doesn't return any of the values. You can see it here: http://levelupsite.azurewebsites.net/ or see the image here
Does anyone know what could cause this? I will attach the code below:
Controller:
[HttpPost]
public ActionResult CBC(System.Web.Mvc.FormCollection form, FFAC model)
{
//recieve values from form
var email = escapeCharactersspace((form["email"].ToString()));
var Gender = Convert.ToString(model.UserGender);
var UserID = escapeCharactersspace((form["username"].ToString()));
var Neutrophils = escapeCharactersspace(form["Neutrophils"]);
var Lymphocytes = escapeCharactersspace(form["Lympthocytes"]);
var Monocytes = escapeCharactersspace(form["Monocytes"]);
var Eosinophils = escapeCharactersspace(form["Eosinophils"]);
var Basophils = escapeCharactersspace(form["Basophils"]);
var Platelets = (escapeCharactersspace(form["Platelets"]));
var Haematocrit = escapeCharactersspace(form["Haematocrit"]);
var Haemoglobin = escapeCharactersspace(form["Haemoglobin"]);
var MCV = escapeCharactersspace(form["MCV"]);
var MCH = (escapeCharactersspace(form["MCH"]));
var MCHC = escapeCharactersspace(form["MCHC"]);
//turn form to array
decimal[] cbcInputs = { Convert.ToDecimal(Neutrophils), Convert.ToDecimal(Lymphocytes), Convert.ToDecimal(Monocytes), Convert.ToDecimal(Eosinophils), Convert.ToDecimal(Basophils), Convert.ToDecimal(Platelets), Convert.ToDecimal(Haematocrit), Convert.ToDecimal(Haemoglobin), Convert.ToDecimal(MCV), Convert.ToDecimal(MCH), Convert.ToDecimal(MCHC) };
//create instance of model
var scfiae = new FIAECBC();
//create instance of external class library
var fiae = new FIAEngine();
//send inputs & model instance to external class library to perform logic
fiae.setcbcvals(cbcInputs, UserID, Gender, scfiae);
//return the get results page
return View("GetResults", scfiae);
}
FIAEngine - dll that performs the logic
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharedComponents;
using static SharedComponents.FIAECBC;
namespace FIAE
{
public class FIAEngine
{
decimal d_Neutrophils;
decimal d_Lymphocytes;
decimal d_Monocytes;
decimal d_Eosinophils;
decimal d_Basophils;
decimal d_Platelets;
decimal d_Haematocrit;
decimal d_Haemoglobin;
decimal d_MCV;
decimal d_MCH;
decimal d_MCHC;
string UserID;
string Gender;
string Neutrophils;
string Lymphocytes;
string Monocytes;
string Eosinophils;
string Basophils;
string Platelets;
string Haematocrit;
string Haemoglobin;
string MCV;
string MCH;
string MCHC;
string rag_Neutrophils;
string rag_Lymphocytes;
string rag_Monocytes;
string rag_Eosinophils;
string rag_Basophils;
string rag_Platelets;
string rag_Haematocrit;
string rag_Haemoglobin;
string rag_MCV;
string rag_MCH;
string rag_MCHC;
public void Setcbcfilevals(FIAECBC cbcmodel)
{
}
public void setcbcvals(decimal[] inputs, string _UserID, string _Gender, FIAECBC cbcmodel)
{
d_Neutrophils = inputs[0];
d_Lymphocytes = inputs[1];
d_Monocytes = inputs[2];
d_Eosinophils = inputs[3];
d_Basophils = inputs[4];
d_Platelets = inputs[5];
d_Haematocrit = inputs[6];
d_Haemoglobin = inputs[7];
d_MCV = inputs[8];
d_MCH = inputs[9];
d_MCHC = inputs[10];
UserID = _UserID;
Gender = _Gender;
Neutrophils = "Neutrophils";
Lymphocytes = "Lymphocytes";
Monocytes = "Monocytes";
Eosinophils = "Eosinophils";
Basophils = "Basophils";
Platelets = "Platelets";
Haematocrit = "Haematocrit";
Haemoglobin = "Haemoglobin";
MCV = "MCV";
MCH = "MCH";
MCHC = "MCHC";
rag_Neutrophils = "i";
rag_Lymphocytes = "i";
rag_Monocytes = "i";
rag_Eosinophils = "i";
rag_Basophils = "i";
rag_Platelets = "i";
rag_Haematocrit = "i";
rag_Haemoglobin = "i";
rag_MCV = "i";
rag_MCH = "i";
rag_MCHC = "i";
try
{
//male calculations
if (Gender == "Male")
{
rag_Neutrophils = FindMaleRAG(d_Neutrophils, 1);
rag_Lymphocytes = FindMaleRAG(d_Lymphocytes, 2);
rag_Monocytes = FindMaleRAG(d_Monocytes, 3);
rag_Eosinophils = FindMaleRAG(d_Eosinophils, 4);
rag_Basophils = FindfeMaleRAG(d_Basophils, 5);
rag_Platelets = FindMaleRAG(d_Platelets, 6);
rag_Haematocrit = FindMaleRAG(d_Haematocrit, 7);
rag_Haemoglobin = FindMaleRAG(d_Haemoglobin, 8);
rag_MCV = FindMaleRAG(d_MCV, 9);
rag_MCH = FindMaleRAG(d_MCH, 10);
rag_MCHC = FindMaleRAG(d_MCHC, 11);
//set view model values to the form values
cbcmodel.d_Neutrophils = d_Neutrophils;
cbcmodel.d_Lymphocytes = d_Lymphocytes;
cbcmodel.d_Monocytes = d_Monocytes;
cbcmodel.d_Eosinophils = d_Eosinophils;
cbcmodel.d_Basophils = d_Basophils;
cbcmodel.d_Platelets = d_Platelets;
cbcmodel.d_Haematocrit = d_Haematocrit;
cbcmodel.d_Haemoglobin = d_Haemoglobin;
cbcmodel.d_MCV = d_MCV;
cbcmodel.d_MCH = d_MCH;
cbcmodel.d_MCHC = d_MCHC;
cbcmodel.Neutrophils = Neutrophils;
cbcmodel.Lymphocytes = Lymphocytes;
cbcmodel.Monocytes = Monocytes;
cbcmodel.Eosinophils = Eosinophils;
cbcmodel.Basophils = Basophils;
cbcmodel.Platelets = Platelets;
cbcmodel.Haematocrit = Haematocrit;
cbcmodel.Haemoglobin = Haemoglobin;
cbcmodel.MCV = MCV;
cbcmodel.MCH = MCH;
cbcmodel.MCHC = MCHC;
cbcmodel.rag_Neutrophils = rag_Neutrophils;
cbcmodel.rag_Lymphocytes = rag_Lymphocytes;
cbcmodel.rag_Monocytes = rag_Monocytes;
cbcmodel.rag_Eosinophils = rag_Eosinophils;
cbcmodel.rag_Basophils = rag_Basophils;
cbcmodel.rag_Platelets = rag_Platelets;
cbcmodel.rag_Haematocrit = rag_Haematocrit;
cbcmodel.rag_Haemoglobin = rag_Haematocrit;
cbcmodel.rag_MCV = rag_MCV;
cbcmodel.rag_MCH = rag_MCH;
cbcmodel.rag_MCHC = rag_MCHC;
}
else if (Gender == "Female")
{
rag_Neutrophils = FindfeMaleRAG(d_Neutrophils, 1);
rag_Lymphocytes = FindfeMaleRAG(d_Lymphocytes, 2);
rag_Monocytes = FindfeMaleRAG(d_Monocytes, 3);
rag_Eosinophils = FindfeMaleRAG(d_Eosinophils, 4);
rag_Basophils = FindfeMaleRAG(d_Basophils, 5);
rag_Platelets = FindfeMaleRAG(d_Platelets, 6);
rag_Haematocrit = FindfeMaleRAG(d_Haematocrit, 7);
rag_Haemoglobin = FindfeMaleRAG(d_Haemoglobin, 8);
rag_MCV = FindfeMaleRAG(d_MCV, 9);
rag_MCH = FindfeMaleRAG(d_MCH, 10);
rag_MCHC = FindfeMaleRAG(d_MCHC, 11);
//set view model values to the form values
cbcmodel.d_Neutrophils = d_Neutrophils;
cbcmodel.d_Lymphocytes = d_Lymphocytes;
cbcmodel.d_Monocytes = d_Monocytes;
cbcmodel.d_Eosinophils = d_Eosinophils;
cbcmodel.d_Basophils = d_Basophils;
cbcmodel.d_Platelets = d_Platelets;
cbcmodel.d_Haematocrit = d_Haematocrit;
cbcmodel.d_Haemoglobin = d_Haemoglobin;
cbcmodel.d_MCV = d_MCV;
cbcmodel.d_MCH = d_MCH;
cbcmodel.d_MCHC = d_MCHC;
cbcmodel.Neutrophils = Neutrophils;
cbcmodel.Lymphocytes = Lymphocytes;
cbcmodel.Monocytes = Monocytes;
cbcmodel.Eosinophils = Eosinophils;
cbcmodel.Basophils = Basophils;
cbcmodel.Platelets = Platelets;
cbcmodel.Haematocrit = Haematocrit;
cbcmodel.Haemoglobin = Haemoglobin;
cbcmodel.MCV = MCV;
cbcmodel.MCH = MCH;
cbcmodel.MCHC = MCHC;
cbcmodel.rag_Neutrophils = rag_Neutrophils;
cbcmodel.rag_Lymphocytes = rag_Lymphocytes;
cbcmodel.rag_Monocytes = rag_Monocytes;
cbcmodel.rag_Eosinophils = rag_Eosinophils;
cbcmodel.rag_Basophils = rag_Basophils;
cbcmodel.rag_Platelets = rag_Platelets;
cbcmodel.rag_Haematocrit = rag_Haematocrit;
cbcmodel.rag_Haemoglobin = rag_Haematocrit;
cbcmodel.rag_MCV = rag_MCV;
cbcmodel.rag_MCH = rag_MCH;
cbcmodel.rag_MCHC = rag_MCHC;
}
}
catch (Exception ex)
{
}
//return inputs;
}
public string FindMaleRAG(decimal i, int x)
{
String connString = Convert.ToString(ConfigurationManager.ConnectionStrings["SQLServerCon"]);
//for each row, do this
//find the threshld values
String thresholdquery = #"select * from dbo.malethreshold where ID = " + x;
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(thresholdquery, conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//compare threshold values t posted values from form
string composite = Convert.ToString(reader[1]);
decimal redlow = Convert.ToDecimal(reader[2]);
decimal greenlow = Convert.ToDecimal(reader[3]);
decimal greenhigh = Convert.ToDecimal(reader[4]);
decimal redhigh = Convert.ToDecimal(reader[5]);
if (i < redlow)
{
// Red low
return ("red");
}
else if (i > redlow && i < greenlow)
{
// Amber Low
return ("orange");
}
else if (i >= greenlow && i <= greenhigh)
{
//green
return ("green");
}
else if (i > greenhigh && i < redhigh)
{
//amber high
return ("orange");
}
else if (i > redhigh)
{
// Redhigh
return ("red");
}
else
{
//sorting error
return ("error in sorting");
}
}
}
}
}
return ("sorting error");
}
public string FindfeMaleRAG(decimal i, int x)
{
String connString = Convert.ToString(ConfigurationManager.ConnectionStrings["SQLServerCon"]);
//for each row, do this
//find the threshld values
String thresholdquery = #"select * from dbo.malethreshold where ID = " + x;
using (var conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(thresholdquery, conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//compare threshold values t posted values from form
string composite = Convert.ToString(reader[1]);
decimal redlow = Convert.ToDecimal(reader[2]);
decimal greenlow = Convert.ToDecimal(reader[3]);
decimal greenhigh = Convert.ToDecimal(reader[4]);
decimal redhigh = Convert.ToDecimal(reader[5]);
if (i < redlow)
{
// Red low
return ("red");
}
else if (i > redlow && i < greenlow)
{
// Amber Low
return ("orange");
}
else if (i >= greenlow && i <= greenhigh)
{
//green
return ("green");
}
else if (i > greenhigh && i < redhigh)
{
//amber high
return ("orange");
}
else if (i > redhigh)
{
// Redhigh
return ("red");
}
else
{
//sorting error
return ("error in sorting");
}
}
}
}
}
return ("sorting error");
}
}
}
FIAECBC model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace SharedComponents
{
public class FIAECBC
{
public string Neutrophils { get; set; }
public string Lymphocytes { get; set; }
public string Monocytes { get; set; }
public string Eosinophils { get; set; }
public string Basophils { get; set; }
public string Platelets { get; set; }
public string Haematocrit { get; set; }
public string Haemoglobin { get; set; }
public string MCV { get; set; }
public string MCH { get; set; }
public string MCHC { get; set; }
public decimal d_Neutrophils { get; set; }
public decimal d_Lymphocytes { get; set; }
public decimal d_Monocytes { get; set; }
public decimal d_Eosinophils { get; set; }
public decimal d_Basophils { get; set; }
public decimal d_Platelets { get; set; }
public decimal d_Haematocrit { get; set; }
public decimal d_Haemoglobin { get; set; }
public decimal d_MCV { get; set; }
public decimal d_MCH { get; set; }
public decimal d_MCHC { get; set; }
public string rag_Neutrophils { get; set; }
public string rag_Lymphocytes { get; set; }
public string rag_Monocytes { get; set; }
public string rag_Eosinophils { get; set; }
public string rag_Basophils { get; set; }
public string rag_Platelets { get; set; }
public string rag_Haematocrit { get; set; }
public string rag_Haemoglobin { get; set; }
public string rag_MCV { get; set; }
public string rag_MCH { get; set; }
public string rag_MCHC { get; set; }
public Gender UserGender { get; set; }
public static IEnumerable<SelectListItem> GetSelectItems()
{
yield return new SelectListItem { Text = "Male", Value = "Male" };
yield return new SelectListItem { Text = "Female", Value = "Female" };
}
public bool Bookkeeping { get; set; }
}
public enum Gender
{
Male,
Female
}
}
I have a program in C# to read data from ".csv" and display in datagrid view. Main code for reading and displaying is shown below. It is executed upon button press.
StreamReader sr = null;
List<LogList> loglist = new List<LogList>();
BindingList<LogList> bindLogList;
BindingSource bLogsource;
dLogView.DataSource = null;
dLogView.Rows.Clear();
dLogView.Columns.Clear();
{
// code to add data to custom list
// the list has 12 fields.
sr = new StreamReader("C:\\VSWR_T&M\\Log\\20200423.csv");
int count = 0;
// skip the first line read containing header, save from second line onwards.
StringBuilder readLine = new StringBuilder(sr.ReadLine());
if (readLine.ToString() != null && readLine.ToString() != "")
{
readLine = new StringBuilder(sr.ReadLine());
while (readLine.ToString() != null && readLine.ToString() != "")
{
string[] subdata = readLine.ToString().Split(',');
count = subdata.Length;
LogList ll = new LogList(subdata[0], subdata[1], subdata[2], subdata[3], subdata[4], subdata[5], subdata[6], subdata[7], subdata[8], subdata[9], subdata[10], subdata[11]);
loglist.Add(ll);
readLine = new StringBuilder(sr.ReadLine());
}
}
}
bindLogList = new BindingList<LogList>(loglist);
bLogsource = new BindingSource(bindLogList, null);
dLogView.AutoGenerateColumns = true;
dLogView.DataSource = bLogsource;
dLogView.Columns[0].Width = 100;
dLogView.Columns[1].Width = 120;
dLogView.Columns[2].Width = 50;
dLogView.Columns[3].Width = 50;
dLogView.Columns[4].Width = 50;
dLogView.Columns[5].Width = 50;
dLogView.Columns[6].Width = 50;
dLogView.Columns[7].Width = 50;
dLogView.Columns[8].Width = 50;
dLogView.Columns[9].Width = 50;
dLogView.Columns[10].Width = 50;
dLogView.Columns[11].Width = 50;
The LogList class is as follows:
class LogList
{
string project { get; set; }
string date_time { get; set; }
string Qty { get; set; }
string Pass { get; set; }
string Fail { get; set; }
string Result { get; set; }
string ANT2_1 { get; set; }
string ANT2_2 { get; set; }
string ANT2_3 { get; set; }
string ANT3_1 { get; set; }
string ANT3_2 { get; set; }
string ANT3_3 { get; set; }
public LogList(string project, string date_time, string Qty, string Pass, string Fail, string Result, string ANT2_1, string ANT2_2,
string ANT2_3, string ANT3_1, string ANT3_2, string ANT3_3)
{
this.project = project;
this.date_time = date_time;
this.Qty = Qty;
this.Pass = Pass;
this.Fail = Fail;
this.Result = Result;
this.ANT2_1 = ANT2_1;
this.ANT2_2 = ANT2_2;
this.ANT2_3 = ANT2_3;
this.ANT3_1 = ANT3_1;
this.ANT3_2 = ANT3_2;
this.ANT3_3 = ANT3_3;
}
}
When I run this code, I get exception message which says
"Index was out of range. Must be non-negative and less than the size of the collection.
parameter name: Index"
Exception is generated from line
dLogView.Columns[1].Width = 120;
It seems the columns are not added. Am I missing some column property here ? I would like to know why the columns are not added ?
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!)
Can someone look at my code? How do I count the grade while looping, or can I take the line from StreamWriter and put substring?
I keep making it more and more messy... and I need to display it like
Student that score A got how many?
Student that got B got how many?>
student that got C how many>
student that got d ... f...
using (StreamReader reader = new StreamReader("Student.txt"))
{
StreamWriter output = new StreamWriter("Result.txt");
String line;
line = reader.ReadLine();
while (line != null)
{
String name = line.Substring(0, 9);
String Asg1 = line.Substring(10, 3);
String Asg2 = line.Substring(16, 3);
String Test = line.Substring(22, 3);
String Quiz = line.Substring(28, 3);
String Exam = line.Substring(34, 3);
int intAsg1 = int.Parse(Asg1);
int intAsg2 = int.Parse(Asg2);
int intTest = int.Parse(Test);
int intQuiz = int.Parse(Quiz);
int intExam = int.Parse(Exam);
int percentAsg1 = (intAsg1 * 25) / 100;
int percentAsg2 = (intAsg2 * 25) / 100;
int percentTest = (intTest * 10) / 100;
int percentQuiz = (intQuiz * 10) / 100;
int percentExam = (intExam * 30) / 100;
// this part i dont get it~
**String Grade;
if (overallScore >= 70)
{
Grade = "A";
}
else if (overallScore >= 60)
{
Grade = "B";
}
else if (overallScore >= 50)
{
Grade = "C";
}
else if (overallScore >= 40)
{
Grade = "D";
}
else
{
Grade = "F";
}
}
}
It's not looping.
while !reader.EndOfStream
{
/*now check each Readline.
parse each line and create a new grade object for
each iteration and add to collection*/
}
A custom class to store the data for each student:
public class grade
{
public int asg1{ get; set; }
public int asg2{ get; set; }
public int test{ get; set; }
public int quiz{ get; set; }
public int exam{ get; set; }
//whatever else you need
}
A collection of Grades:
var grades = New List<grade>
I think you are looking for a class to hold the results in addition to correcting the readline loop.
public class Student
{
public string Name { get; set; }
public List<Grade> Grades { get; set; }
}
Then create a list before you start the loop
var students = new List<Student>();
Then add to the list in each iteration
In Form GroupExmStart I am calling Foo method from Question class and storing all Questions in var questions and passing it to int Quiz method,how can I store this question in string[] so that i can Display My questions using DisplayQuestions(),I tried to convert it into string using string[] ab=questions.ToArray(); but it is not working , Is that conversion is not possible or Where i am going wrong ?
Form GroupExmStart
public partial class GroupExmStart : Form
{
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
Question qsn = new Question();
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
var questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
int count = 0;
timer1.Interval = Convert.ToInt16(conf[1]) * 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
int Quiz(List<Question> questions)
{
var str = questions.ToArray();
foreach (var item in str)//I am not getting how do i do it Getting `item as namespaceName.Question`
{
}
return 0;
}
private void DisplayQuestion(string id, string Q, string OP1, string OP2, string OP3, string OP4)
{
label5.Text = Q;
radioButton12.Text = OP4;
radioButton11.Text = OP4;
radioButton10.Text = OP4;
radioButton9.Text = OP4;
}
}
}
Class Question
public class Question
{
public string Id { get; set; }
public string Text { get; set; }
public string Option1 { get; set; }
public string Option2 { get; set; }
public string Option3 { get; set; }
public string Option4 { get; set; }
public string AnswerOption { get; set; }
public int Marks { get; set; }
Random _random = new Random();
public IEnumerable<Question> GetQuestions(string topicId, int marks)
{
string sql = "select QID,Question,Opt1,Opt2,Opt3,Opt4,AnsOp,Marks from Questions where TopicID IN(" +
topicId + ") and Marks=" + marks;
var cmd = new OleDbCommand(sql,acccon);
var rs = cmd.ExecuteReader();
if (rs != null)
{
while (rs.Read())
{
yield return
new Question
{
Id = rs[0].ToString() + "~",
Text = rs[1].ToString() + "~",
Option1 = rs[2].ToString() + "~",
Option2 = rs[3].ToString() + "~",
Option3 = rs[4].ToString() + "~",
Option4 = rs[5].ToString() + "~",
AnswerOption = rs[6].ToString() + "~",
Marks = marks
};
}
}
}
public List<Question> Foo(string TopicId, string[] conf)
{
var totQsn = Convert.ToInt16(conf[0]);
var mark1qsn = Convert.ToInt16(conf[3]); //this variable contains number of question to be display of mark 1
var mark2qsn = Convert.ToInt16(conf[4]);
var mark3qsn = Convert.ToInt16(conf[5]);
var mark4qsn = Convert.ToInt16(conf[6]);
var mark1questionSet = GetQuestions(TopicId, 1).ToList();
var mark2questionSet = GetQuestions(TopicId, 2).ToList();
var finalQuestions = new List<Question>();
for (int i = 0; i < mark1qsn; i++)
{
var setIndex = _random.Next(mark1questionSet.Count);
finalQuestions.Add(mark1questionSet[setIndex]);
mark1questionSet.RemoveAt(setIndex);
}
for (int i = 0; i < mark2qsn; i++)
{
var setIndex = _random.Next(mark2questionSet.Count);
finalQuestions.Add(mark2questionSet[setIndex]);
mark2questionSet.RemoveAt(setIndex);
}
return finalQuestions;
}
}
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
// do something with question
// question.Id, question.Text etc.. can access from here
}
return 0;
}
if you need to display the question you better override the ToString method of Question class as below
public class Question
{
public override string ToString()
{
// you can change this as you wish
return string.Formt("Id:{0}, Text :{1}", Id, Text);
}
then you can try below
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
MessageBox.Show(question.ToString());
}
return 0;
}
Or you can use existing method as below with few modifications
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
private void DisplayQuestion(Question question)
{
label5.Text = question.Text;
radioButton12.Text = question.Option1;
radioButton11.Text = question.Option2;
radioButton10.Text = question.Option3;
radioButton9.Text = question.Option4;
}
Your problem is that
var str = questions.ToArray();
returns an array of type Question
Question[]
and not an array of type string.
You will have to create an overload for Question to output to string what you need.
Something like
public class Question
{
public override string ToString()
{
return Text;
}
....
This should allow you to do something like
foreach (var item in str)
{
string itemText = item.ToString();
}
Try this,
string _que = string.Empty;
foreach (var item in questions)
{
_que =Convert.toString(item.Text);
}
return 0;
Since Question is class and Text is one of its property so u can access it by its object.