0

So I had this question for a long time, but all my thoughts came to nothing.

For example when you create a new windows application and you put a button into your form, then you create a button-Click event and here it goes:

you have (object sender) and (EventArgs e)

then you cast it like this : Button button = (Button)sender;

Now: how do I create stuff like "Button", for example I want to create a new type called "Book" and then type: "Book myBook = new Book();"

Is that even possible without deep knowledge in c#?

  • 1
    public class Book{ /*...*/ }, if that's the answer you're looking for then you need first to read some documentation about Object Oriented Programming – Gusman Apr 15 '15 at 19:38

3 Answers3

1

You can read more about classes. I think what you really want to know is how to create a class.

Here's an example of a class:

public class Book
{
   public string Title {get;set;}
   public string Author {get;set;}
}

And here's how you can use it.

Book book1 = new Book(); 
book1.Title = "Harry Potter";
book1.Author = "J.K. Rowling";
Console.WriteLine("{0} by {1}", book1.Title, book1.Author);

This is how you can create a constructor for the class.

public class Book
{
   //To create a constructor, you just create a method using the class name. 
   public Book(string title, string author)
   {
      this.Title = title;
      this.Author = author; 
   }

   //Creating a constructor with parameters eliminates the default 
   //constructor that's why you might want to add this if you want to          
   //instantiate the class without a parameters. 
   public Book() { } 

   public string Title {get;set;}
   public string Author {get;set;}
}

With that constructor you can create an instance of your class by

Book book1 = new Book("Harry Potter", "J.K. Rowling"); 

Another way to do this is by using an initializers. This way you don't need to pass a constructor parameters to fill the values of your properties.

You might want to read this.. https://msdn.microsoft.com/en-us/library/x9afc042.aspx

  Book book1 = new Book() { Title = "Harry Potter", Author = "J.K. Rowling" };
Lance
  • 2,774
  • 4
  • 37
  • 57
  • also, is there a way to pass the title and the author straight into book without book1.title == "..."? something like Book book1 = new Book("bla bla", "bla bla"); –  Apr 16 '15 at 09:12
  • Yes through the use of constructors. I'll update the example. – Lance Apr 16 '15 at 12:09
  • Oops, other person already answered this. –  Apr 16 '15 at 12:11
1

Here is the difference between type and class:
Difference between class and type

A good and simple tutorial to how to create/use a class in c#:
http://www.tutorialspoint.com/csharp/csharp_classes.htm

Also you can extend those elements, like Buttons or TextBox to build it as you want:
http://www.c-sharpcorner.com/UploadFile/ehtesham.dotnet/how-to-create-a-custom-control/

Community
  • 1
  • 1
Daniel Moreira
  • 430
  • 4
  • 17
0

Have you ever read about C#? If so, you should know about classes and structs.

class Book
{
  public string ISBN{get;set;}
  public float Price {get;set;}
}

Book myBook = new Book { ISBN="###-####-",Price=22.3}
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • i´m just a beginner, I didn´t this was done with classes, thanks for the answer! –  Apr 16 '15 at 09:09