get / set and sevral values

  • Thread starter Thread starter Zach
  • Start date Start date
Z

Zach

Is it possible to use "get" and "set"
where "value" is say three ints.
If so, is there sample code anywhere?

Adrian.
 
Not as a single property;
you could:
a: Write a Get method that has 3 "out" params, and a Set method with 3
params
b: use an array
c: create a struct with the 3 values

Marc
 
Could you please tell me how to modify the code so it will work?

using System;
using System.Collections;

namespace trial_two
{

public class Class1
{
static void Main(string[] args)
{
HashArray ha = new HashArray();
Display d = new Display(22,33);
ha.hash = d; // error here ********
}

public class HashArray
{
public Hashtable hash = new Hashtable();
Display this[int loc]
{
get
{
return hash[loc] as Display;
}
set
{
hash.Add(loc, value);
}
}
}
}

public class Display
{
int arg1;
int arg2;

public Display(int arg1, int arg2)
{
this.arg1 = arg1;
this.arg2 = arg2;
}
}
}
 
Adrian said:
Could you please tell me how to modify the code so it will work?

Well, it's not clear what it's meant to be doing.

The reason the code doesn't compile is that you're trying to assign the
value of d, which is a Display, to ha.hash, which is a Hashtable. It's
like saying:

Hashtable hash = new Display(22, 33);

What were you trying to do with that line?
 
Hi John,

Sorry I left out the [1] in my trial code.

I was sorting out some Code. In an endeavor to do that
I was breaking the Code down into component logics
and made this silly mistake. Must have been a bit tired.

So I have no question at this stage. Again apologies.

Adrian.
 
John

In case you are wondering what on earth I am
doing, the code below was the trial code. I
was sorting out how to get several values
into "value" in a get / set context.

Adrian.

using System;
using System.Collections;

namespace trial_two
{

public class Class1
{
static void Main(string[] args)
{
HashArray ha = new HashArray();
Display d = new Display(22,33);
ha.hash[1] = d;
d.show(d);
}

public class HashArray
{
public Hashtable hash = new Hashtable();
Display this[int loc]
{
get
{
return hash[loc] as Display;
}
set
{
hash.Add(loc, value);
}
}
}
}
public class Display
{
int arg1;
int arg2;

public Display(int arg1, int arg2)
{
this.arg1 = arg1;
this.arg2 = arg2;
}
public void show(Display dis)
{
Console.WriteLine("{0} {1}", dis.arg1, dis.arg2);
}
}
}
 
John,

Is this OK / can it be improved?
(I does work).

Adrian.



using System;
using System.Collections;

namespace trial_two
{

public class Class1
{
static void Main(string[] args)
{
HashArray ha = new HashArray();
Display d = new Display(22,33);
ha.hash[1] = d;
Display di = ha.hash[1] as Display;
di.show();
}

public class HashArray
{
public Hashtable hash = new Hashtable();
Display this[int loc]
{
get
{
return hash[loc] as Display;
}
set
{
hash.Add(loc, value);
}
}
}
}
public class Display
{
int arg1;
int arg2;

public Display(int arg1, int arg2)
{
this.arg1 = arg1;
this.arg2 = arg2;
}
public void show()
{
Console.WriteLine("{0} {1}", arg1, arg2);
}
}
}
 
Re code that I posted last
What is the reason that I cannot write ha[1] = d?
 
Adrian said:
Re code that I posted last
What is the reason that I cannot write ha[1] = d?

Because the default access level is private. If you make the property
public (or even just internal) it'll be fine.
 
Adrian said:
Re code that I posted last
What is the reason that I cannot write ha[1] = d?

What if you try

ha.add(1, d);
Display di = (Display)ha.ContainsKey(1);
 
John, would the code below represent "cloning"?

using System;
using System.Collections;

namespace trial_two
{

public class Class1
{
static void Main(string[] args)
{
HashArray ha = new HashArray();

ha[1] = new Display(22,33);;
ha[2] = new Display(44,55);;

for(int i = 1 ; i < 3; i++)
(ha as Display).show();
}
}

public class HashArray
{
public Hashtable hash = new Hashtable();
public Display this[int loc]
{
get
{
return hash[loc] as Display;
}
set
{
hash.Add(loc, value);
}
}
}

public class Display
{
public int arg1;
public int arg2;

public Display(int arg1, int arg2)
{
this.arg1 = arg1;
this.arg2 = arg2;
}
public void show()
{
Console.WriteLine("{0}, {1}", arg1, arg2);
}
}
}
 
This code demonstrates the principle better.
Is this "Prototyping"?

using System;
using System.Collections;

namespace trial_two
{

public class Class1
{
static void Main(string[] args)
{
HashArray ha = new HashArray();

ha[1] = new Display(22,33);;
ha[2] = new Display(44,55);;

for(int i = 1 ; i < 3; i++)
Console.WriteLine("{0}, {1}",ha.arg1, ha.arg2);
}
}

public class HashArray
{
public Hashtable hash = new Hashtable();
public Display this[int loc]
{
get
{
return hash[loc] as Display;
}
set
{
hash.Add(loc, value);
}
}
}

public class Display
{
public int arg1;
public int arg2;
public Display(int arg1, int arg2)
{
this.arg1 = arg1;
this.arg2 = arg2;
}
}
}
 
Adrian said:
This code demonstrates the principle better.
Is this "Prototyping"?

Prototyping is just writing a quick test program without going through
all the proper error handling, localization etc, just to test whether
or not a concept or technology works.
 
Adrian said:
John, would the code below represent "cloning"?

Not really - I'm not sure which bit of it you'd call cloning.

Note that you don't need the cast to Display when you call show(), as
the HashArray indexer is of type Display.
 
Is any abstract class a prototype?

Jon Skeet said:
Prototyping is just writing a quick test program without going through
all the proper error handling, localization etc, just to test whether
or not a concept or technology works.
 
Adrian said:
Is any abstract class a prototype?

Not in the way I normally use the word, no. Others use the word that
way sometimes though. I tend to find it brings too much confusion, so I
stick with prototype meaning "early testing version" and use "abstract
class" when I mean abstract class.
 
Thank you John.
Adrian.

Jon Skeet said:
Not in the way I normally use the word, no. Others use the word that
way sometimes though. I tend to find it brings too much confusion, so I
stick with prototype meaning "early testing version" and use "abstract
class" when I mean abstract class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top