Results 1 to 9 of 9

Thread: Need Help accessing Class Properties in a Windows form

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Question Need Help accessing Class Properties in a Windows form

    Background
    excuse my ignorance i'm getting into CSharp development, have knowledge of vb.


    Scenario

    I have a class module SFactory.cs where I retrieve data from the database into a script collection using sql adapter and filling it in dataset and returnign back as a script collection.



    I have 2 values returned from the stored procedure which are stored as Script.Property1 and Script.Property2 (same for each row from stored proc.)





    Situation
    I need to access these 2 values in 2 textboxes in my windows form



    My windows form code looks like this –



    ScriptCollection scripts = SFactory.RetrieveScripts(input1, input2, input3);

    this.mygrid.DataSource = scripts;


    need to add something like as shown below, but i do not see my properties if i can atleast access one row of scripts collection or of the grid and retrieve would help me.

    this.mytext1.text = Convert.ToString(scripts.Property1)

    this.mytext2.text = Convert.ToString(scripts.Property2)

    or

    this.mytext1.text = Convert.ToString(grid.row.1.column(8).text)
    this.mytext2.text = Convert.ToString(grid.row.1.column(9).text)

    any help is sincerely appreciated.


    Thanks.
    csharp developer
    Last edited by csharpdeveloper; Feb 5th, 2007 at 02:20 PM. Reason: Phrase better

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Need Help accessing Class Properties in a Windows form

    I don't know if this will help:

    check here

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Re: Need Help accessing Class Properties in a Windows form

    Thanks Jennifer..

    i had actually looked at it before posting, but what i want is the reverse of it.

    i'll appreciate all your help.

    Thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need Help accessing Class Properties in a Windows form

    You've told us that a Script object has properties named Property1 and Property2. You've also told us that the scripts variable refers to a ScriptCollection object, which is presumably a collection of Script objects. ALWAYS relate OOP to the real world. If you had an egg carton full of eggs, would you be able to get the yolk, the white and the shell of the carton? Of course not. You'd have to first get an egg from the carton and get ITS yolk, white and shell. You cannot get properties of a Script object from a ScriptCollection object because they are two completely different things. You must first get a Script object from the ScriptCollection, THEN get ITS properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Re: Need Help accessing Class Properties in a Windows form

    In the above context, can you suggest the actual code syntax

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need Help accessing Class Properties in a Windows form

    How would you normally get a Script object from a ScriptCollection?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Smile Re: Need Help accessing Class Properties in a Windows form

    Thanks..

    i tried this and it worked

    In Collection file -
    -----------------
    public int ScriptCountOfPages()
    {
    int iScriptCountOfPages = 0;
    foreach (Script script in this)
    {
    iScriptCountOfPages = script.TotalPageCount;
    break;
    }
    return iScriptCountOfPages;
    }

    public int ScriptCountOfItems()
    {
    int iScriptCountOfItems = 0;
    foreach (Script script in this)
    {
    iScriptCountOfItems = script.TotalItemCount;
    break;
    }
    return iScriptCountOfItems;
    }


    In Win Form-
    -------------
    this.txtPageCount.Text = Convert.ToString(scripts.ScriptCountOfPages());
    this.txtItemCount.Text = Convert.ToString(scripts.ScriptCountOfItems());

    need to test... but looks like it worked !!!

    Thanks for everyone who replied.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need Help accessing Class Properties in a Windows form

    Those properties look a bit dodgy. They look like you're just getting the page and item count from the first Script object. Surely you want to sum the page and item count for all Script objects in the collection.
    Code:
    public int ScriptCountOfPages()
    {
        int iScriptCountOfPages = 0;
    
        foreach (Script script in this)
        {
            iScriptCountOfPages += script.TotalPageCount;
        }
    
        return iScriptCountOfPages;
    }
    
    public int ScriptCountOfItems()
    {
        int iScriptCountOfItems = 0;
    
        foreach (Script script in this)
        {
            iScriptCountOfItems += script.TotalItemCount;
        }
    
        return iScriptCountOfItems;
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Need Help accessing Class Properties in a Windows form

    That's a nice way of describing OOP, --> the carton of eggs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width