Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone . I am trying to fill class with data and use these data anywhere else on the program, where i Will need it.

I created this class
C#
public class id
{

private string name; // field

public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}

And in form_name i tried to fill the class on this way:
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
this.Close();
}
}


But i don;t get any result. Could someone help me to clarify this, cause i am new to OOP, and don;t know so much about classes, and objects.

What I have tried:

And in another form i tried to retrieve data like this:
C#
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = new id();
txtrez.Text = idobje.Name;
}
Posted
Updated 14-Jun-20 23:34pm
v2

The problem is that when you create your id instance in the Click handler, it a local variable that holds it:
C#
private void Button_Click(object sender, RoutedEventArgs e)
   {
   id IDOBJE = new id();
   ...
   }
So at the end of the method, it's thrown away - as are all local variables.
Worse, you then close the form!

Instead of trying to create a new instance, use the current one, and create a property in your form which allows the outside world to access it.
C#
        public class id
            {
            public string Name { get; set; }
            }
...
        public id Id { get; private set; } = new id();
        private void Button_Click(object sender, EventArgs e)
            {
            Id.Name = txtshenimi.Text;
            this.Close();
            }
Then when your form closes, the calling code - assuming it used ShowDialog to display the form - can access the id as necessary.
 
Share this answer
 
Comments
[no name] 14-Jun-20 6:42am    
My friend, what does this code represent, cause is a little bit confusing ?
public class id
{
public string Name { get; set; }
}
...
public id Id { get; private set; } = new id();
OriginalGriff 14-Jun-20 6:59am    
It declares Name as a public string property of the id class, and provides a backing field automatically (it's the same as your version, but simpler and easier to read - there is no need for the line:

private string name;

as the system handles the field for you automatically.

"..." is called an ellipsis and it indicates that there may be other code - yours - between the two related pieces I'm showing you. You don't enter "..." into you application!

Then it declares a public id property called Id, which can only be changed from within the class (as the setter is private it can't be accessed from outside the form, only the getter can). It automatically creates a new instance of the id class when the form is created, and assigns it to the property.
Take a look at your code:
C#
//form 1
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
...//skiped lines
}

//form 2
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
...//skiped lines
}

Every time you're creating new instance of id class - level of scope: method.

I'd suggest to read about: Scope of Variables in C# - GeeksforGeeks[^]

If you would like to create single instance of id class and use it in each window of your application, you have to declare a variable in Program class or in your custom static class (you have to add it before):
C#
static class MyHelper
{
    public static id SingleId;
}


Finally:
C#
//write 
MyHelper.SingleId = new id();
MyHelper.SingleId.Name = txtshenimi.Text;

//read
id sid = MyHelper.SingeId;
txtshenimi.Text = sid.Name;


For further details, please see: Access Modifiers - C# Programming Guide | Microsoft Docs[^]

I'd suggest to start with basics: C# Tutorials | Microsoft Docs[^]

Seems, you've created WPF application (i see RoutedEventArgs in click event). Note, that WPF is not the same as WinForm! See: WPF vs. WinForms - The complete WPF tutorial[^] Do not mix them!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900