1

Im making an application for my university in which i want to create object of student through customized constructor, but i dont know how to create objects on button click, i dont want to link it with a database, i want to create objects after application starts and all the objects would be killed when the application is closed.

Thanx Everyone

Rehan Manzoor
  • 87
  • 1
  • 1
  • 8

2 Answers2

2

I am not entirely sure what you're asking.

To create an object (whatever that means)

SomeClass classInstance = new SomeClass();

C# is a managed language which means you don't need to worry about killing the objects after the application closes, unless those object implement IDisposable but I have a feeling that is way above your scope for now.

Stan R.
  • 15,757
  • 4
  • 50
  • 58
1

On a button click - you will need Event handling and subscribe to correct events.

to create a new object:

YourClass objc = new YourClass()

to connect to a database:

you need to set up the ConnectionString correctly and access the database through SqlClient interfaces (or you can use Linq2Sql or entity framework or whatever!)

--- edit after OP's comment ---

You can create a new object with name and some id passing the name and id in a text box by actually compiling the source code and loading the assembly dynamically. This is ugly way to do in C#. But here is a way to actually compile an assembly in C# and then run it:

What is the best scripting language to embed in a C# desktop application?

Community
  • 1
  • 1
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • no i dont want to connect a data base, i know event handling too, but i want to pass name and id through text boxes which is not a problem, but at every click new object should be created, with the name and id i have wrote in the textboxes – Rehan Manzoor Feb 28 '13 at 00:27
  • Just grab the Id value from a Textbox when the button is clicked. If you are using ASP.NET you have a reference in the .cs code to the textbox object. – Garrett Feb 28 '13 at 00:30
  • gotcha @ aniket.. and thanx – Rehan Manzoor Feb 28 '13 at 00:32
  • give you a +1 because I have no idea what he is asking :) – Stan R. Feb 28 '13 at 00:39
  • @StanR. he wants to create objects with names that start with the data input in the textbox and an id in another number field.. so if the textbox contains "hello" and the number field contains `1` then he wants to create an object called `hello1` – Aniket Inge Feb 28 '13 at 00:40
  • no i want to create object if textbox1 contains rehan and textbox2 contains 3, the object name should be rehan3 and now the rehan3 must posess name rehan and ID or rollno 3 whenever called – Rehan Manzoor Feb 28 '13 at 00:52
  • and at every click new object should be created – Rehan Manzoor Feb 28 '13 at 00:52
  • hahahaha both of you mux be laughing at me.. im mad, the country where im, soon night gonna b over.. im sitting on my laptop since evening – Rehan Manzoor Feb 28 '13 at 00:54
  • @RummyKhan, the solution I posted tells you all about how to go about it – Aniket Inge Feb 28 '13 at 01:02
  • @RummyKhan hahaha and no, I don't think you're mad. Nor am I laughing – Aniket Inge Feb 28 '13 at 01:02