|
I assume you've put the assignment into the class-scope, like so (15th line or so):
Public class Course
{
private string courseName;
public string CourseName
{
get { return courseName; }
set { courseName = value; }
}
Student[] studentarray = new Student[3];
studentarray[0] = student1;
public void SomeMethod()
{
studentarray[0] = student1;
}
public void SetStudent0(Student student)
{
studentarray[0] = student;
}
public void SetStudent(Student student, int index)
{
studentarray[index] = student;
}
private int ArrayCursor = 0;
public void AddStudent(Student student)
{
studentarray[ArrayCursor] = student;
ArrayCursor++;
}
private List<Student> Students = new List<Student>();
public void AddStudent(Student student)
{
Students.Add(student);
}
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Assignment5
{
class Program
{
static void Main(string[] args)
{
// Create 3 students
Student student1 = new Student
{
FirstName = "John",
LastName = "Wayne",
BirthDate = "26/05/1907"
};
Student student2 = new Student
{
FirstName = "Craig",
LastName = "Playstead",
BirthDate ="01/01/1967"
};
Student student3 = new Student
{
FirstName = "Paula",
LastName = "Smith",
BirthDate = "01/12/1977"
};
// Create Teacher
Teacher teacher1 = new Teacher
{
TeacherfirstName = "Paul",
};
// Create course object
Course course = new Course();
course.CourseName = "Programming with C#.";
// Create degree object
Degree degree = new Degree();
degree.DegreeName = "Bachelor of Science Degree";
// Create Program object
UProgram uprogram = new UProgram();
uprogram.ProgramName = "Information Technology";
// count = GetActiveInstances(typeof(Student));
Console.WriteLine("The {0} program contains the {1} ", uprogram.ProgramName, degree.DegreeName);
Console.WriteLine("The {0} contains the course {1} ",degree.DegreeName, course.CourseName );
// Console.WriteLine("The {0} course contains students(s)" count);
Console.WriteLine("Count" + Student.count);
Console.Read();
}
public class Student
{
public static int count = 0;
public Student()
{
// Thread safe since this is a static property
Interlocked.Increment(ref count);
}
// use properties!
private string firstName;
// Get Set for FirstName
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
// Get Set for LastName
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private string birthDate;
// Get Set for BirthDate
public string BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
}
public class Teacher
{
private string teacherfirstName;
public string TeacherfirstName
{
get { return teacherfirstName; }
set { teacherfirstName = value; }
}
// string teacherlastName;
// string teacheroffice;
}
public class UProgram
{
private string programName;
public string ProgramName
{
get { return programName; }
set { programName = value; }
}
// string degreeOffered;
// string department;
}
public class Degree
{
private string courseName;
public string DegreeName
{
get { return courseName; }
set { courseName = value; }
}
}
public class Course
{
private string courseName;
public string CourseName
{
get { return courseName; }
set { courseName = value; }
}
Student[] studentarray = new Student[3];
studentarray[0] = student1;
// studentarray[1] = student2;
// studentarray[2] = student3;
Teacher[] teacherarray = new Teacher[3];
}
}
}
|
|
|
|
|
Yes - it's what I assumed it my last reply You inserted the assignment to array-index 0 directly within class-scope. It has to happen in a method (or, theoretically, in the constructor).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
thanks for the post ! I will create a method and increment through the 3 students and add them, i will call the method outside the class
|
|
|
|
|
tá fáilte romhat
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
an-mhaith !!
|
|
|
|
|
Hi there.
I am making an application to manage data in some google spreadsheets.
I have all the References I need, and I can retrieve the data from spreadsheet decently I think, but how can I get all the data into my dataGridView properly?
I have low knowledge in C# generally, and virtually no experience with Google API, but I have sat down for hours now trying to figure it out.
I used this page[^] as a guide, but it doesn't contain any information whatsoever.
My code looks like this:
public Form1()
{
InitializeComponent();
SpreadsheetsService wsService = new SpreadsheetsService("testApp");
wsService.setUserCredentials("email@gmail.com", "password");
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = wsService.Query(query);
foreach (SpreadsheetEntry entry in feed.Entries)
{
comboBox1.Items.Add(entry.Title.Text.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
int sheetIndex = comboBox1.SelectedIndex;
SpreadsheetsService wsService = new SpreadsheetsService("testApp");
wsService.setUserCredentials("email@gmail.com", "password");
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = wsService.Query(query);
SpreadsheetEntry mySpreadsheet = (SpreadsheetEntry)feed.Entries[sheetIndex];
AtomLink link = mySpreadsheet.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, null);
WorksheetQuery wQuery = new WorksheetQuery(link.HRef.ToString());
WorksheetFeed wFeed = wsService.Query(wQuery);
WorksheetEntry worksheetEntry = (WorksheetEntry)wFeed.Entries[0];
AtomLink cLink = worksheetEntry.Links.FindService(GDataSpreadsheetsNameTable.CellRel, null);
CellQuery cQuery = new CellQuery(cLink.HRef.ToString());
CellFeed cFeed = wsService.Query(cQuery);
foreach (CellEntry curCell in cFeed.Entries)
{
Debug.WriteLine("The value on row {0} and column {1} is {2}", curCell.Cell.Row,
curCell.Cell.Column, curCell.Cell.Value);
}
}
I really appreciate your effort on helping me.
Thanks in advance!
|
|
|
|
|
Still stuck on this one.
|
|
|
|
|
I'm writing a WPF app with a custom window frame (not a specific WPF question though). Due to the necessary "macgyverising" of the window and window frame to get things to work correctly (i.e. hidden transparent window borders, etc.), it kind of messed up the aero-snapping a little bit. I want to catch the Win+Left, etc. hot keys so I can "fix" my window.
All the usual suspects don't work for this. Keydown, PreviewKeydown, custom window proc, etc. Only thing I've found that works for this (so far) is a low level keyboard hook.
I'm not thrilled about the low level keyboard hook, as its a global hook and if my EXE is running, the system will call my app for ALL keys in ALL apps. That's not really an acceptable solution in my book.
Any other options?
|
|
|
|
|
Well, it takes a low-level keyboard hook because your app never sees the key combination. Windows handles it. Well, more specifically, Explorer does. Your app window gets a ton of WM_ messages that tell it to resize itself to whatever size the screen height and 1/2 the width is and to move itself over to the top left corner of the desktop window. The messages are essentially no different than if the user told the window to do then same thing using the mouse.
I've never tried to doing this, but you might be able to prevent this by handling the Resize event or setting the forms MaxSize property.
|
|
|
|
|
I did discover that setting ResizeMode=None blocks the Aero-snap commands just for the specific window, but there still wasn't any way to catch the Win+Left that I could find. The regular keyboard hook would only catch it on the release.
I tried catching WM_GETMINMAXINFO, but the samples I saw used Marshal.PtrToStructure, so I guess that makes a copy of the struct and you can't modify the original data?
Regardless, I also tried catching the various sizing messages, and yeah, I guess I was able to kind of trap Height = 100% / Width = 50%, but there was no way to tell if that was from the user mouse or from Aero-snap and that would kind of be the same situation if I got the WM_GETMINMAXINFO message working.
So, I guess the only solution is to use the low level keyboard hook -- with a twist .
I only need it to catch Win+Left, Win+Right and Win+Up... so I can catch the Win down in my app and then I install the system wide hook so I can catch the 3 Win+XXX combinations and when the Win button is released, or my app loses focus I remove it.
Rinse. Repeat.
Still don't like installing a system wide hook, but I'm only keeping it installed when they press the Win key, so I guess its not too bad. Better then leaving it installed the whole time my app is up.
|
|
|
|
|
Don't forget that the keyboard isn't the only way that Aero-snap can be invoked. Dragging the window to the edge of the screen will also invoke it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Guys,
How do I go about adding objects to a list in a class?
I have a some Property classes, that Id like to add to a list contained in another class.
public class Property
{
public string property { get; set; }
public string value { get; set; }
public Property() { }
public Property(string prop, string val)
{
property = property;
value = value;
}
}
public class clsContacts
{
public List<Property> properties { get; set; }
}
When I try something like the code below, it compiles fine, but when I run it, I get the following error:
"object reference not set to an instance of an object."
Property p = new Property();
p.property = "email";
p.value = "xxxxxxxxxxx@Acme1.net";
clsContacts lstContacts = new clsContacts();
lstContacts.properties.Add(p);
Any suggestions as to what I should be doing?
Thanks
modified 18-Apr-15 16:29pm.
|
|
|
|
|
It looks like you haven't initialized properties in lstContacts. You need to instantiate it before you try to add records to it.
|
|
|
|
|
Looks like you didn't initialize lstContacts.properties
Probably in the constructor clsContacts you should initialize it with an instance of an empty list:
public class ClsContacts
{
public List Properties { get; private set; }
public ClsContacts()
{
Properties = new List<Property>();
}
}
Some other suggestions: Follow the official C# naming conventions: Upper case for class names and class members. Avoid public setters. If ClsContacts is just intended to give a custom name to a List<Property>, then I would derive it from List<T> instead. If it's meant to have more functionality, then don't expose the List<Property> as such at all but instead as an interface that only allows reading (e.g. IEnumerable or ReadOnlyCollection) and have the allowed manipulations as methods of ClsContacts.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks a lot Sascha, That works.
I'll keep your suggestions in mind when I start knowing what Im doing. ;o)
Thanks for the quick replies guys.
|
|
|
|
|
Doncal wrote: Thanks a lot Sascha, That works. You're welcome!
Doncal wrote: I'll keep your suggestions in mind when I start knowing what Im doing. ;o) Hehe, alright
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Good answer, Sascha, both your and Pete's answers got my vote.Sascha Lefévre wrote: but instead as an interface that only allows reading (e.g. IEnumerable or ReadOnlyCollection) and have the allowed manipulations as methods of ClsContacts. I'd be curious to see how you implement that: read-only from an interface. Saying this, I am aware I may not fully understand what you mean by this.
thanks, Bill
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"
|
|
|
|
|
Thank you, Bill!
Probably I just managed to express my idea in an obfuscated way
This is what I meant:
class ListEncapsulation
{
public ReadOnlyCollection<int> Example1 { get { return EncapsulatedList.AsReadOnly(); } }
public IEnumerable<int> Example2 { get { return EncapsulatedList.AsReadOnly(); } }
public IReadOnlyList<int> Example3 { get { return EncapsulatedList.AsReadOnly(); } }
private List<int> EncapsulatedList = new List<int>();
}
This answer on Stackoverflow shows the idea in some more detail:
http://stackoverflow.com/a/7445676/4320056[^]
And a short article on the readonly-collection-interfaces new in .NET 4.5:
http://visualstudiomagazine.com/articles/2012/08/07/new-read-only-collection-interfaces-for-net.aspx[^]
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I have just uploaded a windows remote decktop client server application but it needs a client program to be instaled to work and is basicly a program that does a screen grab on the server and it then sends the image across the network to the client. These images are big and slow to send.
Well call me stupid but i think it could all be done in HTML so that it works in any browser and it should not be too hard to do if anyone wants to give me a hand.
Pulling the current desctop background image from windows is easy and looking in the destop folder to pull the shortcut text and then pulling the icon images should be easy too.
The start menu again is just text and icons that could be sent back as a upside down menu in HTML.
OK the task bar might need a bit more thinking about but if the worse comes to the worse then a long sideways image and a javascript polling timer would just about fit the bill.
floating divs with z-index's would do for application windows and could use a bit of JSON to keep an eye on the X,Y positions.
This just leaves the active window to deal with which would need to be sent back over the wire every second but since we know the scaled size of the window then sending it as a smaller .png and resizing it back by streching it to full size or better still doing something with a HTML5 Canvus.
It's all just text and images so does anyone want to get involved and work as a team on this one ?
|
|
|
|
|
I use below code to create chart in Excel with C#:
Excel._Workbook oWB;
Excel.Range oRange;
Excel._Chart oChart;
oWB = (Excel._Workbook)oWS.Parent;
oChart = (Excel._Chart)oWB.Charts.Add(Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
oChart.HasTitle = true;
oChart.ChartTitle.Text = "IDcsacsa\t" + "TIMEfsafafas\t" + "FASvsdvfsd";
oRange = oWS.get_Range("B2", "B6");
oChart.SetSourceData(oRange, Missing.Value);
oChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
this code is working but there is a problem.
with this line:
oRange = oWS.get_Range("B2", "C6");
I specify the columns that must create chart from its date.
for example in above code we create chart of B2 to B6 and C2 to C6 and working well.
but now in excel worksheet I have data in 3 columns B,C and D.
so I want design Chart only from B and D columns and I don't want design chart from C columns.
so how I use get_Reang?
|
|
|
|
|
oRange = oWS.get_Range("B2:B6,D2:D6")
Should do the trick.
Replace the numbers with the ones you need.
See here for more info.[^]
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Mathematics is not my strong point!
I'm coding against a protocol (OSC) that connects to an external device that takes a float between 0.0 and 1.0 to represent a frequency between 20Hz and 400Hz. The frequency scale is an inverse log curve so for example 0.5 represents approximately 89Hz.
How on earth do I convert from the float to the frequency - and back again too?
|
|
|
|
|
Though math is also not my strong point, I would like to give it a shot.. Can you give two more "sample points" ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Sure...
.25 = approx. 42Hz and .75 = approx. 189Hz
I've found a little more information. The displayed values on the end device are arranged into 101 steps so the mid value is actually 0.49 which displays 87Hz (this will be rounded though)
|
|
|
|