Click here to Skip to main content
15,918,125 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem getting a value from a gridview cell Pin
Furi Goled25-May-07 9:48
Furi Goled25-May-07 9:48 
GeneralRe: Problem getting a value from a gridview cell Pin
Furi Goled10-Jun-07 17:47
Furi Goled10-Jun-07 17:47 
Questionperformance problem Pin
mdzieg25-May-07 5:08
mdzieg25-May-07 5:08 
AnswerRe: performance problem Pin
Manoj Kumar Rai25-May-07 5:23
professionalManoj Kumar Rai25-May-07 5:23 
GeneralRe: performance problem Pin
mdzieg25-May-07 5:35
mdzieg25-May-07 5:35 
QuestionC# Download Manager (Browser Integration) Pin
Barguast225-May-07 5:05
Barguast225-May-07 5:05 
AnswerRe: C# Download Manager (Browser Integration) Pin
Judah Gabriel Himango25-May-07 6:30
sponsorJudah Gabriel Himango25-May-07 6:30 
QuestionBeginner: references and calls between objects Pin
Christian Bailey25-May-07 5:00
Christian Bailey25-May-07 5:00 
Thanks to anybody who feels like helping the beginner.

I want to create a system wherein objects are accessing each other and I struggle to understand reference passing. For example, I wish I could write a function that specifically targets one instance of a class, but I only see how to write functions that take object references as arguments such that I have to call the function with the correct reference each time. I show the way I tried, right in the middle of my code below with all the ????. How do I aim functions at specific objects, given that I only get to write function definitions when defining a class, and the other class's objects don't even exist yet? And, in my code, do I correctly comment the things which do seem to work as intended?

Also, there seems to be nothing in the books about this. Doesn't this trip up everybody when they first try to access one object from another? There's stuff about the Observer pattern and deligates and ref modifiers and values versus references, but nothing directly about this basic point. Is there a name or keyword for this topic or for a discussion of this?

By the way, I understand having objects talk directly can tangle things and is a mixed blessing. I still want to grasp the idea.

I'm using:
Programming C# (Liberty)
The C# Programming Language (Hejlsberg...)
C# In a Nutshell (Drayton...)
C# Essentials (Albahari...)
Murach's C# (Murach)
Visual C# 2005 Demystified (Kent)
The Object-Oriented Thought Process (Weisfeld)
as well as several C++ books like Eckel and Prata and the MSDN and CodeProject forums.

Thanks!!






using System;

// Here is an exercise between objects, that studies reference passing.
// The Main function instantiates User, Counter and Reporter objects and
// calls on some of their members. Since the objects are in scope in Program,
// no passing of references is needed. However, when Main asks a Reporter's
// method to call members of User and Counter objects, Main must pass
// Reporter references to the User and Counter objects. But the Reporter
// needs no reference to reach method in static FunctionHolder class.
// ???? I wanted to define a method that specifically accesses Counter2
// ???? instance of Counter but don't see how to do so! See line 95.

