 |
| C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 13th, 2010, 07:43 AM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Is what i'm coding wright or wrong
Hi Every Body
pls some one tell me if what i'm coding below is write or wrong , i wrote main class called "Family" inside the Family class i defined a structure called "Sun" as follow
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test2del
{
public class Family
{
string motherName;
string fatherName;
public string famName;
Son[] FamilyColl = new Son[5];
public Family(string fn, string mn)
{
motherName = mn;
fatherName = fn;
}
public struct Son
{
string SonName;
String kind;
float age;
public Son(string sn, string sk, float sa)
{
SonName = sn;
kind = sk;
age = sa;
}
public string GetSonName
{
get { return SonName; }
}
}
}
}
in the main Program file, i defied object of type Family and no problem
also i want to define an object of type Sun and this is the problem how can i do it
|
|

June 14th, 2010, 03:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Since you nested the Son struct inside the Family class you need to access it by prefxing it with Family like this:
Code:
Family.Son son = new Family.Son("Some Value", "Some Other Value", 12);
Cheers,
Imar
|
|

June 14th, 2010, 04:36 PM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes you alright but when i "wrote Family." I didn't see the son Structure type in the pop up list and i don't know why and for that i asked my question to see if i made something wrong
|
|

June 14th, 2010, 04:43 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Where / how are you trying this? It worked fine for me in another method in another class referencing the namespace with the Family class.
Cheers,
Imar
|
|

June 15th, 2010, 04:00 AM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried this in a button click handler of the main Form, since i defined a global variable of type Family in the form1 class as follow
then i madea button for adding Family and create the fam object by writing the following init,s handler
HTML Code:
fam = new Family(FaNameTxt.Text, MoNameTxt.Text);
then i added a button for define an object of type son and adding son and here's the problem exist or discovered as i said befor when writing " fam." i didn't find son type in the pop up list
|
|

June 15th, 2010, 04:06 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Family.Son is the name of the structure you have defined. However fam is an instance of Family - it doesn't have a property called 'Son', but a field variable called "FamilyCol" which you defined as an array of Son elements.
Code:
fam.FamilyCol[0] = new Family.Son("A", "B", 12);
|
|

June 16th, 2010, 04:53 AM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks samjudson it works
But I have 2 Qustion
you have mentioned in ur answer the following
Quote:
Originally Posted by samjudson
- it doesn't have a property called 'Son', but a field variable called "FamilyCol"
|
could i define a property that could create an object of type son like this
Code:
public Son GetSun
{get{return new Son();}}
if so how code i reference that object in my main form code
2nd question
what's the situation if i don't have family collection in this case the family array(FamilyColl is omitted) how could i create an object of type Son in this case
|
|

June 16th, 2010, 05:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Yes, you could create a property to do that but I'm not sure why you would want to. Why create a property that returns a new Son() element when you could just create one yourself.
I think there are a number of problems here that you are not addressing.
Firstly - the difference between a class and a struct - they are quite complex and can cause problems to people new to programming. Basically if you declare a variable as a struct type then it is a value type, and you don't need to assign a new instance to it. With a class you do.
Code:
struct Son
{
public string SonName;
}
// valid
Son sonA;
sonA.SonName = "Fred";
class SonClass
{
public string SonName;
}
SonClass sonB;
sonB.SonName = "Ted"; // Invalid - sonB is null
sonB = new SonClass();
sonB.SonName = "Ted";
Secondly you have declared the 'Son' struct inside the Family class, therefore OUTSIDE the Family class you need to use its full name, which is Family.Son to refer to it. If you don't want to do this then move the struct declaration outside the Family class.
Code:
class Family
{
//...
}
struct Son
{
//...
}
Finally - exactly what are you trying to achieve here. If I was trying to write a 'Family' class I would not have a single property called "Son" - who say that all families have 1 and only 1 son? Also, using a struct for Son means that there is no concept of a null Son (see previous point above) so you cannot have 'no' Sons.
Perhaps it would be better if you told us what you are trying to achieve so we know what your aim is, rather than asking questions for which there are quite possibly no easy or even correct answers.
|
|

June 17th, 2010, 08:28 AM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My objective not this family son application, but it's creating a bank account management application by creating bank account object which contains all the transactions of the client like drawing money ,how much money,what's kind of coin has been drawn or deposit money and so on .
My Idea was Creating a class represents the client account contain client name,account number,date of account creation,bank name,the size of money in this account and the coin type and want to make the transaction as i said before in the form of structure because it's light weight than class and allow the user to create it then create a container to hold these transactions, but before begin my real application i wanted to make sure that i could create a structure in side a class the creating an object of that structure via tha family son application
|
|

June 17th, 2010, 08:36 AM
|
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My objective not this family son application, but it's creating a bank account management application by creating bank account object which contains all the transactions of the client like drawing money ,how much money,what's kind of coin has been drawn or deposit money and so on .
My Idea was Creating a class represents the client account contain client name,account number,date of account creation,bank name,the size of money in this account and the coin type and want to make the transaction(deposit,draw) as i said before in the form of structure because it's light weight than class and allow the user to create it then create a container to hold these transactions, but before begin my real application i wanted to make sure that i could create a structure in side a class then creating an object of that structure via that family son application
|
|
 |