Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Saturday, March 31, 2012

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

Hi

How to convert string to object?

i.e.

string objname = "Control1"

then some how use (function or method)

objname.visible = false

Hi There,

asmgx:

How to convert string to object?

i.e.

string objname = "Control1"

then some how use (function or method)

objname.visible = false

You can assign string value to object

e.g

string value = "control1";

object obj = value;

But not using string value to identify as object.

However, you can use string value as id and find the control.

e.g

Say you wanted to get textbox with id myTextBox

TextBox txt = (TextBox)this.FindControl("myTextBox"); //


in c#

String obj ="Control1";

Object objCast = ((Object)obj);

in vb

Dim objAsString = "Control1"

Dim objCastAsObject = (CType(obj,Object))

asmgx:

then some how use (function or method)

objname.visible = false

what exact you want to do?

Wednesday, March 28, 2012

string.remove and while

I have the following method that should remove html comments. But it's not reacting the way I expect. When the loop breaks and I return the string, I get the correct value - all comments are removed. But the loop will not break using neither the while conditional statement or the internal IndexOf check, therefore stuck in an infinite loop (save for the counter that breaks it).

What am I missing??

public static string RemoveComments(string page) {int i = 0;while (page.IndexOf("<!--") > 0) {int _CommentStart = page.IndexOf("<!--");int _CommentEnds = page.IndexOf("-->");if (_CommentStart > 0) {if (_CommentEnds <= _CommentStart) {break; } page.Remove(_CommentStart, (_CommentEnds - _CommentStart)); }if (page.IndexOf("<!--") < 1) { i = 100; } i++;if (i > 10)break; }return page +" " + i.ToString(); }
I'm not following that logic too well - I don't use c# either - but wouldn't this be a case for regular expressions ?
I'm not a regular expressions expert, but I'm sure there is a way to look for that string and remove it.
Do a search on using regular expressions...

Thank you, very good suggestion. I used the following:

System.Text.RegularExpressions.Regex r =new System.Text.RegularExpressions.Regex(@."\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>");return r.Replace(page,string.Empty);

Monday, March 26, 2012

String.Split with multi character delimiter

The Split method of the String class takes an array of char as it's
parameter. Rather than declaring a String, you need to declare a char array,
and work with that. Example:

Dim delString As String = "#|#"
Dim del As Char() = delString.ToCharArray()

...

s = source.Split(del)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"PCK" <anonymous@dotnet.itags.org.discussions.microsoft.com> wrote in message
news:3CCCD026-B1BB-4F2D-8DE2-7D3BD398127F@dotnet.itags.org.microsoft.com...
> Hi,
> I have a string that I would like split into a single dimension string
array. The source string has a multi character delimiter. It appears that
string.split(delimiter) only splits based on the first character of the
delimiter.
> For example:
> Dim s() As String
> Dim source As String
> Dim del As String
> del = "#!#"
> source = "ABC#!#DEF#!#GHI"
> s = source.Split(del)
> s(0) = "ABC"
> s(1) = "!"
> s(2) = "DEF"
> s(3) = "!"
> s(4) = "GHI"
> Where as I would like the following returned. Does anyone know of a built
in function to do this?
> s(0) = "ABC"
> s(1) = "DEF"
> s(2) = "GHI"
> Thanks.
> PCKKevin, thanks for the response

I had thought about that earlier. Actually what happens when you use a char array is that it splits based on any item in the array not on the concatenation of all items in the array

Do you have any other suggestions.
myString.Replace( "#!#", "!" ).Split ( "!" ); ?

Dan.

"PCK" <anonymous@.discussions.microsoft.com> wrote in message
news:3F0E32B3-4C65-4AA2-A1A2-110F0644FA6A@.microsoft.com...
Kevin, thanks for the response.

I had thought about that earlier. Actually what happens when you use a char
array is that it splits based on any item in the array not on the
concatenation of all items in the array.

Do you have any other suggestions.
> I had thought about that earlier. Actually what happens when you use a
char array is that it splits based on any item in the array not on the
concatenation of all items in the array.