namespace ClassesAndObjects
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Initialize all.");
// Construct 3 Users, 2 Counters and a Reporter
Reporter ConcreteReporter = new Reporter();
User User1 = new User();
User User2 = new User();
User User3 = new User();
Counter Counter1 = new Counter();
Counter Counter2 = new Counter();
// Try a bunch of things, reporting various properties each time
// This function call needs object references but no static class ref:
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User1.Make6()");
// User1.Make6() is in scope, no reference passing required
User1.Make6();
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User1.X = 9");
User1.X = 9;
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do Counter1.Increment()");
Counter1.Increment();
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User1.X = Counter1.I");
User1.X = Counter1.I;
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do Counter2.Increment() Twice");
Counter2.Increment();
Counter2.Increment();
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User2.XGetsI(Counter2)");
// Must pass reference, which tells User2 which Counter to call.
User2.XGetsI(Counter2);
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User2.X = Counter2.I");
User2.X = Counter2.I;
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do Counter2.Increment()");
Counter2.Increment();
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User3.XGetsIFromCounter2(Counter1), it uses Counter1");
// Must pass reference, which (unfortunately) is what chooses the counter
User3.XGetsIFromCounter2(Counter1);
Console.WriteLine("Look, did not read Counter2 as intended:");
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
Console.WriteLine("Do User3.XGetsIFromCounter2(Counter2), it uses Counter2");
// This time, pass reference to the counter that was intended all along
User3.XGetsIFromCounter2(Counter2);
Console.WriteLine("Look, it read Counter2, just because of the reference:");
ConcreteReporter.Report(User1, User2, User3, Counter1, Counter2);
// Keep console window open for examination.
Console.ReadLine();
}
}
public class User
// Objects of this class can directly use a property in objects of Counter class
// and have members the Reporter and Program objects can directly call
{
private int x;
public User()
{
x = 5;
}
public void Make6()
{
x = 6;
}
// NOTE this function needs a reference to the Counter object
public void XGetsI(Counter CounterN)
{
// Expression uses the reference I passed in as argument
X = CounterN.I;
}
// ????????????????????????????????????????????????????????????
// How could I define a function that specifically targets Counter2?
// Lines below just use "Counter2" as a local identifier
// the same way "CounterN" is used a few lines above so this code
// does NOT specify Counter2, which wouldn't exist here anyway.
// ???????????????????????????????????????????????????????????
public void XGetsIFromCounter2(Counter Counter2)
{
X = Counter2.I; // Unfortunately this doesn't mean my Counter2 object!
}
public int X
{
set
{
x = value;
}
get
{
return x;
}
} // end property X
} // end class User
public class Counter
// Objects of this class have a property and a function which
// User, Reporter and Program objects will directly call
{
private int i;
public Counter()
{
i = 0;
}
public int I
{
get
{
return i;
}
}
public void Increment()
{
i = i + 1;
}
} // end class Counter
public class Reporter
// Function in an object of this class will directly call members of User and Counter objects
// and of the static class FunctionHolder. This function requires User and Counter object
// references as arguments because those objects are only instantiated in another class at
// runtime and do not yet exist during the definition of this class. But it does not need
// to be passed a reference to the static class FunctionHolder because that doesn't wait
// for a runtime instantiation.
{
public Reporter()
{
// No data members to initialize
// Could put singleton construction in here
}
public void Report(User User1, User User2, User User3, Counter Counter1, Counter Counter2)
{
Console.WriteLine("U1.X={0}, U2.X={1}, U3.X={2}, C1.I={3}, C2.I={4}, Fifty={5}\n", User1.X, User2.X, User3.X, Counter1.I, Counter2.I, FunctionHolder.Fifty());
} // end class Reporter
}
public static class FunctionHolder
// This class is static and others use its method with just ClassName.MethodName().
{
public static int Fifty()
{
return 50;
}
} // end class FunctionHolder
} // end namespace ClassesAndObjects



AnswerRe: Beginner: references and calls between objects Pin
Judah Gabriel Himango25-May-07 6:27
sponsorJudah Gabriel Himango25-May-07 6:27 
GeneralRe: Beginner: references and calls between objects Pin
Christian Bailey25-May-07 7:28
Christian Bailey25-May-07 7:28 
QuestionHelp with the run an executable program of a number of times Pin
Anka_Ame25-May-07 4:41
Anka_Ame25-May-07 4:41 
AnswerRe: Help with the run an executable program of a number of times Pin
Judah Gabriel Himango25-May-07 4:49
sponsorJudah Gabriel Himango25-May-07 4:49 
AnswerRe: Help with the run an executable program of a number of times Pin
kubben25-May-07 4:49
kubben25-May-07 4:49 
AnswerRe: Help with the run an executable program of a number of times Pin
Luc Pattyn25-May-07 6:04
sitebuilderLuc Pattyn25-May-07 6:04 
GeneralRe: Help with the run an executable program of a number of times Pin
Anka_Ame25-May-07 4:58
Anka_Ame25-May-07 4:58 
GeneralRe: Help with the run an executable program of a number of times Pin
Manoj Kumar Rai25-May-07 5:15
professionalManoj Kumar Rai25-May-07 5:15 
AnswerRe: Help with the run an executable program of a number of times Pin
Wes Aday25-May-07 9:33
professionalWes Aday25-May-07 9:33 
QuestionFlowLayoutPanel and ScrollPositions Pin
zaboboa25-May-07 4:32
zaboboa25-May-07 4:32 
AnswerRe: FlowLayoutPanel and ScrollPositions Pin
Judah Gabriel Himango25-May-07 4:49
sponsorJudah Gabriel Himango25-May-07 4:49 
GeneralRe: FlowLayoutPanel and ScrollPositions Pin
zaboboa25-May-07 5:22
zaboboa25-May-07 5:22 
Questionhow to ingnore some characters in serach by Regular Expressions Pin
hdv21225-May-07 3:46
hdv21225-May-07 3:46 
AnswerRe: how to ingnore some characters in serach by Regular Expressions Pin
Judah Gabriel Himango25-May-07 4:44
sponsorJudah Gabriel Himango25-May-07 4:44 
Questionproblem in inserting data Pin
Mujz.........25-May-07 3:46
Mujz.........25-May-07 3:46 
AnswerRe: problem in inserting data Pin
Judah Gabriel Himango25-May-07 4:14
sponsorJudah Gabriel Himango25-May-07 4:14 
GeneralRe: problem in inserting data Pin
Mujz.........26-May-07 8:48
Mujz.........26-May-07 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.