Results 1 to 8 of 8

Thread: [RESOLVED] Help with passing Class value

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Resolved [RESOLVED] Help with passing Class value

    hello guys! I have 2 windows forms, form1 and form2, and a class named Model and I created an instance of Class model in form1 name EventData and assigned value to its fields using the code below

    FORM1
    Code:
    Model EventData = new Model();
    EventData.EventTitle = ArgEvntTitle;
    EventData.EvenDate = ArgEvntDate;
    EventData.EventLocation = ArgEvntLocation;
    Class Model
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace RefNumGen 
    {
        
        public class Model
        {
            private string _EventTitle;
            private string _EventDate;
            private string _EventLocation;
    
            public string EventTitle
            {
                get { return _EventTitle; }
                set { _EventTitle = value; }
            }
    
            public string EvenDate
            {
                get { return _EventDate; }
                set { _EventDate = value; }
            }
    
            public string EventLocation
            {
                get { return _EventLocation; }
                set { _EventLocation = value; }
            }     
        }
    }
    my question is how can pass that instance,EventData, to an Instance of Class Model in form2?
    Last edited by daimous; Nov 15th, 2006 at 02:42 AM.

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

    Re: Help with passing Class value

    You are using the term "instance" incorrectly. An instance is an object. You don't pass an instance of the Model class to another instance of the Model class. You pass an instance to a variable or reference.

    Now, a String is an object, so how would you pass a String from one form to another? A Model is also an object so you pass a Model object from one form to another the same way. How do you ever pass data from one object to another? You either set a property or pass a parameter to a method. This is no different. If you have an instance of the Model class in Form1 and you want to pass that instance to Form2 then you need to set a property in Form2 and assign that object or else pass the object as a parameter to a method of Form2. You know how to set properties and call methods already because you do it all the time.
    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

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with passing Class value

    Thanks for the correction JM..Ii really appreciate it..Atleast i know now how to and when to use word "instance"..anyway, I'll give it a try...Thanks a bunch!

  4. #4

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with passing Class value

    I've tried one of your solution which is to create a method in form2 that accepts instance of model(object) as a parameter but I have an error. I just dont know if I do it right...
    In my form1 I have this code
    Code:
                    Model EventData = new Model();                
                    EventData.EventTitle = ArgEvntTitle;
                    EventData.EvenDate = ArgEvntDate;
                    EventData.EventLocation = ArgEvntLocation;
                    Notification_frm.GetEventData(EventData);
    and in my FOrm2 where my method resides which accept object as a parameter
    Code:
            public object GetEventData(object obj)
            {
                Model objEventData = obj;
    
                lnkLblNotificationTitle.Text = objEventData.EventTitle;
                lblNotificationDate.Text = objEventData.EvenDate;
                lblNotificationLocation.Text = objEventData.EventLocation;
            }
    Error:
    Code:
    Error	1	Cannot implicitly convert type 'object' to 'RefNumGen.Model'. An explicit conversion exists (are you missing a cast?)	C:\Documents and Settings\daimous\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Notification.cs	36	34	RefNumGen
    and in this line of my code where error occurs

    Code:
    Model objEventData = obj;
    Did I missed something or done something wrong?
    Last edited by daimous; Nov 15th, 2006 at 10:14 PM.

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

    Re: Help with passing Class value

    Is this GetEventData method supposed to be able to accept any type of object as a parameter? From its implementation I would assume not. If it's only supposed to accept a Model object as a parameter then declare the argument as type Model.

    The actual error is occurring because you could pass absolutely anything to that method as a parameter. The compiler cannot allow you to assign it to a Model variable because it has guarantee that it IS a Model object. You could cast the argument as type Model and thereby tell the compiler that you will take care of making sure that only a Model object ever gets to that point. The compiler will take your word for it and allow you to continue but if you fail to fulfil your promise it will lead to a run time exception, i.e. a crash. You could perform a cast but if you only intend for the method to receive Model objects then you should do as I suggested and declare the argument accordingly.

    Also, are you intending to be able to use this Model object in Form2 some time later? If so then assigning it to a local variable is no good. If you want to access the object later then you need to assign it to a variable that you can access later, i.e. a class-level variable.
    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

  6. #6

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with passing Class value

    Is this GetEventData method supposed to be able to accept any type of object as a parameter?
    Yah, you are right..It is only supposed to accept a model Object as parameter.


    Also, are you intending to be able to use this Model object in Form2 some time later?
    no...actually, I assign it to variable becuase I was thinking that I need first to assign the obj argument to an instance of Class model before i can get its Fields thru its property thats why i have this code
    Code:
    Model objEventData = obj;
    but thanks for the information now i know that assigning it to a variable is no good since i will not be needing it some time later.

    Now its working by changing the argument of GetEventData method and by replacing the type of the method from object to void
    Code:
            public void GetEventData(Model obj)
            {
                
                Model objEventData = obj;
                
                lnkLblNotificationTitle.Text = objEventData.EventTitle;
                lblNotificationDate.Text = objEventData.EvenDate;
                lblNotificationLocation.Text = objEventData.EventLocation;
            }
    Thanks for the Help and Information Jm...

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

    Re: Help with passing Class value

    There's no reason to assign the parameter to another local variable. You've already got the 'obj' variable to get the property values from, so the 'objEventData' variable doesn't add any value.
    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

  8. #8

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with passing Class value

    Ok...thanks for that.

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