9 Replies - 1005 Views - Last Post: 30 December 2010 - 12:10 PM Rate Topic: -----

#1 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

using METHODS in different CLASSES

Posted 30 December 2010 - 11:06 AM

Hi, im trying to get used to using different classes and methods!

as you can see by the attached images, I have tried my best in what i thought was right, and also by looking online and reading up for help in this area.

I am still not totally sure how the get and set jobby works so thats the area that is holding the majority of my syntactical errors i believe!

if anyone can help then i will be most grateful!

Thanks in advance...

Cam

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: using METHODS in different CLASSES

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:31 AM

Well, here's the deal. Properties don't really need to be used if you're going to keep the field private. Properties are best used for public fields, so that you can obfuscate the field itself. You can treat the property like a field in another class, but on the back end, you'll be able to do things like validating input and conditionally returning values, and that kind of stuff.

So, from what I see, none of those properties need to be properties at all. You could just remove the {get;set;} from them (and add a ; the line has to end in one of those).

The other issue...why are you declaring so many Form1s? That must be a mistake. They look like they would be TextBoxes, not Form1s. And usually you don't need to define your GUI components in the code, you can drag and drop them onto the form in the designer, and change their Name property in the properties window to whatever you want.
Was This Post Helpful? 0
  • +
  • -

#3 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:33 AM

okay guys! i seem to have played around a bit and changed some stuff that i read up on!

it was supposed to be something like this wasnt it? :

//in the calculation Class i would have

public decimal salePrice { get; private set; }
public decimal discountPrice { get; private set; }

//and then the method with the calculation...



// and then with the Form1 i would have the same again so that the method in the form can read what we are talking about? :

public decimal salePrice { get; private set; }
public decimal discountPrice { get; private set; }




thats basically what i did... have a look at the images attached:

dont worry about my comments i made in green.. just look at the method at the bottom :)

there is a problem however when i try something into the PriceBox on compilation of the program!

it says: "object reference not set to an instance of an object"

any ideas please?

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,070
  • Joined: 02-June 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:33 AM

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library.

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

  • Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
  • Arrays: Shows how to use arrays.
  • Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
  • Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
  • COM Interop Part I: Shows how to use C# to interoperate with COM objects.
  • COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
  • Commandline: Demonstrates simple command-line processing and array indexing.
  • Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
  • Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
  • Events: Shows how to declare, invoke, and configure events in C#.
  • Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
  • Generics: Shows how to make generic collection classes that can be used with the foreach statement.
  • Hello World: A Hello World application.
  • Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
  • Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
  • Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
  • Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
  • Nullable: Demonstrates value types, such as double and bool, that can be set to null
  • Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
  • OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
  • Operator Overloading: Shows how user-defined classes can overload operators
  • Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
  • PInvoke: Shows how to call exported DLL functions from C#
  • Properties: Shows how properties are declared and used; also demonstrates abstract properties
  • Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
  • Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
  • Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
  • Structs: Shows how to use structs in C#.
  • Threading: Demonstrates various thread activities such as creating and executing a thread, synchronizing threads, interacting between threads, and using a thread pool
  • Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
  • User Conversions: Shows how to define conversions to and from user-defined types
  • Versioning: Demonstrates versioning in C# by using the override and new keywords
  • XML Documents: Shows how to document code by using XML
  • Yield: Demonstrates how to use the yield keyword to filter items in a collection

Was This Post Helpful? 0
  • +
  • -

#5 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:41 AM

oh right? so i dont need the { get; private set; } ??

hmm.. okay let me try your way if i understand properly..

so i changed the:

public decimal salePrice { get; set; } etc to:

public decimal salePrice;

and that should do it?

let me try it now

thanks for the help bro

wow, okay thank you tlhIn'toq for providing me with these references! i have been trying to look for good tutorials online on like youtube etc but havent found any to be useful!

i will look at what you have referenced, thank you for taking the time!

Cam
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:43 AM

Yes. But you still need to fix whatever it was you're doing with all those Form1s.

As to the way properties work:

When you use {get; set;}, that is actually the shortcut way of doing things. The old way is this:

private int someInt;
public int SomeInt
{
  get
  {
    return someInt;
  }
  set
  {
    someInt = value;
  }
}


So it's basically hiding methods as what are called "accessors". This was the simplest way to do it. You can do whatever you want in these accessors, but this is the most basic way of making a Property expose a field.

Doing it the shortcut way basically replicates that without the need to define the private field.

Now, the accessors can have their own visibility levels as well. Notice on yours they all say "private set". You're trying to set the setter's visibility, but the property itself was already private, so it wouldn't work.

The reason that each of those accessors has it's own visibility is to allow for things to expose getting and setting at differnt levels. Private set means that you can only set values from inside the class. Outside, it will appear to be read only.

You can also completely leave out the set accessor to make a value read only.
Was This Post Helpful? 0
  • +
  • -

#7 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:45 AM

What you need to do in your calculation class is have your fields as private:

private decimal salePrice;
private decimal discountPrice;



Then you need to add a public properties with the get and set accessors:

public decimal SalePrice
{
   get { return salePrice; }
   set { salePrice = value; }
}

public decimal DiscountPrice
{
   get { return discountPrice; }
   set { discountPrice = value; }
}



The standard method is to start the private fields in lower case and the properties in upper case. When you add a property it doesn't have to have both get and set accessors but must have at least one. This will depend on if you need to obtain values from the class or just set the values or both.

To access the data in the class you woul use the instance name:

Calculation calculation = new calculation();

SubTotalBox.Text = calculation.SalePrice.ToSring();

Edit: From the above posts I assume im doing it the old way then :P

This post has been edited by mouse88: 30 December 2010 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#8 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 11:52 AM

OKAY! yes i am starting to understand this now!

thank you all for your help in making me get all this right in my head!

let me go and make the neccessary changes!

im very grateful for everyones help and i will definately be using the references tlhIn'toq gave me as well as implement the code snipets you guys have shared with me! its great i love programming :)

thank you all very much, i hope to get back soon with a finished product!

Cam
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 12:05 PM

View Postmouse88, on 30 December 2010 - 11:45 AM, said:

From the above posts I assume im doing it the old way then :P


More that you're doing it the long way. get; set; is a quick thing you do for properties that you know are just going to be your standard return val, val = value sort of properties. If you need to do anything more advanced than that, you have to do it the long way.

Now, as to this case here, you suggested that he use public properties. I disagree, since I don't think any of this data has to be available outside the form. I think he should just stick with private fields.
Was This Post Helpful? 0
  • +
  • -

#10 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: using METHODS in different CLASSES

Posted 30 December 2010 - 12:10 PM

okay im sticking to private fields :) ahah

im busy reading up on the tutorials atm... i need to understand everything properly!

thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1