Showing posts with label double. Show all posts
Showing posts with label double. Show all posts

Saturday, March 31, 2012

String to convert into Double

Hello All,

I am having string object coming from a DataSet. It can contain either the Numeric Strings (like "123","123.23",...) or even the Character string (Like "America","Britain"...).

Now, I am assigning this value to another column in the dataset whose datatype is Doube. Now, I wanted to check before assigning whether whatever the String I am getting from the Source column can it be converted to Double or not?

i.e I want to know is there any object/method in .Net Framework using which I can know whether I can convert one datatype to another datatype or not?

Any help regarding this would be great.

Thanks in Advance,
Areef.Hi Areef,
I am not familiar with such an object, but you can use Double.TryParse to perform your task.
Hi pmd_areef,

Try the following for testing if value is a Double. Example:


[C#]
/// <summary>
/// Is the value supplied numeric (Integer, Double).
/// </summary>
/// <param name="number">Value to test</param>
/// <returns></returns>
public bool IsNumeric(object number)
{
double tempResult;
return IsNumeric(number, out tempResult);
}

public bool IsNumeric(object number, out double result)
{
return Double.TryParse(number.ToString(), System.Globalization.NumberStyles.Any,
System.Globalization.NumberFormatInfo.InvariantInfo, out result);
}


Hope this helps.

Wednesday, March 28, 2012

String with double quotes

Hello Everyone,

I have a little problem. I have to insert a string into a column of a database object.

For exmaple my string is : Dim filename as string = "C:\Temp\Hello.txt".. but my problem is I have to insert into db "path="C:\Temp\Hello.text"".

I don't know how to include double quote in my string. Please help.

to add a quote character to a quoted string you need to escape the quote

in vb this is done by doubling up the quote

for example to get this string:

"this string contains one quote character -> " <- see"

you actually need to use:

"this string contains one quote character -> "" <- see"

"path=""C:\Temp\Hello.text"""


Hellombanavige,

Thanks for your reply.. Your answer solved half of my problem. Now suppose if i have to add a value in between those string. For example

From your example "this string contains one quote character -> "" . If i have a string variable say dim x as string = "Hello" and I have to add this in between the present string and get the double quotes. How do i do this. Thanks a lot.


any doublequote that needs to be treated as a literal character needs to be doubled up.

dim x as string = "Hello"

dim y as string = "Testing literal quote -> """ & x & """ <-another literal quote..."

y actually equals: Testing literal quote -> "Hello" <-another literal quote...


Thanks for your help...

String with double quotes

I have a label control on my form. I'm assigning a text to the label at
runtime.
The problem is that the string that I'm trying to assign also contains text
in double quotes.So when I try n assign this to the label I face a problem.
my code is using vb.net

Please help me out in getting this thing done, Is there any in-built
function or property to handle the same?

thanx
GaneshAre you getting any specific error messages?

You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
before putting it into your label.

Mun

--
Munsifali Rashid
http://www.munit.co.uk/blog/

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
All you need to do is use "double double" quotes (a term I just made up):

For example:

lblMain.text = """Hello World """

will show on your screen as:

"Hello World"

lblMain.text = """" + "Hello World" + """"

will produce the same result!

Hope this helps you!

Fred

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
text
> in double quotes.So when I try n assign this to the label I face a
problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
Your best bet would probably be to HTML-encode the string. e.g.:

someLabel.Text = Server.HtmlEncode(yourString)

This will replace the quotes with " named entity, as well as escaping
other HTML that might potentially cause problems (e.g.: unexpected page
modifications or cross-site scripting).

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
No I don't get any specific error message.
The problem is that I'm reading this from a source where the text contains
phrases that are mentioned in double quotes.
So i dont know how do I handle this.

"Munsifali Rashid" wrote:

> Are you getting any specific error messages?
> You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
> before putting it into your label.
> Mun
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
> "Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
> news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> >I have a label control on my form. I'm assigning a text to the label at
> > runtime.
> > The problem is that the string that I'm trying to assign also contains
> > text
> > in double quotes.So when I try n assign this to the label I face a
> > problem.
> > my code is using vb.net
> > Please help me out in getting this thing done, Is there any in-built
> > function or property to handle the same?
> > thanx
> > Ganesh
>
Hi Ganesh,

There are two ways to have double quote sign in string:

1) Dim strQuote As String = "text""" ' It shows text"

2) Dim strQuote As String = "text" & Chr(34) ' It also
shows text"

HTH

Elton Wang
elton_wang@.hotmail.com

>--Original Message--
>I have a label control on my form. I'm assigning a text
to the label at
>runtime.
>The problem is that the string that I'm trying to assign
also contains text
>in double quotes.So when I try n assign this to the label
I face a problem.
>my code is using vb.net
>Please help me out in getting this thing done, Is there
any in-built
>function or property to handle the same?
>thanx
>Ganesh
>.

String with double quotes

I have a label control on my form. I'm assigning a text to the label at
runtime.
The problem is that the string that I'm trying to assign also contains text
in double quotes.So when I try n assign this to the label I face a problem.
my code is using vb.net
Please help me out in getting this thing done, Is there any in-built
function or property to handle the same?
thanx
GaneshAre you getting any specific error messages?
You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
before putting it into your label.
Mun
Munsifali Rashid
http://www.munit.co.uk/blog/
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
All you need to do is use "double double" quotes (a term I just made up):
For example:
lblMain.text = """Hello World """
will show on your screen as:
"Hello World"
lblMain.text = """" + "Hello World" + """"
will produce the same result!
Hope this helps you!
Fred
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
text
> in double quotes.So when I try n assign this to the label I face a
problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
Your best bet would probably be to HTML-encode the string. e.g.:
someLabel.Text = Server.HtmlEncode(yourString)
This will replace the quotes with " named entity, as well as escaping
other HTML that might potentially cause problems (e.g.: unexpected page
modifications or cross-site scripting).
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
No I don't get any specific error message.
The problem is that I'm reading this from a source where the text contains
phrases that are mentioned in double quotes.
So i dont know how do I handle this.
"Munsifali Rashid" wrote:

> Are you getting any specific error messages?
> You may need to encode your string using System.Web.HttpUtilites.HtmlEncod
e,
> before putting it into your label.
> Mun
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
> "Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
> news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>
>