Exit Hashtable function...need help
Hello everyone,
I have a function that returns a hashtable. If there is a key missing from the hashtable, it needs to display (Console.WriteLine()) that the hashtable is missing a certain key.
this is 'Exit Sub' in vb.
i know its return in C#, how ever i am getting a complile error of "An object of a type convertible to 'System.Collections.Hashtable' is required".
if i do return false; i get cannot convert bool to Hashtable (makes sense...)
my my question is, how do i exit the remainder of the function?
i know i can use lable's, and would be quite happy to do so, if this is the correct way of doing this?
Thanks,
Justin
Re: Exit Hashtable function...need help
The difference is that in VB every function has an implicit local variable with the same name as the function itself. If you use Exit Function, which is BAD by the way, then it's equivalent to returning the value of that variable. If no value was ever assigned to that variable then that is equiavalent to returning Nothing.
C# functions have no such implicit variable so every function must return a value. If you don't want to return an object then you need to explicitly return null.
Having said that, your description sounds dubious. Can you show us this function?
Re: Exit Hashtable function...need help
Functions should return only meaningful values and should not display output of their own accord (unless that is the sole purpose of the function). If you encounter a circumstance under which you cannot return a value, and this circumstance is one that is likely to occur, you should return a value that indicates this condition.
Under exceptional circumstances, you may throw an exception; however, it is better to always return a value. Exceptions are inefficient and bad for code readability.
Re: Exit Hashtable function...need help
Hey,
And thanks for the replies...the function wont display the data, only return a Hashtable. it only display's data now for debugging only..
here is the function in question...
Code:
public static Hashtable ReturnRegistryValues(Hashtable Config) {
//Hashtable _regConfig = new Hashtable();
if (Config["reg_key"] == null) {
Console.WriteLine("reg_key directive missing! Check configuration file.");
// exit code here...
}
string _regKeyLocation = (string)Config["reg_key"];
RegistryKey _regKey = Registry.LocalMachine.OpenSubKey(_regKeyLocation);
if (_regKey == null) {
Console.WriteLine("The registry key: {0} does not exist!", Config["reg_key"]);
// exit code here...
} else {
foreach (DictionaryEntry de in Config) {
string regValue = (string)_regKey.GetValue((string)de.Value);
//_regConfig.Add(de.Key, regValue);
// need to loop through and update the Hashtable here...
}
}
return Config;
}
please mind the mess, im still trying to work it all out... :eek:
Re: Exit Hashtable function...need help
It doesn't make sense that you're passing in a Hashtable as a parameter and then returning the same Hashtable. The return type of the method should be void because the changes will be made to the Hashtable anyway. Alternatively you could have it return a bool that indicates some condition like whether the desired key was present or not, but unless you're going to create a second Hashtable and return that the return type should NOT be Hashtable.
Re: Exit Hashtable function...need help
Quote:
Originally Posted by jmcilhinney
It doesn't make sense that you're passing in a Hashtable as a parameter and then returning the same Hashtable. The return type of the method should be void because the changes will be made to the Hashtable anyway. Alternatively you could have it return a bool that indicates some condition like whether the desired key was present or not, but unless you're going to create a second Hashtable and return that the return type should NOT be Hashtable.
Ok, so i would then just pass a referenced hashtable?
so it would become:
public static void ReturnRegistryValues(ref Hashtable Config) {
}
??
Thanks for the advice :)
Re: Exit Hashtable function...need help
No, there's no need to pass the parameter by reference. A reference type object passed by value can still be changed. Passing it by reference means that you can assign a new object to the variable and that will affect the original variable too.
Re: Exit Hashtable function...need help
Quote:
Originally Posted by jmcilhinney
No, there's no need to pass the parameter by reference. A reference type object passed by value can still be changed. Passing it by reference means that you can assign a new object to the variable and that will affect the original variable too.
Yeah, thats what i thought. that way, memory is only being taken up by one hashtable, rather than 2...
Thanks,
Justin
Re: Exit Hashtable function...need help
Reference types passed by value do not get copied. The only way you can end up with the same object twice in memory is if you clone it manually.
Re: Exit Hashtable function...need help
Whether a parameter is passed by value or by reference relates to how the variable is treated, not how the object referred to by the variable is treated. When a parameter is passed by value the variable is copied, so you end up with two variables referring to the same object. A variable is basically just a 32-bit memory address regardless of the type of object it refers to. When a parameter is passed by reference a new variable is created that refers to the original. Thus a new 32-bit value is created regardless so there's no difference in the memory consumption.
It is when the parameter is a value type that it can be an issue, because the variable contains the object itself rather than a reference to the object. If you copy a value type variable, i.e. a structure, then it may be bigger than 32-bits. That said, good practice dictates that only small types be declared as structures so it should never be an issue.