# Rewrite

• ### Question

• User335504481 posted

Hi guys,

Can help me rewrite back the code below in proper way for VB?

```Public Function ConvertCompoundingPeriod2Freq(ByVal Compounding_Period As String) As Integer
Dim freq As Integer

Select Case Compounding_Period
Case "SMP"          ' Simple Compounding
freq = 1
Case "CONT"         ' Continuous Compounding
freq = 1000
Case "DAY"          ' Daily Compounding
freq = 365
Case "MON"          ' Monthly Compounding
freq = 12
Case "QURT"         ' Quarterly Compounding
freq = 4
Case "SEMI"         ' Semi-Annual Compounding
freq = 2
Case "ANNU"         ' Annual Compounding
freq = 1
Case Else
freq = -99
End Select

ConvertCompoundingPeriod2Freq = freq

End Function```

Regards,

Hafees

Thursday, May 8, 2014 4:53 AM

• User335504481 posted

Problem solved sir.. Thank you for replying.. :) I put the code as reference..

```       public int ConvertCompoundingPeriod2Freq(string Compounding_Period)
{
int freq = 0;

if (Compounding_Period == "SMP")
{
freq = 1;
}
else if (Compounding_Period == "CONT")
{
freq = 1000;
}
else if (Compounding_Period == "DAY")
{
freq = 365;
}
else if (Compounding_Period == "MON")
{
freq = 12;
}
else if (Compounding_Period == "QURT")
{
freq = 4;
}
else if (Compounding_Period == "SEMI")
{
freq = 2;
}
else if (Compounding_Period == "ANNU")
{
freq = 1;
}
else
{
freq = -99;
}
return freq;
}```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, May 12, 2014 3:53 AM

### All replies

• User753101303 posted

Hi,

The problem being ? At first sight it seems valid code. If this is just a style question, a thing you could think about is that every time you select a data according to a value, you can use a data structure instead (for example http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx rather than a select case. Also if the parameter is not valid you could throw an exception rather than to return an invalid value (ie the app could appear to work but would produce very strange result, it can be better to just fail if you have a programing error). Or rather than to provide a string (it's hard to guess which value to provide), you could use an enum (which will trigger intellisense when writing the calling code).

Please try to give some more details so that we can understand what you wanted to change (or what is the point you want to focus on for now)...

Thursday, May 8, 2014 5:49 AM
• User335504481 posted

Yea its a valid code.. Can help me write back this code in VB form? The 1 I post.. Thank you..

Thursday, May 8, 2014 5:59 AM
• User753101303 posted

Sorry but I still don't get it. It *is* VB code. Or do you mean that you want to copy/paste this code inside a Form ???

What is the problem you have ? Do you see some kind of error message when you try to use this code ??

Friday, May 9, 2014 4:50 AM
• User335504481 posted

Problem solved sir.. Thank you for replying.. :) I put the code as reference..

```       public int ConvertCompoundingPeriod2Freq(string Compounding_Period)
{
int freq = 0;

if (Compounding_Period == "SMP")
{
freq = 1;
}
else if (Compounding_Period == "CONT")
{
freq = 1000;
}
else if (Compounding_Period == "DAY")
{
freq = 365;
}
else if (Compounding_Period == "MON")
{
freq = 12;
}
else if (Compounding_Period == "QURT")
{
freq = 4;
}
else if (Compounding_Period == "SEMI")
{
freq = 2;
}
else if (Compounding_Period == "ANNU")
{
freq = 1;
}
else
{
freq = -99;
}
return freq;
}```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, May 12, 2014 3:53 AM
• User1140095199 posted

Hi Hafees,

Problem solved sir.. Thank you for replying.. :)

Could you POST some code here so that other members who view this thread can benefit from your REPLY.

If any of the above POST helped you do mark the post as Answer.

Thanks and Regards!

Monday, May 12, 2014 6:15 AM
• User753101303 posted

Great, mark whatever helped so that this thread is properly mark as closed. For future questions, note that its best to be always explicit ie rather than telling that you want to rewrite some code that seems valid, tell instead what benefit you want or if you had a problem in using this code perhaps from somewhere else in your app, tell what is the error message you have etc...

Monday, May 12, 2014 6:53 AM
• User-192893974 posted

I think you have asked for VB Code, Right?

Is it syntax of VB code - int freq = 0;

We need to use ";" at the end of line?

Wednesday, May 21, 2014 3:03 AM
• User753101303 posted

Ah, I thought you wanted to rewrite existing VB code in a better way rather than to port this code to C#. Be always explicit. As advised earlier:
- a data structure could be better
- if you pass Smp or XXx what should happen. It might be better to throw an exception to tell the parameter is invalid rather than to return 0 that you'll use in your calculation with unwanted effects. Using an enumation could be easier to know which Compounding_Periods are allowed without having to look at this method source code...

Wednesday, May 21, 2014 3:57 AM