Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

WPF - class containing property that is true/false

Options
  • 11-03-2012 9:17pm
    #1
    Registered Users Posts: 1,496 ✭✭✭


    Im creating a windows app in xaml/wpf, that shows a list of animals.
    In the class of one of the animals there is a property that is to be either true or false.
    could anyone tell me how to do this it would look something like this :
    public class animal 
    {
    public ______(what will go here?) Endangered(name of property) {get; set;}
    }
    

    i will then create the animal and will input myself if the property is true or false, but how do i tell it that its going to be a true/false outcome ?
    thanks.


Comments

  • Registered Users Posts: 138 ✭✭MagicRon


    Show us how your animal class is looking so far.

    For true/false, read up on System.Boolean
    http://msdn.microsoft.com/en-us/library/c8f5xwh7(v=vs.71).aspx

    Perhaps in the constructor of the class you could pass in whether the animal is endangered or not (http://msdn.microsoft.com/en-us/library/ms229060.aspx)


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    bool


  • Registered Users Posts: 586 ✭✭✭Aswerty


    public class animal
    {
    public bool Endangered {get; set;} // properties do not have parameters
    }

    public class program
    {
    int main()
    {
    Animal panda = new Animal(); // create an instance of an animal
    panda.Endangered = true; // poor pandas :(
    }
    }

    Basic implementation of creating new animal instance and assigning a property value.


  • Registered Users Posts: 138 ✭✭MagicRon


    Aswerty wrote: »
    public class animal
    Aswerty wrote: »
    Animal panda = new Animal();

    ^ will not build. You should be using Pascal Casing for class names i.e. "public class Animal" not "public class animal".

    You will probably be creating your Animal objects in the constructor of the MainWindow partial class just after InitializeComponent() in file MainWindow.xaml.cs; (assuming you have kept the default window name)


  • Registered Users Posts: 586 ✭✭✭Aswerty


    MagicRon wrote: »
    ^ will not build. You should be using Pascal Casing for class names i.e. "public class Animal" not "public class animal".

    You will probably be creating your Animal objects in the constructor of the MainWindow partial class just after InitializeComponent() in file MainWindow.xaml.cs; (assuming you have kept the default window name)

    A typo from copy pasting the OP's code. I'm giving an example of how to use a property not trying to write his code for him, so chill.


  • Advertisement
  • Registered Users Posts: 138 ✭✭MagicRon


    You == OP


  • Registered Users Posts: 586 ✭✭✭Aswerty


    MagicRon wrote: »
    You == OP

    false


  • Registered Users Posts: 138 ✭✭MagicRon


    Ah here, you're not the one creating endangered Pandas for your homework. I was simply giving the OP some advice


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    That's tragic, Ron.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Fair enough, just sounded like you were nit picking for the sake of it.


  • Advertisement
  • Registered Users Posts: 1,496 ✭✭✭mcw92


    Ive got this working, cheers to Aswerty for sorting it out.
    Btw, this is not my actual code, mine was too long to post here so i wrote a quick example of what i wanted done. :)


  • Registered Users Posts: 2,494 ✭✭✭kayos


    MagicRon wrote: »
    You will probably be creating your Animal objects in the constructor of the MainWindow partial class just after InitializeComponent() in file MainWindow.xaml.cs; (assuming you have kept the default window name)

    Well the OP could do that but such logic does not belong in the UI. OP please look into the MVVM pattern if you are doing WPF. Will take a little time to get to grips with it but oh so worth it in the end. If you are already following MVVM good stuff :)


  • Registered Users Posts: 1,496 ✭✭✭mcw92


    Ok thanks for all the replies, but i have another question now,

    I have a listbox that contains item's properties and such,
    The listbox contents is done one letter per line, whereas i want it words per line like shown in the image below.
    This is the code i have used

    xaml.cs code:
    lblDetails.ItemsSource =  a.BDescription;
    
    where lbldetails is the listbox,

    inside the main Animal class code
    public virtual string BigDesc() { return "Unknown Animal"; }
            public string BDescription { get { return BigDesc(); } }
    



    inside the individual animal classes:
    public override string BigDesc()
            {
    
                return String.Format("Elephant ({0}) native to {1}, Nickname: {2} Eats {3}kg grass Sub-species: {4} Tusks: {5}m ", SexType, Continent, GoesBy, QuantityGrass,typeOfElephant,tuskLenght   );
            }
    

    What it looks like now:

    2irmc0p.png

    What it should look like:

    10enejb.png


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    lblDetails.ItemsSource should probably be lblDetails.Text??? Is it a ListBox (lb?) or a Label? (lbl)

    It should probably be a list if it's a ListBox ie
    lblDetails.ItemsSource = ListOfAnimals.

    Elephant also seems to be hardcoded.


  • Registered Users Posts: 1,496 ✭✭✭mcw92


    Giblet wrote: »
    lblDetails.ItemsSource should probably be lblDetails.Text??? Is it a ListBox (lb?) or a Label? (lbl)

    It should probably be a list if it's a ListBox ie
    lblDetails.ItemsSource = ListOfAnimals.

    Elephant also seems to be hardcoded.

    Sorry yes its a Listbox(lb),
    Ive changed elephant so that its not hardcoded now,

    I dont understand what you mean by 'ListOfAnimals', id have to create that obviously yes but what exactly do i create?

    The lblDetails Listbox should contain the strings,ints and enums of the selected class.


  • Registered Users Posts: 138 ✭✭MagicRon


    List<Animal> listOfAnimals = new List<Animal>();
    listOfAnimals.Add(animal1);
    listOfAnimals.Add(animal2);
    listOfAnimals.Add(animal3);
    listOfAnimals.Add(animal4);


  • Registered Users Posts: 138 ✭✭MagicRon


    Ohhh, you want to display the details of ONE animal in the listbox, not a listbox listing all of your animals? :)


  • Registered Users Posts: 1,496 ✭✭✭mcw92


    MagicRon wrote: »
    Ohhh, you want to display the details of ONE animal in the listbox, not a listbox listing all of your animals? :)

    Yes the details of one animal,
    i have another listbox containing all the animals names already working.
    So all the animals are in a list called 'zoo' already.

    In the details listbox i want to display individual strings of the selected animal, its doing this, but the text if formatted at one letter per line, and i want it to be normal like words per line.
    is it just case of a convert.to ? or is there a property in the listbox code in the xaml that i have to set?


  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    mcw92 wrote: »
    Yes the details of one animal,
    i have another listbox containing all the animals names already working.
    So all the animals are in a list called 'zoo' already.

    In the details listbox i want to display individual strings of the selected animal, its doing this, but the text if formatted at one letter per line, and i want it to be normal like words per line.
    is it just case of a convert.to ? or is there a property in the listbox code in the xaml that i have to set?

    That's because it's doing some sort of magic with the Text to turn it into an array it seems! You need a list of strings most likely.
    List<String> myDesc = new List<String>();
    myDesc.Add(a.BDescription);
    lblDetails.ItemsSource = myDesc;


  • Registered Users Posts: 1,496 ✭✭✭mcw92


    Giblet wrote: »
    That's because it's doing some sort of magic with the Text to turn it into an array it seems! You need a list of strings most likely.
    List<String> myDesc = new List<String>();
    myDesc.Add(a.BDescription);
    lblDetails.ItemsSource = myDesc;


    thanks very much, but now one more simple thing that ive dones millions of times but cannot remember it with all this cramming!:(

    how do i get it to skip to next line mid sentence ?
    "{0} ({1}) native to {2}, Nickname: {3} Kills with {4}", SpeciesName, SexType, Continent, GoesBy, KillsWith
    
    like for the first line in the listbox to be this content :
    {0} ({1}) native to {2}, Nickname: {3}
    second line :
    Kills with {4}"

    i knows its something simple i put inbetween the code?


  • Advertisement
  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    You're going to need to do a string.split on some special token or generate it that way in the first place. Or maybe "\n" might do something I'm not familiar with how Listboxes render all their contents.


Advertisement