Showing posts with label strings. Show all posts
Showing posts with label strings. 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.

String to integer conversion

Hello!

Class int has a method for conversion numeric values to strings (ToString() method). Is there any similar method or function, which converts a string into Int32 type?

Cheers!static method System.Int32.Parse()
Thanks!
Hello!

I tried to use the method you gave me, but got an error message: "System.FormatException: Input string was not in a correct format."
Maybe there is any special way I should use it?

Here's how I do it:


void Fill_End_Day(int auto, int aasta, int kuu, int paev) {
autorent.broneering wsProxy = new autorent.broneering();
System.Data.DataSet ds;
ds = wsProxy.JargmineHyiv(auto, aasta, kuu, paev);
int i;
for (i=paev; i<System.Int32.Parse(ds.Tables[0].Rows[0]["paev"].ToString()); i++)
DropDownList7.Items.Add(i.ToString());
}

Is there something wrong with it?

Cheers.
c# doesn't have cInt()?
I'm afraid it doesn't. Maybe there is some similar function, but I can't even assume in which class I should seek for it.
Sorry, it works now! It should have worked from the beginning, but there has been a stupid mistake in another palce.
Thank you!

String Tokenizing with ASP.NET?

Hi all,

I am somewhat new to the DOTNET world and I am wondering if there is an easy way to tokenize strings using asp.net.

With java, this is done quite easilly, but I have not seen anything in the .net class libraries that would work the same.

Any help is appreciated!

_Luke Dubya
_minneapolisOkay I'll be more specific:

I have cookies that have several values seperated by a comma ","

I want to parse the values into seperate tokens so I can then run a SQL statement with several OR statements to return the results in my datagrid.

the values of my cookies are product ids.

example: 4322-001,E322,8000DN,4317-001

Then I want to turn this into a sql statement:

SELECT * FROM sometable WHERE productid="4322-001" OR WHERE productid="E322" etc...

so thats what I'm trying to do. any ideas or help is much appreciated.

Thanks all!

_Luke Dubya
_Minneapolis
Substrings seems the best way to go (assume that you dont know how many product numbers you have in the cookie).

<a href"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclasssubstringtopic.asp">Here</a> is a link to set you off.

You want something on the lines of:


Dim cookieString as string = ......(get the cookie string)
Dim sqlStatement = "SELECT * FROM sometable "

if ( there is a "," in the cookieString)
{
sqlStatement += " WHERE productid = " & cookieString.substring(...)
cookieString = cookieString.substring(...) 'get rid of the first product and ","
}

where ( there is a "," in the cookieString)
{
sqlStatement += " OR WHERE productid = " & cookieString.substring(...)
cookieString = cookieString.substring(...) 'get rid of the first product and ","
}

HTH

Jagdip

Wednesday, March 28, 2012

string.lenght vs string.empty

Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?AVL wrote:
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?

I think it is faster because:
* Length is stored, doesn't need to be calculated
* integer comparison (Length == 0) is easier that string comparison,
which (I think) needs to be done on a character-by-character basis.

Hans Kesting
On Wed, 27 Apr 2005 22:46:02 -0700 in
microsoft.public.dotnet.framework.aspnet, AVL
<AVL@.discussions.microsoft.com> wrote:

>Hi,
> I read that to check for empty strings , comparing strings using
>String.Length == 0 is faster than String.Empty or "" . Is it true...?
>If so, how?

I think str.Length is probably faster, however if your string is null,
using Length will throw a NRE. I prefer to use
string.IsNullOrEmpty(someString). It safely works on null strings as
well as empty strings.

Roger
Under normal circumstances it's unlikely to give a perceptible difference...

You could easily measure and see what the IL is...

Patrice

--

"AVL" <AVL@.discussions.microsoft.com> a crit dans le message de
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?
It is true that checking the String.Length is faster.
Numeric functions are almost always more efficient than string functions.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"AVL" <AVL@.discussions.microsoft.com> wrote in message
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?

