Using a String as the name of a struct's field

J

Jeoryos

Well here is some code that will help explain the situation:

I first make a struct that actually will be used to save some server
informations:
struct Server {
public String IP;
public int CurrentBandwidthUsage;
public int MonthlyNetUsage;
public double Cost;

public Server(String ip, int CurrBandUse, int MonthNetUse,
double cost) {
this.IP = ip;
this.CurrentBandwidthUsage = CurrBandUse;
this.MonthlyNetUsage = MonthNetUse;
this.Cost = cost;
}

public object getValue(int ValueID) {

object _returnValue = new Object();
switch (ValueID) {
case 0:
_returnValue = MonthlyNetUsage;
break;
case 1:
_returnValue = CurrentBandwidthUsage;
break;
case 3:
_returnValue = Cost;
break;
}
return _returnValue;
}

}

Then I want to do some kind of sorting to an ArrayList witch contains
Server structs:

private static ArrayList partialSort(ArrayList a) {
object _min = ((Server)a[0]).getValue(0);
for (int i = 1; i < a.Count; i++) {
_min = ((Server)a).getValue(0);
if (((Server)a).MonthlyNetUsage <= _min) {
_min = ((Server)a).getValue(0);
a.Insert(0, (Server)a);
a.RemoveAt(i + 1);
}
}
return a;

}

Ofcourse the above does not work as I can't use the <= operator with
objects. The getValue method of the struct returns an object as I don't
know if the return type will be an int or a double type (in this
situation, or generally any other type of number).

I could of course create a getIntValue and a getDoubleValue methods to
solve this problem but I wouldn't like to use many if statements in the
for loop of the partialSort method, as I'll have to check every time
the method to use. I was woundering if I could use a String str witch
has the name of the field in order to do something like: _min =
((Server)a).str

Any suggestions or workarounds?

Thanks in advance,
Jeoryos
 
J

justncase80

It sounds to me like you have a fundamental flaw in the logic of what
you are trying to do. Its really hard for me to imagine a situation
where you would want to get the value of a particular field based on
some int value... You might need to show the code that is creating the
server object and calling the GetValue(...) method to make any sense of
this.

Furthermore, in this situation I would say that using a class would be
better than a structure. I'd really only use a struct if I needed
strict sizes of my datastructures. If you're needing to save the
objects to a file you can just use serialization, xml or binary. Why on
earth are you needing to get a value based on an int? Why not just get
the value out of a property?

~justin
 
J

Jeoryos

I do have the code that makes use of the getValue() method. See the
implementation of the partialSort witch actually get an arraylist of
server objects and makes some kind of sorting in order to have the
Servers with the min value of a property at the top of the arraylist.

I could of cource create CostBasedPartialSort and generaly a
partialSort method for every property that I would like the sort to be
based on but I'm looking for "global" sorting method (or let's say more
spicy)!

At the end I don't care about the size of the datastructures. I just
thought that there wasn't any need of using a class. Do really think
that I have to? Generaly what are the advantages - disadvantages of
the class vs struct in this case?
It sounds to me like you have a fundamental flaw in the logic of what
you are trying to do. Its really hard for me to imagine a situation
where you would want to get the value of a particular field based on
some int value... You might need to show the code that is creating the
server object and calling the GetValue(...) method to make any sense of
this.

Furthermore, in this situation I would say that using a class would be
better than a structure. I'd really only use a struct if I needed
strict sizes of my datastructures. If you're needing to save the
objects to a file you can just use serialization, xml or binary. Why on
earth are you needing to get a value based on an int? Why not just get
the value out of a property?

~justin

Well here is some code that will help explain the situation:

I first make a struct that actually will be used to save some server
informations:
struct Server {
public String IP;
public int CurrentBandwidthUsage;
public int MonthlyNetUsage;
public double Cost;

public Server(String ip, int CurrBandUse, int MonthNetUse,
double cost) {
this.IP = ip;
this.CurrentBandwidthUsage = CurrBandUse;
this.MonthlyNetUsage = MonthNetUse;
this.Cost = cost;
}

public object getValue(int ValueID) {

object _returnValue = new Object();
switch (ValueID) {
case 0:
_returnValue = MonthlyNetUsage;
break;
case 1:
_returnValue = CurrentBandwidthUsage;
break;
case 3:
_returnValue = Cost;
break;
}
return _returnValue;
}

}

Then I want to do some kind of sorting to an ArrayList witch contains
Server structs:

private static ArrayList partialSort(ArrayList a) {
object _min = ((Server)a[0]).getValue(0);
for (int i = 1; i < a.Count; i++) {
_min = ((Server)a).getValue(0);
if (((Server)a).MonthlyNetUsage <= _min) {
_min = ((Server)a).getValue(0);
a.Insert(0, (Server)a);
a.RemoveAt(i + 1);
}
}
return a;

}

Ofcourse the above does not work as I can't use the <= operator with
objects. The getValue method of the struct returns an object as I don't
know if the return type will be an int or a double type (in this
situation, or generally any other type of number).

I could of course create a getIntValue and a getDoubleValue methods to
solve this problem but I wouldn't like to use many if statements in the
for loop of the partialSort method, as I'll have to check every time
the method to use. I was woundering if I could use a String str witch
has the name of the field in order to do something like: _min =
((Server)a).str

Any suggestions or workarounds?

Thanks in advance,
Jeoryos
 
J

Jon Skeet [C# MVP]

Jeoryos said:
At the end I don't care about the size of the datastructures. I just
thought that there wasn't any need of using a class. Do really think
that I have to? Generaly what are the advantages - disadvantages of
the class vs struct in this case?

Classes and structs behave very differently - classes are reference
types, and structs are value types. I haven't yet gotten around to
writing this difference up fully (it's tricky to do in an accurate but
readable way) but if you read
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html
that should help a bit.

Using a class should be your "default" position - it's rare to need to
define your own struct.
 
J

Jeoryos

Thanks! I'll have a look at your links, and I'll make use of a class
instead of the struct, but my main problem here haven't been solved!
 

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

Top