8 Replies - 795 Views - Last Post: 17 September 2015 - 06:52 PM Rate Topic: -----

#1 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 03:44 PM

I feel stupid that I can't figure this out, and I've been searching for a while now. I'll show a rough example.

Class Boy
{
     private string Name = "Chris";

     public void PrintName()
     {
          Console.WriteLine(Name);
     }
}

Class Girl
{
     private string HerName = "Sally";

     public void PrintName()
     {
          Console.WriteLine(HerName + " loves " + [how can I display Chris here using the variable from my class Boy?])
     }
}



I don't want to pass Boy into Girl :whistling:.. haha. Seriously though, I'm trying to think of another way to do this because imagine if Boy was populated with a ton and ton of stuff, it would require a lot of overhead (I believe) to copy the entire instance of the class into the Girl class, only to access one variable. I'm wondering how I go about just passing in the variable.

Thanks much!

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing Private Member Variable From One Class to Another

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15806
  • View blog
  • Posts: 63,315
  • Joined: 12-June 08

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 03:46 PM

You use a property.

https://msdn.microso...y/x9fsa0sw.aspx
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 03:49 PM

As you might have guessed, you can't do this. private has specific meaning: only things in that class can access a private member.

Of course, even if you make it public or internal, you still can't access that field from another class. It's an instance field, not a static field. Therefore, it only exists on an instance of a class, not the class itself.

Think about it this way. Boy is a class. It's like blueprints. No "Boy" actually exists until you call new Boy(). Therefore, there's nothing to get. The variable Name doesn't exist until you make a new boy.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 03:51 PM

There are tutorials linked in my signature block, both for classes and properties. They should help you out.
Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 03:55 PM

Just saw something else:

Quote

I'm trying to think of another way to do this because imagine if Boy was populated with a ton and ton of stuff, it would require a lot of overhead (I believe) to copy the entire instance of the class into the Girl class


You've got a fundamental misunderstanding of how classes work here. Classes are "reference types". When you pass an instance of a class, you're not passing the whole thing. You're passing what amounts to a simple integer. That's it. Behind the scenes, you're actually passing a pointer (location in memory). The system is smart enough to follow that pointer when you use it.

So, there's literally no overhead to passing around a class instance. You should embrace that style of programming.

As a thought experiment, consider the following. Here's the class we'll use:

public class MyClass
{
	public string SomeField = "Test";
}


Then consider these methods:

public static void Main()
{
	MyClass c = new MyClass();
	Console.WriteLine(c.SomeField);
	AlterMyInstance(c);
	Console.WriteLine(c.SomeField);
}

public static void AlterMyInstance(MyClass instance)
{
	instance.SomeField = "Not a test";
}


What do you think will be printed on the second Console.WriteLine?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 04:01 PM

Also, all the properties should be defined in an interface for a Person.
I wouldn't even make a boy and girl class. Male or Female is just a trait of a person

iPerson
{
   string name;
   DateTime DateTimeOfBirth;
   Gender MaleFemale;
   Color HairColor;
   Person Parent1;
   Person Parent2;
}

public class Person : iPerson
{

}

Was This Post Helpful? 1
  • +
  • -

#7 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 04:38 PM

Thanks guys, and I understand I should use an interface for this example, but I was more trying to convey the concept I'm working with while just using a simple example.

Oh, I didn't know that C# passes classes as references like that instead of by value!! Then it wouldn't be bad to do this:

Class Boy
{
     private string Name = "Chris";
     public void PrintName()
     {
          Console.WriteLine(Name);
     }
}

Class Girl
{
     private string HerName = "Sally";

     Girl(Boy BoyReference)
     {
          BoyName = BoyReference.Name;

     public void PrintName()
     {
          Console.WriteLine(HerName + " loves " + BoyName;
     }
}



Correct?

Also, I'm assuming the second WriteLine will display "Not a test." I'm thinking that because if it is pointing to the actual address in memory of the object your accessing, it should directly change it, correct?

Oh and I know it doesn't exist until I Boy Chris = new Boy() and Girl Sally = new Sally(Chris) but I was just trying to keep it quick.

This post has been edited by ChrispyChris: 17 September 2015 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 04:57 PM

It would still be bad to do such a horrible thing where boy and girl are totally seperate and duplicitive. At the very least just directly inherit from a person class


class Person
{
   string name;
}

class Boy Person
{
}

class Girl : Person
{
}


Please please please work the tutorial on objects and get a handle on inheritance.

Now anyplace you need to use one or the other you can specify a Person object then later provide a Boy or a Girl object.
Was This Post Helpful? 1
  • +
  • -

#9 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Accessing Private Member Variable From One Class to Another

Posted 17 September 2015 - 06:52 PM

Well I do, but I was just trying to break down a concept. Here is my last little thing I wrote playing around with inheritance:

IAnimal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    interface IAnimal
    {
        int Age { get; set; }
        int Legs { get; set; }
        string Name { get; set; }

        void speak();
    }
}


Bird
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Bird : IAnimal
    {
        public int Age { get; set; }
        public int Legs { get; set; }
        public string Name { get; set; }

        public void speak()
        {
            Console.WriteLine("CACAAAA CACAAAA CACAAAA");
        }
    }
}


Cat
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Cat : IAnimal
    {
        public int Age { get; set; }
        public int Legs { get; set; }
        public string Name { get; set; }

        public void speak()
        {
            Console.WriteLine("MEOW MEOW MEOW");
        }
    }
}


Dog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Dog : IAnimal
    {
        public int Age { get; set; }
        public int Legs { get; set; }
        public string Name { get; set; }

        public void speak()
        {
            Console.WriteLine("BARK BARK BARK");
        }
    }
}


Person
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Person
    {
        public void spank(IAnimal _animal)
        {
            IAnimal animal = _animal;
            animal.speak();
        }
    }
}


Zoo
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Zoo
    { 
        List<IAnimal> myList = new List<IAnimal>();

        public void AddToList(IAnimal _animal)
        {
            myList.Add(_animal);
        }

        public void PrintMyList()
        {
            foreach (var animal in myList)
            {
                animal.speak();
            }
        }

    }
}


Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacePractice
{
    class Program
    {
        static void Main(string[] args)
        {
            Person AbusiveBrian = new Person();
            Zoo PortlandZoo = new Zoo();
            Dog Puppy = new Dog() {Age = 10, Legs = 4, Name = "Spot"};
            Cat Garfield = new Cat() {Age = 24, Legs = 4, Name = "Garfield"};
            Cat Chester = new Cat() {Age = 50, Legs = 4, Name = "Chester"};
            Bird Crow = new Bird() {Age = 100, Legs = 2, Name = "Death"};
            IAnimal Pet = new Bird();

            //Puppy.speak();
            //Garfield.speak();
            //Chester.speak();
            //Crow.speak();
            //Pet.speak();

            PortlandZoo.AddToList(Puppy);
            PortlandZoo.AddToList(Garfield);
            PortlandZoo.AddToList(Chester);
            PortlandZoo.AddToList(Crow);
            PortlandZoo.AddToList(Pet);
            PortlandZoo.PrintMyList();
            Console.ReadLine();

            Console.WriteLine("Now he beats the animals!");
            AbusiveBrian.spank(Puppy);
            AbusiveBrian.spank(new Cat());

            Console.ReadLine();
        }
    }
}



Like I said, sometimes I just come in here trying to come at a problem from another direction so I can make sure I fully understand a concept. I can make things more complicated than they need to be sometimes. I appreciate the help though guys :)!

tlhIn`toq thanks for those tutorials too. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1