string.lenght vs string.empty

Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?AVL wrote:
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?
I think it is faster because:
* Length is stored, doesn't need to be calculated
* integer comparison (Length == 0) is easier that string comparison,
which (I think) needs to be done on a character-by-character basis.
Hans Kesting
On Wed, 27 Apr 2005 22:46:02 -0700 in
microsoft.public.dotnet.framework.aspnet, AVL
<AVL@.discussions.microsoft.com> wrote:

>Hi,
> I read that to check for empty strings , comparing strings using
>String.Length == 0 is faster than String.Empty or "" . Is it true...?
>If so, how?
I think str.Length is probably faster, however if your string is null,
using Length will throw a NRE. I prefer to use
string.IsNullOrEmpty(someString). It safely works on null strings as
well as empty strings.
Roger
Under normal circumstances it's unlikely to give a perceptible difference...
You could easily measure and see what the IL is...
Patrice
"AVL" <AVL@.discussions.microsoft.com> a crit dans le message de
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?
It is true that checking the String.Length is faster.
Numeric functions are almost always more efficient than string functions.
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"AVL" <AVL@.discussions.microsoft.com> wrote in message
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> Hi,
> I read that to check for empty strings , comparing strings using
> String.Length == 0 is faster than String.Empty or "" . Is it true...?
> If so, how?

String.Length issue

Hi. I've got a vb.net/asp.net application that passes strings back a
forth between a c++ COM object. We use simple tagging to designate
what's what, and it's worked pretty well until now. We had some data
that wasn't parsing right because it also contained tags that conflicted
with the tags designating the data.
To get around this the programmer of the COM object (not me) decided we
could put string lengths in the tags, so if an element was called x, I'd
send <x L=3>123</x> with the L= indicating the length of the string. So
I wrote a tagging function to take care of this that uses the
String.Length property to calculate the length of the string. Then I
send the following string, and he can't parse it out:

<body L=579>Dear MATT B--

Thanks for purchasing Print@dotnet.itags.org.Home tickets!
Included in this this email is a link to your ticket(s) from sale id
**SUB(wwsale_id).
To retrieve your tickets, please click (or copy/paste into your browser)
the link below:
<http://localhost/webwaresales1/GetTix.aspx?req=kXU%2bANUsFnl4YlNHjua5lA%3d%3d
You will need Adobe Acrobat Reader (available free from
http://www.adobe.com)
to view or print your items.

You will need to bring your printed tickets with you to the resort.

Thanks again and we look forward to seeing you soon!

S---, Inc.</body
The other guy said he calculates a length of 551 but if I use
String.Length I get 579. Incidentally, if I paste the string into
TextPad and look at the document properties, it says 551 characters and
a file size of 579.
So can anyone explain where the difference (28) comes from? It's not the
number of lines (15) and I don't see any other special characters
besides line endings.

Thanks!

MattMattB <somedudeus@.yahoo.com> wrote in news:3n41c6F19h9o1U3@.individual.net:

> The other guy said he calculates a length of 551 but if I use
> String.Length I get 579. Incidentally, if I paste the string into
> TextPad and look at the document properties, it says 551 characters and
> a file size of 579.
> So can anyone explain where the difference (28) comes from? It's not the
> number of lines (15) and I don't see any other special characters
> besides line endings.

Line endings actually take up 2 bytes in Windows. Carriage Return and Line
Feed.

So 579 bytes is correct - there are 15 lines, but only 14 CR + LF. So:

551 bytes + (14 CRLF x 2 bytes) = 579 : )

BTW, didn't the other guy calculate 579 too? (<body L=579>).

