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
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