Oops. Sorry. That's true. Well, you could always use the Visual Basic.Net
Split() method (function), which takes a string as an argument.
Alternatively, you could try Daniel's solution.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"PCK" <anonymous@.discussions.microsoft.com> wrote in message
news:3F0E32B3-4C65-4AA2-A1A2-110F0644FA6A@.microsoft.com...
> Kevin, thanks for the response.
> I had thought about that earlier. Actually what happens when you use a
char array is that it splits based on any item in the array not on the
concatenation of all items in the array.
> Do you have any other suggestions.
the problem is the split method is not intended for use with string
seperators, to do one that works properly, you'd have to write one yourself.

alternatively, if there is a character you know (think highly likely) will
NOT appear in your text, use the replace method to convert to that character
as a seperator, then split.

hope that helps.
Dan.

"PCK" <anonymous@.discussions.microsoft.com> wrote in message
news:A6A5A7D8-5BB1-46D7-BC35-ABD8DD55130E@.microsoft.com...
Dan, thanks for the response.

But what if the data was "ABC#!#D!EF#!#GHI"

That would split it out as

ABC
D
EF
GHI

versus

ABC
D!EF
GHI
The VB Split method is what I was looking for. Thanks for the help.

StringBuilder to Text

Hello, I a m building an email message in a StringBuilder Object and then
using the StringBuilder ToString() method to get it to regular text. It
appears to me that when I place a "\n" into the Stringbuilder object that
when this is translated into text, I loose the page break. Is this true and
I doing something wrong?
I'm guessing that I could insert a special character into the StringBuilder
object and then use the string Replace() method to translate that special
character into a "\n" and perhaps this would work.
Just wondering why it does not work when I place that value into the
StringBuilder Object.
ThanksHow are you noticing that you loose the break? In what context is this
happening?
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Have you considered using Environment.Newline instead of "\n"?
Example:
StringBuilder textBuilder = new StringBuilder(512);
textBuilder.Append("Dear John,");
textBuilder.Append(Environment.Newline)
Perhaps that will help.
carl
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Try Environment.NewLine
Remy
I am loosing the page break when I use the StringBuilder ToString() method
when I am formatting an email message. I will try the Environment.NewLine()
method and see if that retains the line break.
Does the StringBuilder cause the newline disappear, or is it something with
the mail component. What happens if you just set the
StringBuilder.ToString() to a variable and looked at it, is the newline
gone?
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:9876DF24-F310-4FDC-826D-D7872576655C@.microsoft.com...
>I am loosing the page break when I use the StringBuilder ToString() method
> when I am formatting an email message. I will try the
> Environment.NewLine()
> method and see if that retains the line break.
>
I assume you are using c?
If you pass "\n" you'll get newline > chr(10)
If you want "\n" you might consider to use "\\n"
(Outlook express mangels this for me, use slash twice)
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> schreef in bericht
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks

StringBuilder to Text

Hello, I a m building an email message in a StringBuilder Object and then
using the StringBuilder ToString() method to get it to regular text. It
appears to me that when I place a "\n" into the Stringbuilder object that
when this is translated into text, I loose the page break. Is this true and
I doing something wrong?

I'm guessing that I could insert a special character into the StringBuilder
object and then use the string Replace() method to translate that special
character into a "\n" and perhaps this would work.

Just wondering why it does not work when I place that value into the
StringBuilder Object.

ThanksHow are you noticing that you loose the break? In what context is this
happening?

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Have you considered using Environment.Newline instead of "\n"?

Example:

StringBuilder textBuilder = new StringBuilder(512);
textBuilder.Append("Dear John,");
textBuilder.Append(Environment.Newline)

Perhaps that will help.

carl

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Try Environment.NewLine

Remy
I am loosing the page break when I use the StringBuilder ToString() method
when I am formatting an email message. I will try the Environment.NewLine()
method and see if that retains the line break.
Does the StringBuilder cause the newline disappear, or is it something with
the mail component. What happens if you just set the
StringBuilder.ToString() to a variable and looked at it, is the newline
gone?

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:9876DF24-F310-4FDC-826D-D7872576655C@.microsoft.com...
>I am loosing the page break when I use the StringBuilder ToString() method
> when I am formatting an email message. I will try the
> Environment.NewLine()
> method and see if that retains the line break.
I assume you are using c?

If you pass "\n" you'll get newline > chr(10)
If you want "\n" you might consider to use "\\n"
(Outlook express mangels this for me, use slash twice)

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> schreef in bericht
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks

Saturday, March 24, 2012

StringWriter object and dispose method

Re hello,
I m' sorry for my questions ..
Under framework 1.1 with vb.net, i m using a StringWriter object to export
in .xls file.
To empty memory, I would like to use the propterty dispose on the object
StringWriter.
But i have received an error like this :
example :
Dim myStringWriter As System.IO.StringWriter = New System.IO.StringWriter
...
myStringWriter.Dispose(System.Boolean)(true)
myStringWriter=Nothing
BC30390: 'System.IO.StringWriter.Protected Overrides Sub Dispose(disposing
As Boolean)' is not accessible in that context. it is protected..
I can use the Close() method but i m not sure that this last one have the
same effects for Garbage Collector ?
Is it possible to use Dispose method with StringWriter object
Thanks for you help
fabriceUse Close(). Setting the objec to null stops the object from being
referenced, so the GC will clear it out when it's ready. Note that Dispose
DOES not remove the object from memory.
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
"fabrice" <emouchet@.test.com> wrote in message
news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...
> Re hello,
> I m' sorry for my questions ..
> Under framework 1.1 with vb.net, i m using a StringWriter object to export
> in .xls file.
> To empty memory, I would like to use the propterty dispose on the object
> StringWriter.
> But i have received an error like this :
> example :
> Dim myStringWriter As System.IO.StringWriter = New System.IO.StringWriter
> ...
> myStringWriter.Dispose(System.Boolean)(true)
> myStringWriter=Nothing
>
> BC30390: 'System.IO.StringWriter.Protected Overrides Sub Dispose(disposing
> As Boolean)' is not accessible in that context. it is protected..
>
> I can use the Close() method but i m not sure that this last one have the
> same effects for Garbage Collector ?
> Is it possible to use Dispose method with StringWriter object
>
> Thanks for you help
> fabrice
>
Great.
Thanks for you help.
have a nice day
"Dan Bass" <na> a crit dans le message de news:
%23mDFZP$BHHA.3540@.TK2MSFTNGP03.phx.gbl...
> Use Close(). Setting the objec to null stops the object from being
> referenced, so the GC will clear it out when it's ready. Note that Dispose
> DOES not remove the object from memory.
> http://msdn.microsoft.com/msdnmag/issues/1100/gci/
>
> "fabrice" <emouchet@.test.com> wrote in message
> news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...
>
remember that you also don't need <myObject> = Nothing explicitly in order
for the GC to clean the object up, although it is (IMO) good coding
practice.
"fabrice" <emouchet@.test.com> wrote in message
news:ewVgTr$BHHA.4928@.TK2MSFTNGP02.phx.gbl...
> Great.
> Thanks for you help.
> have a nice day
> "Dan Bass" <na> a crit dans le message de news:
> %23mDFZP$BHHA.3540@.TK2MSFTNGP03.phx.gbl...
>
Thanks Dan.
"Dan Bass" <na> a crit dans le message de news:
OgD8SCACHHA.3380@.TK2MSFTNGP04.phx.gbl...
> remember that you also don't need <myObject> = Nothing explicitly in order
> for the GC to clean the object up, although it is (IMO) good coding
> practice.
> "fabrice" <emouchet@.test.com> wrote in message
> news:ewVgTr$BHHA.4928@.TK2MSFTNGP02.phx.gbl...
>

StringWriter object and dispose method

Re hello,

I m' sorry for my questions ..
Under framework 1.1 with vb.net, i m using a StringWriter object to export
in .xls file.
To empty memory, I would like to use the propterty dispose on the object
StringWriter.
But i have received an error like this :

example :

Dim myStringWriter As System.IO.StringWriter = New System.IO.StringWriter
...
myStringWriter.Dispose(System.Boolean)(true)
myStringWriter=Nothing

Quote:

Originally Posted by

Quote:

Originally Posted by

>Error


BC30390: 'System.IO.StringWriter.Protected Overrides Sub Dispose(disposing
As Boolean)' is not accessible in that context. it is protected..

I can use the Close() method but i m not sure that this last one have the
same effects for Garbage Collector ?
Is it possible to use Dispose method with StringWriter object