--
Lucas Tam (REMOVEnntp@.rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

String.Length issue

Hi. I've got a vb.net/asp.net application that passes strings back a
forth between a c++ COM object. We use simple tagging to designate
what's what, and it's worked pretty well until now. We had some data
that wasn't parsing right because it also contained tags that conflicted
with the tags designating the data.
To get around this the programmer of the COM object (not me) decided we
could put string lengths in the tags, so if an element was called x, I'd
send <x L=3>123</x> with the L= indicating the length of the string. So
I wrote a tagging function to take care of this that uses the
String.Length property to calculate the length of the string. Then I
send the following string, and he can't parse it out:
<body L=579>Dear MATT B--
Thanks for purchasing Print@dotnet.itags.org.Home tickets!
Included in this this email is a link to your ticket(s) from sale id
**SUB(wwsale_id).
To retrieve your tickets, please click (or copy/paste into your browser)
the link below:
<http://localhost/webwaresales1/GetT...//www.adobe.com)
to view or print your items.
You will need to bring your printed tickets with you to the resort.
Thanks again and we look forward to seeing you soon!
S--, Inc.</body>
The other guy said he calculates a length of 551 but if I use
String.Length I get 579. Incidentally, if I paste the string into
TextPad and look at the document properties, it says 551 characters and
a file size of 579.
So can anyone explain where the difference (28) comes from? It's not the
number of lines (15) and I don't see any other special characters
besides line endings.
Thanks!
MattMattB <somedudeus@.yahoo.com> wrote in news:3n41c6F19h9o1U3@.individual.net:

> The other guy said he calculates a length of 551 but if I use
> String.Length I get 579. Incidentally, if I paste the string into
> TextPad and look at the document properties, it says 551 characters and
> a file size of 579.
> So can anyone explain where the difference (28) comes from? It's not the
> number of lines (15) and I don't see any other special characters
> besides line endings.
Line endings actually take up 2 bytes in Windows. Carriage Return and Line
Feed.
So 579 bytes is correct - there are 15 lines, but only 14 CR + LF. So:
551 bytes + (14 CRLF x 2 bytes) = 579 : )
BTW, didn't the other guy calculate 579 too? (<body L=579> ).
Lucas Tam (REMOVEnntp@.rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
[url]http://members.ebay.com/aboutme/spot18/[/url]

Monday, March 26, 2012

strings

Pls help me write a function in c#, to exclude all the dots(.) from a string.
Like the string "123.0000" should return "1230000"
Thanks a ton.Use the Replace function or regular expressions
Thanks...

strings

i display tags on the page separated by commas

eg:tag1,tag2,tag3

if i have only 1 tag it should add

eg:tag1

not

eg:tag1,

if i have only 2 tag it should add

eg:tag1,tag2

not

eg:tag1,tag2,

how to do that because my functions returns with comma

while

(dtardrTagOther.Read())

{

strOtherTags += dtardrTagOther.GetValue(0).ToString()+ ",";

}

Hello,Just discard the last comma from the strOtherTags after the end of while loop. like

while

(dtardrTagOther.Read())

{

strOtherTags += dtardrTagOther.GetValue(0).ToString()+ ",";

}

strOtherTags = strOtherTags.Remove(strOtherTags.Length-1,1);

!!! Mark as Answer if it meets your requirement !!!


Thanks.

Try the below code

while(dtardrTagOther.Read())

{

if (strOtherTags == string.Empty)

{

strOtherTags = dtardrTagOther.GetValue(0).ToString();

}

else

strOtherTags += "," + dtardrTagOther.GetValue(0).ToString();

}

HC


after u exit while:

strOtherTags = strOtherTags.Substring(0, strOtherTags.Length - 1)

HTH

Strings and parsing and errors with split

Hi

I am fairly new to .NET. However, I am very experienced with ASP. Here is the error I am getting
Value of type '1-dimensional array of String' cannot be converted to 'String'.
I am trying the following:
Dim tot As String
tot=cmd3.executescalar()
tot = split(tot,",")
response.write (tot)
Does anyone have any ideas of how I can get the values "1,2,3" from the database (as a string), parse those into an array, and then have the database look up each value and bid the results to the page. I have the datagrid in place. At the moment, my code works with 1 variable from the database (like 12), but not multiples like "1,2,3". The numbers could be anything too - "12,17" or "1,4,5,7,33,46"
Any ideas are appreciated - I am stuck as of now.
Thanks
HBHey,

You are on the right track, but you need to declare tot as an array, as such:

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

Brian
Wow - thanks for the quick response - I will try it out today.
HB
There is still and error:
<code>
Dim tot() As String = nbrs.split(","c)
<code
The error is - Variable 'tot' hides a variable in an enclosing block.
Is this "c" suppose to be there? I also tried nbrs.split(tot,",") and it gave back the same error.
I think I am getting close. I also need to use each of the numbers from the array in a SQL statement to get information from the DB and bind it. So, there will be one column with 3 results from the array containing 3 elements.

Thanks
HB
c means convert "," to a char. In C#, you denote chars with ', which obviously you can't do in VB.NET.

I think it means you are using tot outside of the if statement block, which the object wouldn't exist. Could you post your entire code so I can verify?

Brian
Sure - here is the code - it's messy at the moment. What it is suppose to do is select a product from the main page and then publish the 3 chemicals which are used for the product. So, I am using a one to many relationship. The data is stored in the database as 1,2,3 and I must take the 1,2,3 and get product 1, then product 2, then product 3. I also need to get away from the scalar - it should be Reader after closer inspection.
<code>
<%@. Page Language="VB" Debug="True" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Collections" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<%@. Import Namespace="System.Configuration.ConfigurationSettings" %>
<%@. Register TagPrefix="header" TagName="top" src="http://pics.10026.com/?src=header.ascx" %>
<%@. Register TagPrefix="footer" TagName="bottom" src="http://pics.10026.com/?src=footer.ascx" %>
<html>
<head>
<title></title>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim Conn As SQLConnection
Dim Rdr,Rdr2 As SQLDataReader
Dim Q1,Q2,Q3,total,number,Names,stringSQL2,Word As string
Dim x As integer

Q2 = Request.QueryString("product")
Dim strConn As string = AppSettings("connect")

Dim stringSQL As string = "Select " & Q2 & " from menu_green"

Dim SQL as string="Select names1,names2,names3 from menu_green where C1='" & Q2 & "'"
SQL = SQL & "OR C2='" & Q2 & "'"
SQL = SQL & "OR C3='" & Q2 & "'"
Dim Conn2 as New SQLConnection(strConn)
Dim Cmd3 as New SQLCommand(SQL,Conn2)
Conn2.Open()
Dim tot As String
'litHowMany.text=cmd3.executescalar()
tot=cmd3.executescalar()
'Response.write ("" & tot)
'Conn2.Close()

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

stringSQL2 = "Select id,names from products where id='" & tot & "'"
Conn = New SQLConnection(strConn)
Dim Cmd2 As New SQLCommand(stringSQL2,Conn)
Conn.Open

'Dim myname As string
'myname = cmd2.executescalar()
'response.write ("" & myname & "<br>")

Rdr2 = Cmd2.ExecuteReader()
myproducts.datasource = Rdr2
myproducts.databind()
Conn.close

Conn = New SQLConnection(strConn)

Dim Cmd As New SQLCommand(stringSQL,Conn)
'Dim Cmd2 As New SQLCommand(stringSQL2,Conn)

Conn.Open
Rdr = Cmd.ExecuteReader()
mylist.datasource = Rdr
mylist.databind()
Conn.close

End Sub
</script>
<code
Thanks for any input

HB
One thing: You declared tot twice, once as a variable, once as an array.

Strings and wildcard

I am doing some things with page url's, and of couse, I test on the local host.
Only thing is, when I post to the server, the name of the host chages.

I can do an if x or y then.... but as usual, want to save code space and incase the name of the server changes, need to do something else.

What I have been searching for, unsuccessfully, is a way to insert the wildcard * into the string... such as...

If myurl = "http://" & * & "pagename.aspx" Then

Well, the * is not declared and can't seem to find how to tell the string doesn't matter the host name.

Suggestions?

Thanks all,

ZathI don't recommend approaching this problem with a wildcard. There is a simpler solution.
Try something along these lines:

 Dim requestedUrl As String = System.Web.HttpContext.Current.Request.Path.ToLower()
Select Case requestedUrl
Case "default.aspx"
' settings for homepage
Case "about.aspx"
' settings for about page
Case "contact.aspx"
' settings for contact page
Case Else
' settings for page not found
End Select
The long first line will give us the requested URL, but without the host and application name.
So this ignores the host and application name, in the same way your wildcard would have done.

I hope this helps.
Thanks!

That works even better!

After posting the message, I was actually looking for a way to get the host name to input it that way and solve the problem.

Zath
The following is a test page that I use quite often.
It allows me to see various Request and HTTP values.

In situations such as the above, you can look at the results of this test page,
and see whether there are any existing values that will match your needs.

For example, this test page will show you which Request property to use if
your URL testing also takes into account QueryStrings.

Anyway, I thought that the following may help you.

<%@. Page Language="C#" Debug="True" Trace="True" %><html>
<head>
</head>
<body>
<h2>Request Report</h2>
<hr />
<p><asp:Label id="Message" runat="server" /></p>
</body>
</html>
<script runat="server">
public void Page_Load (object sender, EventArgs e)
{
Message.Text += "Page.Request.ApplicationPath: " + Page.Request.ApplicationPath + "<br " + "/>";
Message.Text += "Page.Request.FilePath: " + Page.Request.FilePath + "<br " + "/>";
Message.Text += "Page.Request.Path: " + Page.Request.Path + "<br " + "/>";
Message.Text += "Page.Request.PathInfo: " + Page.Request.PathInfo + "<br " + "/>";
Message.Text += "Page.Request.PhysicalApplicationPath: " + Page.Request.PhysicalApplicationPath + "<br " + "/>";
Message.Text += "Page.Request.PhysicalPath: " + Page.Request.PhysicalPath + "<br " + "/>";
Message.Text += "Page.Request.RawUrl: " + Page.Request.RawUrl + "<br " + "/>";
Message.Text += "Page.Request.UserAgent: " + Page.Request.UserAgent + "<br " + "/>";
Message.Text += "Page.Request.UserHostAddress: " + Page.Request.UserHostAddress + "<br " + "/>";
Message.Text += "Page.Request.UserHostName: " + Page.Request.UserHostName + "<br " + "/>";
Message.Text += "Page.Request.Url.AbsolutePath: " + Page.Request.Url.AbsolutePath + "<br " + "/>";
Message.Text += "Page.Request.Url.Host: " + Page.Request.Url.Host + "<br " + "/>";
Message.Text += "Page.Request.Url.LocalPath: " + Page.Request.Url.LocalPath + "<br " + "/>";
Message.Text += "Page.Request.Url.PathAndQuery: " + Page.Request.Url.PathAndQuery + "<br " + "/>";
Message.Text += "<br " + "/>";

Message.Text += "<hr />Request.UrlReferrer<br>";
if (Request.UrlReferrer != null)
{
Uri referrer = Request.UrlReferrer;
Message.Text += "Request.UrlReferrer.AbsolutePath: " + referrer.AbsolutePath + "<br>";
Message.Text += "Request.UrlReferrer.AbsoluteUri: " + referrer.AbsoluteUri + "<br>";
Message.Text += "Request.UrlReferrer.Authority: " + referrer.Authority + "<br>";
Message.Text += "Request.UrlReferrer.Fragment: " + referrer.Fragment + "<br>";
Message.Text += "Request.UrlReferrer.Host: " + referrer.Host + "<br>";
Message.Text += "Request.UrlReferrer.LocalPath: " + referrer.LocalPath + "<br>";
Message.Text += "Request.UrlReferrer.PathAndQuery: " + referrer.PathAndQuery + "<br>";
Message.Text += "Request.UrlReferrer.Query: " + referrer.Query + "<br>";
Message.Text += "Request.UrlReferrer.Scheme: " + referrer.Scheme + "<br>";
Message.Text += "Request.UrlReferrer.UserInfo: " + referrer.UserInfo + "<br>";
Message.Text += "<br " + "/>";
}

Message.Text += "<hr />Request.Headers" + "<br " + "/>";
OutputCollection(Request.Headers);

Message.Text += "<hr />Request.Browser" + "<br " + "/>";
HttpBrowserCapabilities bc = Request.Browser;
Message.Text += "Type = " + bc.Type + "<br>";
Message.Text += "Name = " + bc.Browser + "<br>";
Message.Text += "Version = " + bc.Version + "<br>";
Message.Text += "Major Version = " + bc.MajorVersion + "<br>";
Message.Text += "Minor Version = " + bc.MinorVersion + "<br>";
Message.Text += "Platform = " + bc.Platform + "<br>";
Message.Text += "Is Beta = " + bc.Beta + "<br>";
Message.Text += "Is Crawler = " + bc.Crawler + "<br>";
Message.Text += "Is AOL = " + bc.AOL + "<br>";
Message.Text += "Is Win16 = " + bc.Win16 + "<br>";
Message.Text += "Is Win32 = " + bc.Win32 + "<br>";
Message.Text += "Supports Frames = " + bc.Frames + "<br>";
Message.Text += "Supports Tables = " + bc.Tables + "<br>";
Message.Text += "Supports Cookies = " + bc.Cookies + "<br>";
Message.Text += "Supports VB Script = " + bc.VBScript + "<br>";
Message.Text += "Supports JavaScript = " + bc.JavaScript + "<br>";
Message.Text += "Supports Java Applets = " + bc.JavaApplets + "<br>";
Message.Text += "Supports ActiveX Controls = " + bc.ActiveXControls + "<br>";
Message.Text += "CDF = " + bc.CDF + "<br>";

Message.Text += "<hr />Request.ServerVariables" + "<br " + "/>";
OutputCollection(Request.ServerVariables);

}

private void OutputCollection(NameValueCollection coll)
{
int loop1, loop2;
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1<arr1.Length; loop1++)
{
Message.Text += "Key: " + arr1[loop1] + "<br " + "/>";
// Get all values under this key.
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2<arr2.Length; loop2++)
{
Message.Text += "Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br " + "/>" + "<br " + "/>";
}
}
}
</script>


Don't you think usingTrace would be simpler for this?
> Don't you think using Trace would be simpler for this?

The test page does have tracing turned on, so trace information is included on the page.

But Trace alone will not show the value of the Request's properties, or the Request.UrlReferrer's properties.

I know that much of my output is redundant for a page with Trace output. But the idea is tosee how the values in the HTTP headers and the form collection translate into the various properties available in the current Request object.

A simple page like this excuses me from having to remember which of the Request properties include the querystring, and which do not. Even though it was a test page I made when I was new to ASP.NET, I still find it helpful. I simply thought it may help the original poster, too.
Thanks SomeNewKid, that test page does help a LOT!

Yes, it is because I am doing a new user email confirm activate new account page.
The URL will have a querry sting in it for confirmation.

Zath

strings in db question.

i am letting people enter a name into a textbox, then I store it in a string and
store the string in my sql database.

I noticed that I get an error when someone enters a string with a ' in it.
I'm going to test for that, and possibly make a special case to handle that
character.

are there any other characters that i need to watch out for?Depending upon the settings, you might need to guard against " (quote) as well.

The better solution is to use parameters.This article shows you how.

Strings with quotation marks that cant be accessed

Hello everyone...
I did search for my problem but most hits concerned other problems concering the quotation mark.
My problem is this:
Within a CMS, that allows .NET code to be executed I need to assign a variable to a cms-element. That cms-element contains text.
As long as it just plain text there's no problem. If the element contains an hyperlink for example the variable assignment will not work.
Is there anyway I can alter the cms-element with vb beforehand?
The idea is to remove all html code within the cms-element before the CMS exports the page to PDF...
Accessing and altering the cms-element itself is not possible in my case...
I hope I could explain the problem properly. If not, please ask.
Any hints, workarounds or thoughts will be deeply appreciated!
Philipp
no ideas?Smile [:)]