Thanks for you help
fabriceUse Close(). Setting the objec to null stops the object from being
referenced, so the GC will clear it out when it's ready. Note that Dispose
DOES not remove the object from memory.

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
"fabrice" <emouchet@.test.comwrote in message
news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

Re hello,
>
I m' sorry for my questions ..
Under framework 1.1 with vb.net, i m using a StringWriter object to export
in .xls file.
To empty memory, I would like to use the propterty dispose on the object
StringWriter.
But i have received an error like this :
>
example :
>
Dim myStringWriter As System.IO.StringWriter = New System.IO.StringWriter
...
myStringWriter.Dispose(System.Boolean)(true)
myStringWriter=Nothing
>

Quote:

Originally Posted by

Quote:

Originally Posted by

>>Error


>
BC30390: 'System.IO.StringWriter.Protected Overrides Sub Dispose(disposing
As Boolean)' is not accessible in that context. it is protected..
>
>
I can use the Close() method but i m not sure that this last one have the
same effects for Garbage Collector ?
Is it possible to use Dispose method with StringWriter object
>
>
Thanks for you help
fabrice
>


Great.
Thanks for you help.
have a nice day

"Dan Bass" <naa crit dans le message de news:
%23mDFZP$BHHA.3540@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>
Use Close(). Setting the objec to null stops the object from being
referenced, so the GC will clear it out when it's ready. Note that Dispose
DOES not remove the object from memory.
>
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
>
>
>
"fabrice" <emouchet@.test.comwrote in message
news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>Re hello,
>>
>I m' sorry for my questions ..
>Under framework 1.1 with vb.net, i m using a StringWriter object to
>export in .xls file.
>To empty memory, I would like to use the propterty dispose on the object
>StringWriter.
>But i have received an error like this :
>>
>example :
>>
>Dim myStringWriter As System.IO.StringWriter = New System.IO.StringWriter
>...
>myStringWriter.Dispose(System.Boolean)(true)
>myStringWriter=Nothing
>>

Quote:

Originally Posted by

>>>Error


>>
>BC30390: 'System.IO.StringWriter.Protected Overrides Sub
>Dispose(disposing As Boolean)' is not accessible in that context. it is
>protected..
>>
>>
>I can use the Close() method but i m not sure that this last one have the
>same effects for Garbage Collector ?
>Is it possible to use Dispose method with StringWriter object
>>
>>
>Thanks for you help
>fabrice
>>


>
>


remember that you also don't need <myObject= Nothing explicitly in order
for the GC to clean the object up, although it is (IMO) good coding
practice.

"fabrice" <emouchet@.test.comwrote in message
news:ewVgTr$BHHA.4928@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Great.
Thanks for you help.
have a nice day
>
"Dan Bass" <naa crit dans le message de news:
%23mDFZP$BHHA.3540@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>>
>Use Close(). Setting the objec to null stops the object from being
>referenced, so the GC will clear it out when it's ready. Note that
>Dispose DOES not remove the object from memory.
>>
>http://msdn.microsoft.com/msdnmag/issues/1100/gci/
>>
>>
>>
>"fabrice" <emouchet@.test.comwrote in message
>news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>>Re hello,
>>>
>>I m' sorry for my questions ..
>>Under framework 1.1 with vb.net, i m using a StringWriter object to
>>export in .xls file.
>>To empty memory, I would like to use the propterty dispose on the object
>>StringWriter.
>>But i have received an error like this :
>>>
>>example :
>>>
>>Dim myStringWriter As System.IO.StringWriter = New
>>System.IO.StringWriter
>>...
>>myStringWriter.Dispose(System.Boolean)(true)
>>myStringWriter=Nothing
>>>
>>>>Error
>>>
>>BC30390: 'System.IO.StringWriter.Protected Overrides Sub
>>Dispose(disposing As Boolean)' is not accessible in that context. it is
>>protected..
>>>
>>>
>>I can use the Close() method but i m not sure that this last one have
>>the same effects for Garbage Collector ?
>>Is it possible to use Dispose method with StringWriter object
>>>
>>>
>>Thanks for you help
>>fabrice
>>>


>>
>>


>
>


Thanks Dan.

"Dan Bass" <naa crit dans le message de news:
OgD8SCACHHA.3380@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

remember that you also don't need <myObject= Nothing explicitly in order
for the GC to clean the object up, although it is (IMO) good coding
practice.
>
"fabrice" <emouchet@.test.comwrote in message
news:ewVgTr$BHHA.4928@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

>Great.
>Thanks for you help.
>have a nice day
>>
>"Dan Bass" <naa crit dans le message de news:
>%23mDFZP$BHHA.3540@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>>>
>>Use Close(). Setting the objec to null stops the object from being
>>referenced, so the GC will clear it out when it's ready. Note that
>>Dispose DOES not remove the object from memory.
>>>
>>http://msdn.microsoft.com/msdnmag/issues/1100/gci/
>>>
>>>
>>>
>>"fabrice" <emouchet@.test.comwrote in message
>>news:Os20oF$BHHA.4472@.TK2MSFTNGP03.phx.gbl...
>>>Re hello,
>>>>
>>>I m' sorry for my questions ..
>>>Under framework 1.1 with vb.net, i m using a StringWriter object to
>>>export in .xls file.
>>>To empty memory, I would like to use the propterty dispose on the
>>>object StringWriter.
>>>But i have received an error like this :
>>>>
>>>example :
>>>>
>>>Dim myStringWriter As System.IO.StringWriter = New
>>>System.IO.StringWriter
>>>...
>>>myStringWriter.Dispose(System.Boolean)(true)
>>>myStringWriter=Nothing
>>>>
>>>>>Error
>>>>
>>>BC30390: 'System.IO.StringWriter.Protected Overrides Sub
>>>Dispose(disposing As Boolean)' is not accessible in that context. it is
>>>protected..
>>>>
>>>>
>>>I can use the Close() method but i m not sure that this last one have
>>>the same effects for Garbage Collector ?
>>>Is it possible to use Dispose method with StringWriter object
>>>>
>>>>
>>>Thanks for you help
>>>fabrice
>>>>
>>>
>>>


>>
>>


>
>

Thursday, March 22, 2012

Strongly Type Web Forms

How does a User Control invoke a method on its parent page? Say I have a publicMethod in code behind for an aspx page:
...
public void SomeImportantMethod() { /* do important stuff */}
...
From an Event Handler within the User Control, I'd like to invoke a method on the page in which the User Control is embedded. If I stop in the debugger and inspect this.Page, I see that it has a type of "ASP.MyAspxPage"... where did that come from? Can I delcare it intentionally somewhere?
TIA,
Geo
Your MyAspxPage is your custom page class, which inherits from the System.Web.UI.Page class.
The thing with UserControls is though, that you don't know what parent page they could be in at run-time.
So, to be safe, if you want to call a particular method on the parentPage, I'd say let your Page implement your interface which contains themethod signature (and other stuff if you want).
In your UserControl you then do a defensive cast (using the as keyword)to that interface and check for != null. You then simply call yourmethod on your interface object.
Hope that helps.
Wim

Yes, that does help. It helped in that I was hung up in anold paradigm and should get over it and just use an interface. So, thanks, that helped me get the job done.
I guess I was stuck in theOLD ASP.NET 1.1 code behind days where an ASPX page would have a code behind page which had a namespace declaration and the web form would be a public class derived from System.Web.UI.Page. So, from any C# code I could say:
MyNamespace.MyPage myPage = this.Page as MyNameSpace.MyPage;
if ( myPage != null)
{
myPage.MyMethod("hello world");
}
else
{
Response.Write("what gives?");
}
No fuss no muss, no interfaces.
I think I better go find an article or 2 on code beside or what ever it is we call it now.
Thanks again,
geo

Hello.

I think that you can also use the @.reference directive to introduce the type of the page in the user control (haven't tried it though).

struct inside method

Hi all!

I have the need to create a struct that will only be usefull inside a method. I have been trying to do so by doing something like this:


public class myClass
{
public void myMethod
{
private struct myStruct
{ int myInt; }
}
}

I a keep getting compiler errors such as:


} expected

Can somebody give me any clue on this?

Thanks in advance!

LAMScoping a struct to a method isn't allowed. You can scope a struct to a class, though, using the private keyword and embedding it inside a class.


public class MyClass
{
private struct MyStruct { int MyInt; }
public void MyMethod()
{
MyStruct x = new MyStruct();
}
}

This should work perfectly fine.

Nick