Saturday, March 24, 2012

Strings, webformatting

Hey.

I believe when I was reading through unleashed, I saw a function that converts strings to a nice webform and back again, but I can't remember, and I can't find it again, LOL...

Basically,

What I want to do:

I email users a web address containing their email and validation code

example:

http://mySite.com/verify.aspx?User=<user name>&Code=<code
now this is working awesome, except, if the username had a space in it, the system falls down (as the link stops where the space is)

so basically, I wanna know if there is a function I can run on the string containing the web address that will convert the spaces to %20, and if there is a function that converts it back again (%20's to a space).

I'm sure I could code my own function and put it in a namespace or something, but it'd be so much nicer if there was a default function (YAY)

is there? or am I stark raving mad?!?! lol

Thanks for your help
-AshleighHi Ashleigh

Perhaps the "Server.URLEncode" and "Server.UrlDecode" will help you.

Thursday, March 22, 2012

Struct Default Constructor Question

When I create a new object of type struct, I want objects to be created for each member variable in that struct. For example:

private struct Strings {
public string s1;
public string s2;
}

Strings myStrings = new Strings();

Based on what I read, the default constructor for the struct should automatically initalize s1 and s2 to string.Empty implicitly. However, doing something like int size = myStrings.s1.Length throws a NullReferenceException. I don't understand why myStrings.s1 does not exist. Aren't s1 and s2 created automatically when I create a new object called myStrings??

Hello anonim:

> Based on what I read, the default constructor for the struct should automatically initalize s1 and s2 to string.Empty implicitly.

I'm afraid this is not correct. There's no implicit initialization of objects anywhere and your strings won't be created automatically.

So here is how one commonly would write it:

private struct Strings {
public string s1;
public string s2;

public Strings() {
s1 = String.Empty;
s2 = String.Empty;
}
}

Strings myStrings = new Strings();

Feel free to ask more.

HTH. -LV


Ludovico, thanks for your reply.

Per the MSDN [link]:

"Unlike a class, a struct is not permitted to declare a parameterlessinstance constructor. Instead, every struct implicitly has aparameterless instance constructor that always returns the value thatresults from setting all value type fields to their default value andall reference type fields to null. A struct can declare instance constructors having parameters."

So based on that, you cannot create a parameterless constructor for any struct (public Strings() { ...} does not compile - try it).

The statement above says all value type fields are set to their default value, and all reference type fields are set to null. Based on the fact that the implicit constructor does not initialize the string variables in my struct, I can safely assume that these string variables are reference types, correct? So, what I must do is to create a constructor with parameters and use that to instantiate and initialize the struct member variables.

Thanks for your help.

anonim:

> Unlike a class, a struct is not permitted to declare a parameterless instance constructor.

Yes, you are right, sorry...

> The statement above says all value type fields are set to their default value, and all reference type fields are set to null. Based on the fact that the implicit constructor does not initialize the string variables in my struct, I can safely assume that these string variables are reference types, correct?

Yes: String is a class, so strings are reference types, not value types.

> So, what I must do is to create a constructor with parameters and use that to instantiate and initialize the struct member variables.

Right again!

Cheers. :) -LV


Thank you much!